Multiple inheritance using interface

Multiple inheritance using interface

We know that in multiple inheritance , sub classes are derived from multiple super classes. If two super classes have same names for their members then which member is inherited into the sub class is the main confusion in multiple inheritance. This is the reason, java does not support the concept of multiple inheritance. This confusion is reduced by using multiple interfaces to achieve multiple inheritance.

Program
interface sample1
{
void display();
}
interface sample2
{
void show();
}
class test implements sample1,sample2
{
public void display()
{
System.out.println(“Hello”);
}
public void show()
{
System.out.println(“This is Basicofy.com”);
}
public static void main(String args[])
{
test obj = new test();
obj.display();
obj.show();
}
}

Output:
Hello
This is Basicofy.com

Program
interface father
{
float ht=6;
void height();
}
interface mother
{
float ht=5.5;
void height();
}
class child implements father,mother
{
public void height()
{
float ht=(father.ht+mother.ht)/2;
System.out.println(“Child height “+ht);
}
public static void main(String args[])
{
child obj = new child();
obj.height();
}
}

Output:
Child height 5.75

Leave a Reply

Your email address will not be published. Required fields are marked *