Java Interface and abstract class related interview questions
Tags: java, oops
Published: 2022-07-18 10:18:00

## 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

```java
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();
}
```
**Output**: _No errors_
```
This is main class
```
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`.

```java

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();
}
```
**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
```

## 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

```java
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();
}
```

**Output**: 

```
Hello, World!
```
**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**:

```java
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");
    }
}
```
**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

```
**When return types are different**:

```java
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";
    }
}
```
**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
```