Java Programming
About Lesson

Why java not providing Multiple Inheritance directly?

Java is not supporting multiple inheritance directly like other Object Oriented Programming Languages like C++.  If there is any need of multiple inheritance, it can be achieved by using interfaces.  There is an ambiguity problem with multiple inheritance where two methods with same name but different definitions exist in two parent classes.  The object of child class will get confusion that which definition of its parents to be followed.

The above diagram is depicting how ambiguity can lead to ambiguity in the child class due to Multiple Inheritance in OOP Languages.

There are some other solutions for the above said ambiguity problem in C++, but java completely eliminated direct multiple inheritance to avoid the ambiguity problem.  The solution of java for multiple inheritance is Interface.

What is an Interface?

An interface a special class that can only have final data members and methods without definition.  All the data members of an interface are static and final and methods of interface are public and abstract implicitly.  We need not to use the respective keywords before the members.  A method without body or definition is known as abstract method. 

The interface and implement keywords

The interface keyword is used to declare interfaces.  The implements keyword to be used to derive a new class from an interface.

//testInterface.java
interface calculate
{
public double calInt();
}
class savingAc implements calculate
{
int acNo,amt,time;
double rateOfInt;
savingAc(int a,double b,int c, int d)
{
acNo=a;
rateOfInt=b;
time=c;
amt=d;
}
void displayInt()
{
System.out.println(“Interest for Account No:”+acNo+”is Rs.”+calInt());
}
public double calInt()
{
return(amt*rateOfInt*time/100);
}
}
public class testInterface
{
public static void main(String args[])
{
savingAc s=new savingAc(1010,4.5,5,5000);
s.displayInt();
}
}

A class implements an interface must define the abstract methods. Since abstract methods do not have implementation, a class can be inherited from two or more interfaces too.

interface Father
{
void doEducation();
}
interface Mother
{
void doEducation();
}
class Son implements Father,Mother
{
void doEducation()
{
System.out.println(“I will become Software Engineer”);
}
}

The class which is implementing one or more interfaces, must define all the abstract methods existing in its parent interfaces.

Two or more interfaces can be extend into another interface.
interface Guardian extends Father,Mother
{
void doTakecare();
}

classes vs interfaces:
• objects cannot be declared based on interfaces (no instantiation)
• interface doesn’t contain constructors
• all of the methods of interface are implicitly public and abstract
• Interface cannot contain instance fields. If any fields, they should be declared as static and final.
• An interface cannot be implemented to another interface but is implemented by a class
• An interface can extend multiple interfaces.

properties of an interface:
• abstract keyword is not necessary to be used since all the members of interface are by default abstract
• methods in the interface are implicitly public and abstract.

How an Interface is solving ambiguity problem of Multiple Inheritance?

//example of interface
interface animal
{
public void eat();
public void travel();
}

A class uses implement keyword to implement an interface.
/* File name : MammalInt.java */
public class MammalInt implements Animal{

public void eat(){
System.out.println(“Mammal eats”);
}

public void travel(){
System.out.println(“Mammal travels”);
}

public int noOfLegs(){
return 0;
}

public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}

 

//IntAndInh.java – program that contains inheritance of interface
interface one
{
void methodOne();
}

interface two extends one
{
public void methodTwo();
}

class interfaceInherit implements two
{
public void methodOne()
{
System.out.println(“Method of first interface is implemented”);
}
public void methodTwo()
{
System.out.println(“Method of Second interface is implemented”);
}
}

class InhOfInt
{
public static void main(String args[])
{
System.out.println(“Interface Inheritance Demonstration”);
interfaceInherit ob=new interfaceInherit();
ob.methodOne();
ob.methodTwo();
}
}

//program implements two interfaces into single class
interface First
{
void MethodOne( );
int MyValue1 = 100;
}
interface Second
{
public void MethodTwo();
int MyValue2 = 200;
}
class interfaceIn implements First,Second
{
public void MethodOne()
{
System.out.println(“Method of interface First is implemented with value:”+MyValue1);
}
public void MethodTwo()
{
System.out.println(“Method of interface Second is implemented with value:”+MyValue2);
}
}
class TwoInterfTest
{
public static void main( String args[])
{
interfaceIn object = new interfaceIn() ;
object.MethodOne();
object.MethodTwo();
}
}

You cannot copy content of this page