Tuesday 27 September 2011

MULTIPLE INHERITANCE


Exno:                                       MULTIPLE INHERITANCE

Aim:
To write a java program to implement the concept of multiple inheritance.
Algorithm:
Step1: Start.
Step2: Define a class stu to get the name and roll no and display the same.
Step3: Define a class test that acts as a sub-class of stu. Get the marks m1, m2 and display the same.
Step4: Define an interface sports that carries the spors weightage.
Step5: Define class  result that extends class test and implements interface sports.
Step6: Calculate the total marks in class result.
Step7: Define class final pgm and void main() as public static.
Step8: Declare an object ‘s’ to class result and invoke getdata(), getmark() and display() to print the output.
Step9: Stop.
Program:
class stu
{
String name;
int rollno;
void getdata(String s,int r)
{
name=s;
rollno=r;
}
void putdata()
{
System.out.println("\nName="+name);
System.out.println("\nRoll No="+rollno);
}
}
class test extends stu
{
int m1,m2;
void getmarks(int x,int y)
{
m1=x;
m2=y;
}
void putmarks()
{
System.out.println("\nMark1="+m1);
System.out.println("\nMark2="+m2);
}
}
interface sports
{
final int sp=6;
abstract void putsp();
}
class result extends test implements sports
{
int tot;
public void putsp()
{
System.out.println("\nSports wt="+sp);
}
void display()
{
tot=m1+m2+sp;
putdata();
putmarks();
putsp();
System.out.println("\nTotal="+tot);
}
}
class ex2
{
public static void main(String args[])
{
result s=new result();
s.getdata("Anitha",101);
s.getmarks(98,99);
s.display();
}
}


OUTPUT:

Z:\anitha>javac ex2.java
Z:\anitha>java ex2
Name=Anitha
Roll No=101
Mark1=98
Mark2=99
Sports wt=6
Total=197

0 comments:

Post a Comment