Now we will see some code examples of inheriting in case of interfaces in Java:
interface Furniture{
//This is interface declaration and we will see more about that in next articles.
String table="";
//Now focus your attention to only inheritance concept.
String sofa="";
public void buy();
}
/*interface HomeFurniture implements Furniture{
The above line will cause the compiler error as one interface never implements another
interface but one interface only extends another interface;as show in following code:
*
}*/
interface HomeFurniture extends Furniture{
//HomeFurniture inherits assumes all the members of Furniture, along with its own members.
String bed="";
String dinigtable="";
public void showDemo();
}
interface OfficeFurniture extends HomeFurniture{
//HomeFurniture inherits assumes all the members of Furniture and HomeFurniture along with its own members.
String chair="";
String computerTable="";
public void showPrice();
}
Following class InheritanceInInterface implements (inherits/assumes) only one interface so it needs to implement the methods in Furniture interface only.
public class InheritanceInInterface implements Furniture{
public void buy(){
System.out.println("Buy method inherited from interface implemented.");
System.out.println("Buy the furniture at lowest rates!\n");
}
public static void main (String args[]) {
System.out.println("Creating the instance of Child1 class and calling the methods in it:");
Child1 ch1=new Child1();
ch1.printmsg();
ch1.buy();
//buy() method is associated with child class due to inheritance relationship
//between Child1 and InheritanceInInterface class.
System.out.println("Creating the instance of Child2 class and calling the methods in it:");
Child2 ch2=new Child2();
//buy() method is associated with child class due to intheritance relationship
between Child2 and InheritanceInInterface class.
ch2.showDemo();
ch2.showPrice();
ch2.buy();
}
}
So even if you are extending the only one interface HomeFurniture or OfficeFurniture you have to
implement all the methods up to the inheritance tree of interfaces and you can assume all the
members of interfaces up to the inheritance tree.
Follwoing class is not inheriting any interfaces but it is inherting the class
InheritanceInInterface so it does not need to add the implementation for methods in itnerfaces
as it is already implemented by child class but you can specify the child specific behaviour to
buy() method from 'Furniture' interface
class Child1 extends InheritanceInInterface{
public void printmsg(){
System.out.println("Method from child class which only inherits the class and that class is inheriting the interface.\n");
}
}
/*
* Following code proves that we can extend a class and we can implement one or more interfaces at the
* same time.
* */
class Child2 extends InheritanceInInterface implements HomeFurniture,OfficeFurniture{
public void showDemo(){
System.out.println("showDemo method from HomeFurniture implemented here.");
}
public void showPrice(){
System.out.println("showPrice method from OfficeFurniture implemented here.\n");
}
}
In case of Child2 class it is inheriting two interfaces so we have to implement all the methods in
those interfaces because neither Child2 class is abstract nor these methods in implemented
by parent of Child2 class.
0 comments:
Post a Comment