Java Interfaces Multiple Inheritance
Multi-level Inheritance with Two Interfaces
A programmer should be a little careful in using inheritance between two interfaces. It must be "extends" keyword between two interfaces.
interface Reptiles
{
void size();
}
interface Crocodile extends Reptiles
{
void amphibian();
}
public class Features implements Crocodile
{
public void size()
{
System.out.println("Reptiles are of small size except crocs and a few snakes and turtles");
}
public void amphibian()
{
System.out.println("Croc is amphibian");
}
public static void main(String args[])
{
Features f1 = new Features();
f1.size();
f1.amphibian();
}
}
Observe the usage of "extends" between the two interfaces Reptiles and Crocodile. The abstract methods of the both are overridden by the Features class. Observe the figure representing above classes.
Multiple Inheritance with Interfaces
There is another way of using interfaces. Interfaces support multiple inheritance. That is, after "extends" keyword, there can be any number of interfaces. This is not possible with abstract classes and concrete classes.
interface Hello1 { void display1(); }
interface Hello2 { void display2(); }
interface Hello3 extends Hello1, Hello2 // notice, after extends two interfaces
{
void display3();
}
public class StrangeInheritance implements Hello3
{
public void display1() { System.out.println("Display 1"); }
public void display2() { System.out.println("Display 2"); }
public void display3() { System.out.println("Display 3"); }
public static void main(String args[])
{
StrangeInheritance si1 = new StrangeInheritance();
si1.display1(); si1.display2(); si1.display3();
}
}
Interface Hello3 extends two interfaces, Hello1 and Hello2. The concrete class StrangeInheritance implements Hello3 and overrides the abstract methods of all the three. Even if one is omitted, it is a compilation error. That is, among interfaces, we can go for multiple inheritance. Following figure gives the details.
The above inheritance can be achieved as follows also.
public class StrangeInheritance implements Hello1, Hello2, Hello3
Getting Objects of Interfaces
This topic is discussed in Object Casting and Java Dynamic Binding Dynamic Polymorphism.
Pages: 1 2
Sir… .in abstract class Variables can be any access specifier including private….if I using private with variable in super class(abstract class)…how can I access in sub class or call
Hi Good Day to you all ..
I need some clarity with this lines.. can anyone please help me with sample lines for the followings:
Interface reference variable can be assigned with concrete subclass objects. Once assigned, the interface reference variable works like an object. This feature applies to abstract classes also.
interface Test
{
public abstract void display();
}
public class Demo implements Test
{
public void display()
{
System.out.println(“Hello Sir”);
}
public static void main(String args[])
{
Test t1; // reference variable of an interface
// t1.display(); // error
Demo d1 = new Demo(); // subclass class object
t1 = d1; // subclass object assigned to super class reference variable
t1.display(); // now t1 works practically as an object
}
}
Sir please explain me why multiple inheritance is not supported in Java?
Just to avoid the confusion of virtual functions. Java proved an OOPS language can exist without multiple inheritance.
Sir I am Confusing in a doubt, If there are 2 interface having same method, The class which implements both interface, and override the method, than How can we know which Interface method the class is Overriding.
interface A
{
public void m1();
}
interface B
{
public void m1();
}
class Demo implements A, B
{
public void m1() // How can we know this m1() is of which interface
{
}
}
________________________________________________________
Please Sir, Solve this problem as soon as possible, I have to face an Interview Tomorrow.
Thanks
Ankit
You have the same method in both interfaces. When you override both in the implementation class, it becomes duplication of method. For this reason, override only once. Which interface the method belongs is irrelevant as you are overriding only once.
Sir,
What is the advantage of using Interference classes, anyway we will give method definitions in the sub classes. so, what is the point of using an interface classes and extending them?
See this link:
http://way2java.com/oops-concepts/what-is-interface-in-java-with-realtime-example/
Sir,can we use same method name in two interfaces and if we implement both these interfaces in one class then which method will be executed? and why? Since both interfaces have same mehod name what about ambiguity??
No ambiguity. You overriding only once. When both have same abstract method, overriding only once is enough. Which one is overridden is taken care by Java; you can think both are overridden.
Sir,
Can a interface should hold only abstract methods?
Interface holds only abstract methods please.
Hello Sir,
i am a fresher or beginner in java.
Please tell me,
1. When to use Interface instead of Abstract class with an example.
1. When to use Abstract class instead of interface with an example.
In inheritance, if the super class would like to give all methods with implementation (body), prefer a concrete class. If the super class would like to give a few methods with implementation, prefer abstract class and if the super class would like to not give implementation for all methods, prefer interface.
1. if u want to write instance variable, static variable & constructor, go with abstract class.
2.if u want to write abstract method & concrete methods then go with abstract class
3. if u want to write abstract method only, both are ok but prefer interface because of multiple inheritance.
4. and if u want to write static variables & abstract method only, then go with interface bcoz of multiple inheritance.
Sir, firstly what is default access specifier in java.
e.g class abc{}
what is the access specifier of class abc?
secondly, i didn’t get what does it mean?
All interfaces methods should be overridden with public only as OVERRIDDEN METHOD CANNOT HAVE WEAKER ACCESS SPECIFIER.
Default specifier classes are accessible by the other classes of within the package only.
Overridden methods cannot have less scope access specifier. That is, if in the super class a method is protected, the subclass should override with either public or protected but not by default or private.
Thankyou sir..
this is awsomeeeeeeeeeeeeee
thanks a lot.. i understud it well.. i want to hav another example of how multiple interfaces having same method can be inherited..
lik as in ur example, if both suzuki and ford had method body, wat to do??it gives error???
Then go for abstract classes.
Sir.. Can u please tel me exactly when to use abstract classes n when to use interfaces.. As far as my knowledge interfaces r used when there s frequent modification s involved n abstract classes r used when v know something in advance..
The + operator, in fact, internally converted into concat() method.
Sir, how can we say that java supports multiple inheritance through interfaces?
Can you explain in detail?B’coz there is no link between inheritance and interfaces, I’m bit confused in this.
Why not, there exists. Interfaces are inherited with implements keyword.
sir i want to know the flow in jvm can u pls explain me with diagrams
sir
can interface be implemented in newer version of C++ ie C++11
Can anybody tell me what is partial implementation of interface in java. I am very confused. I think we can implement the methods of our choice and let the others as it is. Can we do this? If yes, How?
Refer way2java.com with link: “http://way2java.com/oops-concepts/interfaces-partial-implementatin-of-multiple-inheritance/”