Sunday, January 17, 2021

Interfaces in Java - questions and answers

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

An Interface in Java programming is defined as an abstract type used to specify the behavior of a class. A Java interface contains static constants and abstract methods. A class can implement multiple interfaces. In Java, interfaces are declared using the interface keyword.

What is an interface in Java?

Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default. 

What is the use of interface in Java?

As mentioned above they are used for full abstraction. Since methods in interfaces do not have a body, they have to be implemented by the class before you can access them. The class that implements interface must implement all the methods of that interface. Also, java programming language does not allow you to extend more than one class, However you can implement more than one interface in your class.

What is the difference between an abstract class and an interface?

The key technical differences between an abstract class and an interface are: Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.

Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. 


Why is abstraction an important concept in software development
and what role do interfaces play in abstraction?

Abstraction is one of the key elements of good software design. It helps encapsulate behavior. It helps decouple software elements. When developing with higher level of abstraction, you communicate the behavior and less the implementation.

Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it. Using interfaces, you can achieve (complete) abstraction.

What must a class do in order to implement an interface?

To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.

What is an abstract method?

Abstract Class is a type of class in OOPs, that declare one or more abstract methods. These classes can have abstract methods as well as concrete methods. A normal class cannot have abstract methods. An abstract class is a class that contains at least one abstract method as defined below:

Abstract Method is a method that has just the method definition but does not contain implementation. A method without a body is known as an Abstract Method. It must be declared in an abstract class. The abstract method will never be final because the abstract class must implement all the abstract methods.

The main benefit of using an Abstraction in Programming is that it allows you to group several related classes as siblings. Abstraction in Object Oriented Programming helps to reduce the complexity of the design and implementation process of software.

Abstract methods are mostly declared where two or more subclasses are also doing the same thing in different ways through different implementations. It also extends the same Abstract class and offers different implementations of the abstract methods.

Can you instantiate an interface?

An interface can't be instantiated directly. Its members are implemented by any class or struct that implements the interface. A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.

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

No, you cannot have a constructor within an interface in Java. You can have only public, static, final variables and, public, abstract, methods as of Java7. From Java8 onwards interfaces allow default methods and static methods.

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

If the method signature you defined in interface has public access specifier then you have to override that method, with the same method signature (that means with public access specifier) whenever you implement the interface to any class.

No comments:

Post a Comment