Interface
Interface is the pure abstract class. It is syntactically similar to classes but we can not create the instance of class. Basically it is used to achieve pure abstraction.
Interface is the pure abstract class. It is syntactically similar to classes but we can not create the instance of class. Basically it is used to achieve pure abstraction.
- all variables inside the interface are implicitly public,static and final variable.
- all methods inside the interface are implicitly public and abstract,even if you don't use public or abstract keyword.
- interface can extend on or more other interface.
- interface cannot implement a class.
- a class implements an interface.
Multiple inheritance in java using interface
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print()
{
System.out.println("Hello")
}
public void show()
{
System.out.println("Welcome");
}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
output: Hello
Welcome
Why the multiple inheritance is not supported in java through class
Multiple inheritance is not supported through class in java cause of ambiguity.
But it supports in case of interface because there is no ambiguity in this case.
Here implementation is provided by the subclass.
interface Printable{
void print();
}
interface Showable{
void print();
}
class testinterface1 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
testinterface1 obj = new testinterface1();
obj.print();
}
}
output: Hello
Multiple inheritance is not supported through class in java cause of ambiguity.
But it supports in case of interface because there is no ambiguity in this case.
Here implementation is provided by the subclass.
interface Printable{
void print();
}
interface Showable{
void print();
}
class testinterface1 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
testinterface1 obj = new testinterface1();
obj.print();
}
}
output: Hello
No comments:
Post a Comment