Derivation
of a class involving more than one form of Inheritance is called Hybrid
Inheritance.
Example:
import javax.swing.JOptionPane;
class Student
{
int
rno;
String
name;
void
readdata1()
{
rno=Integer.parseInt(JOptionPane.showInputDialog("Enter
roll number"));
name=JOptionPane.showInputDialog("Enter
name");
}
void
printdata1()
{
System.out.println("Roll
number="+rno);
System.out.println("Name ="+name);
}
}
class Test extends Student
{
int
m1,m2,m3;
void
readdata2()
{
m1=Integer.parseInt(JOptionPane.showInputDialog("Enter
sub1 marks"));
m2=Integer.parseInt(JOptionPane.showInputDialog("Enter
sub2 marks"));
m3=Integer.parseInt(JOptionPane.showInputDialog("Enter
sub3 marks"));
}
void
printdata2()
{
System.out.println("Sub1="+m1+" Sub2="+m2+" Sub3="+m3);
}
}
interface Sports
{
int
smarks=55;
public
void printdata3();
}
class Result extends Test
implements Sports
{
int
sum;
public
void printdata3()
{
System.out.println("Sports
marks="+smarks);
}
public
void readdata3()
{
readdata1();
readdata2();
}
public
void printdata4()
{
sum=m1+m2+m3+smarks;
printdata1();
printdata2();
printdata3();
System.out.println("Total
Marks="+sum);
}
}
class hy
{
public
static void main(String[] args)
{
Result
obj=new Result();
obj.readdata3();
obj.printdata4();
}
}
No comments:
Post a Comment