Java Interface interview questions and answers

Interface Interview Questions In Java.


Interview questions on Java Interface

Question 1. Super class of all classes is java.lang.Object class, does this applies to Interface as well? What is super class of all Interface in Java?

java.lang.Object class is the super class of all Java classes. All non-primitive types (including arrays) inherit either directly or indirectly from Object class. 

For Interface that is not the case, Super class of Interface is null.


From below program that will be clear.
interface ITest{}
class CTest{}

class Test{ 
 public static void main(String[] args) {
  System.out.println(CTest.class.getSuperclass()); // class java.lang.Object
  System.out.println(int[].class.getSuperclass()); // class java.lang.Object
  System.out.println(ITest.class.getSuperclass()); // null
  System.out.println(int.class.getSuperclass());   // null
 }
}
Output:
 class java.lang.Object
 class java.lang.Object
 null
 null

    Question 2. Can Interface access methods of Object class? What is the output of below program? Will it compile and run properly?

    java.lang.Object class is the super class of all Java classes. All non-primitive types (including arrays) inherit either directly or indirectly from Object class. 
    For Interface that is not the case, Super class of Interface is null.
    interface ITest{
     String abc();
    }
    class CTest implements ITest{
     @Override
     public String abc() {
      return "hello";
     }
    }
    
    class Test{ 
     public static void main(String[] args) {
      ITest i = new CTest();
      System.out.println(i.abc());
      System.out.println(i.toString());
     }
    }
    
    Output:
    hello
    com.javabypatel.sample.test.dto.CTest@4e44ac6a
     

    Call to i.abc() is fine but call to i.toString() method should work or should not work as 
    toString() method is not declared in interface ITest.

    Yes. It should work because all the public methods of Object class are accessible through interface object.

    Note: Interface doesn't extends Object class by default.

    Every Interface (that doesn't explicitly extend another interface) has an implicit method declaration for each public method in Object class thats is why you can see all the Object class methods accessible on "i" in above program.

    Details:
    If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.


    Question 3. What access specifier are allowed for methods in Interface?

    Only public and abstract are allowed as access specifier for methods. Also if method is not declared any access specifier then public and abstract are by default added to methods declared in interface.
    interface ITest{
     protected String abc(); // Compile Error
    }
    


    Question 4. What access specifier are allowed for variables in Interface?

    Only public, static and final are allowed for variables declared in interface. Also if variable is not declared with any access specifier then by default public, static and final is added to variables declared in interface.
    interface ITest{
     protected int a; // Compile Error
    }
    


    Question 5. What is the output of below program? Is it valid overriding of method test() of interface ITest?

    interface ITest{
     Object test(Object o) throws IOException;
    }
    
    class CTest implements ITest{
     @Override
     public synchronized String test(Object o) throws IOException, FileNotFoundException, EOFException, 
                StackOverflowError, IndexOutOfBoundsException{
      return "hello";
     }
    }
    
    Yes. It is perfectly valid method overriding. 
    1. Access specifier of method cannot be changed which is public in class CTest and is valid.
    2. Adding or removing synchronized keyword doesn't take part in overriding as it is upto implementer whether they want several threads to allow method execution simultaneously or one after another. So it is valid.
    3. Return type should be same or compatible(belong from hierarchy). String(all class) by default extends Object, so belongs from hierarchy and is valid.
    4. Name of method should be exactly same and it is valid.
    5. Parameter should be exactly same and it is valid.
      Note: Compatible parameters that is parameter from same hierarchy tree is Invalid and it should be exactly same.(In our case, decaring parameter as String for test() in CTest is invalid).
    6. For Exception, If overriding method choose not to throw any Exception is perfectly valid even if overriden method in interface is throwing.
      So in our case if overriding test() method choose not to throw any Exception then also it is perfectly valid.
      If it throws then it should be same or any number of compatible exceptions.
      Runtime exception will not take part in overriding, it is upto overriding method to throw or not to throw Runtime exception.

    You may also like to see


    Exception Handling Interview Question-Answer

    Method Overloading - Method Hiding Interview Question-Answer

    Advanced Multithreading Interview Questions-Answers In Java

    Type Casting Interview Questions-Answers In Java

    How Thread.join() in Java works internally

    How is ambiguous overloaded method call resolved in java

    Enjoy !!!! 

    If you find any issue in post or face any error while implementing, Please comment.

    Post a Comment