Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

situation where interface is better than abstract class [duplicate]

Tags:

java

oop

Please tell me situation where interface is better than abstract class in Java

like image 344
JavaUser Avatar asked Jun 04 '10 03:06

JavaUser


People also ask

What is advantage of using interfaces over abstract classes?

The main advantages of interface over abstract class is to overcome the occurrence of diamond problem and achieve multiple inheritance. In java there is no solution provided for diamond problem using classes. For this reason multiple inheritance is block using classes in java.

Are interfaces preferred over abstract classes?

Anyway, it depends on what you are trying to create. Abstract classes are preferred if the subclasses share some common functionalities with the superclass. Interfaces when you create unrelated classes to implement the interface. Interfaces allow you to implement multiple inheritances.

Which is faster interface or abstract class?

The fourth difference between abstract class and interface in Java is that abstract classes are slightly faster than the interface because the interface involves a search before calling any overridden method in Java.

Why interface is used instead of multiple inheritance?

As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.


2 Answers

I think you have misunderstood the real meaning of interface and abstract class.

Interface is programming structure where you define your functions/services that you want to expose to public or other modules. Kind of a contract where you promise that you are providing some functionalities or services, but hiding the implementation so that implementation can be changed without affecting your contract.

Abstract class is a partially implemented class and it has no real meaning other than serving as a parent for multiple child classes those with real meaning. Abstract class is special parent class that provides default functionalities to multiple child classes. it is created as abstract because of unavailability of suitable concrete parent class.

In a good design, you should always create an interface. But abstract class is optional. If you can not find a concrete parent class, create an abstract class and implement the interface then provide default implementations for those interface functions (if possible) otherwise mark them as abstract functions and leave the implementation to the child classes.

like image 78
Sujee Avatar answered Nov 14 '22 21:11

Sujee


  • A class can implement multiple interfaces, but it can only extend one abstract class.
  • Interfaces allow the creation of proxies that encapsulate a concrete class. This is used extensively by frameworks in order to intercept method calls to the concrete class (e.g., for starting a transaction before the method is executed or to write to the log).
like image 34
Artefacto Avatar answered Nov 14 '22 22:11

Artefacto