Interface is one of the general concepts of Java but there are some tricky questions based on it:
1) One interface class can be extended by another interface class. It is a wrong perception of lot of people that interface can only be implemented.
2) When an interface I1 is extended by another interface I2 and I2 is implemented in class C1 , then all the methods of interface I1 and I2 has to be implemented in class C1. This is best used in the scenario when there are two methods of same name and different signature in I1 and I2 because JAVA doesn't allows a class directly to implement two interfaces having same method name and different signature type.
3) When an interface I1 is extended by another interface I2 and we try to implement I1 in class C1 , then all the methods of interface I1 has to be implemented in class C1. No need to implement methods of I2 interface.
4) As of now we were knowing that Interface does not have defined methods and it contains only declared methods. In Java 8 we have option to give method body in an Interface. It can be done in two ways :
-> a) By defining the method as Default - It can be called by implementing in child object and creating object of child class
-> b) By defining the method as Static - It can be called directly by class name.
5) If we want to implement only few of the methods of an interface and leave the remaining ones then we have to create an abstract class for implementing the partial methods of interface.
6) Multiple inheritance is not supported by java directly but it can be done by interfaces.
7) All the variables in interface are static constants
8) All the methods in interface are abstract and public
9) Java doesn't allows a class to implement two interfaces that have methods of same name but different signature
10) The interface variable has to be initialized otherwise compiler will throw an error.
11) We can't create object of interface class
1) One interface class can be extended by another interface class. It is a wrong perception of lot of people that interface can only be implemented.
2) When an interface I1 is extended by another interface I2 and I2 is implemented in class C1 , then all the methods of interface I1 and I2 has to be implemented in class C1. This is best used in the scenario when there are two methods of same name and different signature in I1 and I2 because JAVA doesn't allows a class directly to implement two interfaces having same method name and different signature type.
3) When an interface I1 is extended by another interface I2 and we try to implement I1 in class C1 , then all the methods of interface I1 has to be implemented in class C1. No need to implement methods of I2 interface.
4) As of now we were knowing that Interface does not have defined methods and it contains only declared methods. In Java 8 we have option to give method body in an Interface. It can be done in two ways :
-> a) By defining the method as Default - It can be called by implementing in child object and creating object of child class
-> b) By defining the method as Static - It can be called directly by class name.
5) If we want to implement only few of the methods of an interface and leave the remaining ones then we have to create an abstract class for implementing the partial methods of interface.
6) Multiple inheritance is not supported by java directly but it can be done by interfaces.
7) All the variables in interface are static constants
8) All the methods in interface are abstract and public
9) Java doesn't allows a class to implement two interfaces that have methods of same name but different signature
10) The interface variable has to be initialized otherwise compiler will throw an error.
11) We can't create object of interface class
No comments:
Post a Comment