Interface is a keyword in java and also a concept in java to over come the limitation of not supporting multiple inheritance by java. Now with the concept of interface we can have implement multiple interface full filing the concept of a " single class extending more than one class ". In the case of class we can extend only one but with the interface we can implement more than one interface.

Click here to know why java is not supporting multiple inheritance

Syntax to declare an interface :-
interface < interface_name > { }

Syntax to implement an interface :-
class < class_name > implements interface < interface_name > { }

Syntax to implement an interface :-
class < class_name > implements  interface < inter_nm1 >, < inter_nm2>......< inter_nmN > { }

Syntax to extending another interface :-
class < class_name > implements  interface extends < inter_nm1 >, < inter_nm2>......< inter_nmN > { }

Important point regarding interface in java

1. " interface " keyword is used to define any interface in java.

2. We have to use ' implement ' keyword for implementing an interface, extends cannot be used with interface. 

3. Possible to implement more than one interface with single class. 

4.  Interface are fully abstract class. So no need to make an interface as abstract as it is by-default " public abstract ".

5.  An interface can extends another interface but cannot extends/ implements another class. 

6. We cannot declare any instance member inside an interface as it is fully abstract. 

7. Variable declared inside an interface are by default - " public final static " or we can use these modifier explicitly also. 

8. We cannot declare a method as final and static in an interface.

9. We cannot use private , protected , transient and volatile keyword with the variable declared in an interface.

10. We cannot use synchronized and native with the method declared in an interface. 

11. Inner class in an interface are by-default " public static " .

12. We cannot define constructor, instance and static initialization block in an interface. 

13. By-default method declared in an interface are " public abstract " or we can use these keyword explicitly also. 

14. We cannot create an object of an interface as it is fully abstract class.  

15. " .class " file is generated by the compiler for every interfaces. 

Some of the important questions regarding interface with answer

Q1. Why we cannot define instance variable in an interface?
 --> Defining instance variable in an interface is restricted to save memory as because instance variable is inherited in its sub-class.

Now think if we have 3 interfaces(A, B and C)  and 1 class (D).
Interface B and C are extending A and Class D is implementing B and C. That means if declaring instance variable is allowed then for each instance variable as separate copy is also created in the interfaces B and C extending A and those copy are again copied to the class D. So we have many duplicate copies in the sub-classes.

For ex- Now if we have define 100 instance variable then it will create 100 copy in B and 100 in C and finally class D contain total of 100+ 100= 200 unnecessary copy which will obviously take memory as it an instance variable.
So to solve this problem declaring instance variable is restricted instead static is allowed which will create one memory.  

Q2. Why we cannot define variable defined in an interface as private and protected ?
        There are two reason behind it :-

  1. It is because by-default it is using public so we cannot define both public and protected to a single variable. 
  2. As interface doesn't have any instance method implementation and we need to access private member with the help of any method as because we cannot create an object of an interface and with cannot directly access with the sub-class object so need some method implementation which is not possible.   

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic