Java Interfaces



1. What is interface in Java?

Answer:



In Java interface is like a class only but its all methods are public and abstract. and its all variables are public static and final.

As its all methods are abstract so using abstract keyword in method declaration is optional. Similarly all variables are final and static so these keywords are optional in variable declaration.

If you don't use these keyword in your interface then upon compilation JVM will automatically will add these keywords in the compiled code. So e.g. if you write following interface:
package com.meritmix.blog.java;
/**
 * @author Ravi Yadav
 * blog address: www.meritmix.com 
 */
public interface InterfaceA {
int a = 10;
void move(int i);
}


then after compilation it will become like this
package com.meritmix.blog.java;
/**
 * @author Ravi Yadav
 * blog address: www.meritmix.com 
 */
public interface InterfaceA {
public static final int a = 10;
public abstract void move(int i);
}



2. Can we declare variable in interface?

Answer:

  • Yes we can declare variables in interface.
  • In interface variables are by default public , static& final.
  • You cannot create an instance of an interface
  • As variables are final so all the variables of the interface must be initialized at the time of declaration.


3. Why are methods of Interface public and abstract by default?

Answer:

  • Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.
  • If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
  • Methods of an Interface has to be public because if they are not public then all the implementing classes have to be in same package otherwise they could not be accessible (would not be visible) to classes in other packages.
  • Methods are abstract because they are not having method body (code inside {} braces).


4. Can you implement multiple inheritance in java?

Answer:

  • Yes, Multiple Inheritance is allows in Interface, though in Classes multiple Inheritance is not allowed in Java
  • One Interface can 'extends' one or more Interfaces, one Interface doesn't 'implement' another Interface.
  • If an Interface(A) is extending two interfaces (B and C) and both B and C have same method (e.g. public void draw()), then any class implementing Interface A have method draw() only once.


  • If a Class is implementing two interfaces which have same method but their return types are different then that calass will not compile and give compile time error 'The return type is incompatible with Interface1.add(int, int)'.


5. What is meant by implementation in java?

Answer:

Implementation in java means you are writing body/definition of a method in a Class. And method could be the abstract method of an abstract Parent Class or a method of an Interface which is being implemented by this Class.

6. What is the difference between a class and an interface?

Answer:

  • A class has all its methods implemented( though Abstract class may have one or more abstract methods) while all methods of an Interface are abstract.
  • A Class can have public/protected/private and default/friendly method while all the methods of an Interface are public.
  • A Class can have one or more constructors while Interfaces don't have any constructor.
  • Classes can have non-static, non-final variables while all the variables of an Interface are final and static.
  • You can create instance/object a Class (though not of an abstract class) but you cannot create instance/object of an Interface.


7. Can an interface have attributes?

Answer:

  • Yes we can declare attributes/variables in interface.
  • In interface attributes/variables are by default public , static& final.


8. Can an interface have data members?

Answer:

Here Data members mean instance level (non static) variables, so answer is NO, as all the variables in Interfaces are public , static and final.

11. Can you define methods in an interface?

Answer:

No, you can only declare methods in Interface.

Since Java 8, you can define default and static methods in Interfaces.

12. Can an interface have implemented methods?

Answer:

No, you can only declare methods in Interface.

Since Java 8, you can define default and static methods in Interfaces.

No comments:

Post a Comment