Interview Preparation: Interfaces

8  BRILLIANT ANSWERS TO SOFTWARE ENGINEERING INTERVIEW QUESTIONS! 👍






Q 1. What is an interface in Java and why would you, as a software developer, use interfaces?

a)     Interface in java

An Interface in the Java programming language is defined as an abstract type that is used to specify the behavior of the class. In java, interface is a blueprint of the behavior.

The Java interface contains static constants and abstract methods. The interface in a Java is the mechanism to achieve abstraction. There can be only the abstract methods in Java interface,

not a method body. It is used to achieve abstraction and multiple inheritance in java. In other words, we can say that the interfaces can have abstract methods and variables.

It cannot have the method body. Java Interface also represents the IS-A relationship. When we decide the type of entity by its behavior and not through the attribute, we should define it as the interface.

Like the class, interface can have the methods and variables, but the methods declared in the interface are by default abstract (only the method signature, no body).

Syntax

 interface{

// declare the constant fields

// declare the methods that are abstract

// by default. }

b) Why did software developer use an interface?

-        It is used to achieve total abstraction.

-        Since java doesn't support the multiple inheritances in the case of a class but by using an interface it can achieve the multiple inheritances,

-        and any class can extend only 1 class, but can any class implement an infinite number of interfaces.

-        It is also used to achieve loose coupling.

-        Interfaces are used to implement abstraction. So, the question arises why use the interfaces when we have abstract classes.

So, the reason is that abstract classes may contain the non-final variables, whereas the variables in the interface are final, public, and static.

Q 2. What is the difference between an abstract class and an interface?

The difference between abstract class and interface are given below: -

Abstract class

-        Abstract class can have the abstract and the non-abstract methods.

-        Abstract class doesn't support multiple inheritance.

-        Abstract class can have the final, non-final, the static and the non-static variables.

-        Abstract class can also provide the implementation of the interface.

-        The abstract keywords are used to declare abstract class.

-        An abstract class can extend to another Java class and implement multiple Java interfaces.

-        An abstract class can be extended using extends keyword.

-        A Java abstract class can have the class members like the private, protected, etc.

Example:

public abstract class draw Shape{

public abstract void draw();

}

 

Interface

- Interface can only have abstract methods. Since Java 8, it can also have the default and static methods.

- Interface supports multiple inheritance.

- Interface has only the static and final variables.

- Interface can't provide implementation of the abstract class.

- An interface can extend another Java interface only.

- An interface can be implemented using the “implements" keyword.

- The members of the Java interface are public by default.

Example:

public interface Draw{

void draw();

 

Q 3. Why is abstraction an important concept in software development and

What role do interfaces play in abstraction?

a)      By using the process of abstraction, the programmer/software developer can hide all but the relevant data about an object to reduce the complexity

and increase efficiency. In the same way that abstraction sometimes works in art, the object that remains is the representation of the original, with the unwanted detail omitted.

 

  b) what role do interfaces play in abstraction?

  An interface in Java is the specification of method prototypes. Whenever we need to guide the programmer/software developer or make the contract specifying how the methods

  and fields of the type should be we can define as an interface. To create an object of this type we need to implement this interface, provide the body for all the abstract methods of

  an interface and obtain the object of implementing the class. The user who wants to use the methods of an interface, they only know the classes that implement this interface and their methods,

  the information about implementation is completely hidden from the user, thus abstraction can be 100% achieved.

For example

interface Man{   

void display();

}

class Student implements Man{   

public void display() {     

system.out.println("This is the display method of Student class");   

} }

class Lecturer implements Man{   

public void display() {     

//it is used to print the output on the output screen.

system.out.println("This is the display method of Lecturer class");   

} }

public class Abstraction_Example{ 

public static void main(String[] args){     

Man Man1=new Student();     

Man1.display();     

Man Man2=new Lecturer();     

Man2.display();   

} }

Output

This is the display method of Student class

This is the display method of Lecturer class

 

Q 4. What must a class do to implement an interface?

What is interface first?

--> The interface is nothing but the blueprint of the class. It consists of all those things the class might contain. It is a way to save the time as well as to

achieve the abstractions in the class implementation in most of the Object-Oriented Programming languages. Interface never has the definition of the methods.

It only contains the declaration of the methods for the class.

 

For any class that might implement the interface. It should implement all the extract methods the interface consists of.

Consider we have the interface named

interface Car{

//string to represent the car name

Public string carName;

public double carSpeed;

//abstract method

public double getMilege();

}

Here the interface consists of some member variables and the methods abstract methods that do not have the definition for the method but just have the declaration for the method.

Hence to implement class from this interface we have to implement the methods the interface consists of.

class TestCar implements Car{

//Implement the method in the interface

public double getMilege(){

//Some code here.

}

}

This is the only thing required to implement the interface as a class.

 

Q 5. What is an abstract method?

The abstract method is something that does not require implementation. You can simply create the abstract method statement without implementing it for example.

public abstract int getMarks();

Here the getMatrks() method is abstract as i have provide the keyword abstract to it also it is the public method in the class.

Consider we have the class that is also the abstract class, hence the abstract class will have the abstract method definitions. If the abstract class is implemented later then

All the methods of this abstract class should be implemented else the another implemented class will also be considered as the abstract class.

Hence the implemented class should implement all the abstract methods in the abstract class. To be a non -abstract implemented class.

 

Q 6. Can you instantiate an interface?

An interface can't be instantiated directly. Its members are implemented by any class or structure that implements the interface.

They are interfaces and not just simple classes. The interface should be implemented to instantiate it.

Because the interface consists of the non-implemented methods inside it. These methods should be implemented to have the instantiation for them.

 

Q 7. Can you declare a constructor inside an interface? If not, why?

No, the interface cannot have constructors.

 

As we know that all the methods in interface are public abstract by default which means the method implementation cannot be provided in the interface itself. It must be provided by the implementation class. Let’s have a look at the following program:

interface SumInterface{

    public int mymethod(int num1, int num2);

}

public class SumClass implements SumInterface{

    public int mymethod(int num1, int num2){

           int op= num1+num2;

           return op;

    }

    public static void main(String args[])

    {

           SumClass obj= new SumClass();

           System.out.println(obj.mymethod(2, 3));

    }

}

As you can see we have implemented the method mymethod() in the class SumClass that implements the interface. So, there is no need to have interface object

(Interface cannot be instantiated in fact) to call the method as the implementation is in the class and class object can call this method.

Let’s come to the point now: All the methods of interface don’t have body so there is no need to call the methods in the interface itself.

To call any method, we need an object since there is no need to have object of interface, there is no need of having constructor in interface (Constructor is being called during creation of object).

 

Q 8. Can we override an interface method with visibility that is not public?

No, we cannot override an interface method if its visibility is not public. And if it has its visibility as public then you can override it with the same method signature

(i.e., with the same access specifier public) whenever you implement the interface to any class.

 

Comments