DEV Community

Prashant Mishra
Prashant Mishra

Posted on • Updated on

Java Interface and abstract class related interview questions

Abstract class

  • Can Abstract class have contructor() ?

Yes abstarct classses can have constructors. Even if you don't define JVM will create one default constructor for the abstract class.

  • What is the need of constructor in abstract class ?

The main purpose of the constructor is to initialize the newly created object. In abstract class, we have an instance variable, abstract methods, and non-abstract methods. We need to initialize the non-abstract methods and instance variables, therefore abstract classes have a constructor

  • How costructors will be called in an abstract method as we can't create object of abstract class ?

It's called with the help of costructor chaining i.e. when we create the object of sub-class, constructor of abstract class will also be called.

  • What will happen if abstract class implements an interface ?

Abstract class can miss the implementation of one or more methods defined in the interface and it won't through any errors.
This is because abstract class by definition is required to be extended by sub-classes and when a subclass is extending the abstract class then we can probvide the implementation of all the abstract methods

class Main {
    public static void main(String[] args) {
        System.out.println("This is main class"); 
    }
}

abstract class Bird implements Ifly{

}
interface Ifly{
    public void fly();
}
Enter fullscreen mode Exit fullscreen mode

Output: No errors

This is main class
Enter fullscreen mode Exit fullscreen mode

Note as soon as the abstract method Bird is getting extended then it is required to implement all the abstract methods including the methods of the interface Ifly.


class Main  extends Bird{
    public static void main(String[] args) {
        System.out.println("This is main class"); 
    }
}

abstract class Bird implements Ifly{

}
interface Ifly{
    public void fly();
}
Enter fullscreen mode Exit fullscreen mode

Output: with error as we are not implementing the abstract method fly()

Main.java:4: error: Main is not abstract and does not override abstract method fly() in Ifly
class Main  extends Bird{
^
1 error
Enter fullscreen mode Exit fullscreen mode

Interface

  • If there are two interfaces having same method what will happen if both of them are implemented by a class ?

If the methods are abstract and method signatures are same (including return type) then the implementing class will have to override only one method and there won't be any issues

class HelloWorld implements Face,Anatomy {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
        HelloWorld w  = new HelloWorld();
        w.getName();
    }
    @Override
    public void getName(){}
}
interface Face{
    void getName();
}
interface Anatomy {
    void getName();
}
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Note:
But if return types is different in both the methods then implementing class will have to override both the methods.

Note:
If there are default method with same name in both the interfaces then it will give you compile time error

When return types are same:

class Main implements Face,Anatomy {
    public static void main(String[] args) {
    }
    @Override
    public void getName(){}
}
interface Face{
    void getName();
    default void getFaceOrientation(){
        System.out.println("the default face shape fo Face interface");
    }
}
interface Anatomy {
    void getName();
     default void getFaceOrientation(){
        System.out.println("the default face shape of Anatomy interface");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Main.java:4: error: types Face and Anatomy are incompatible;
class Main implements Face,Anatomy {
^
  class Main inherits unrelated defaults for getFaceOrientation() from types Face and Anatomy
1 error

Enter fullscreen mode Exit fullscreen mode

When return types are different:

class Main implements Face,Anatomy {
    public static void main(String[] args) {
    }
    @Override
    public void getName(){}
}
interface Face{
    void getName();
    default void getFaceOrientation(){
        System.out.println("the default face shape fo Face interface");
    }
}
interface Anatomy {
    void getName();
     default String getFaceOrientation(){
        return "the default face shape of Anatomy interface";
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Main.java:4: error: types Anatomy and Face are incompatible;
class Main implements Face,Anatomy {
^
  both define getFaceOrientation(), but with unrelated return types
1 error
Enter fullscreen mode Exit fullscreen mode

Top comments (0)