What to choose between abstract class and interface if…

What to choose between abstract class and interface if…

You know that interface contains only abstract methods. But, consider the restriction that abstract class has only abstract methods only, then what would be your choice and why? Here is an example.

No alt text provided for this image

Answer:

Best choice is an interface for two reasons given below.

1)Missing a chance of multiple inheritance

Since multiple inheritance is not supported in java, you cannot inherit your class from two abstract classes or one abstract and other concrete class. But, if you use interface the still you have a chance to extend a class.

For given interface and classes below, you cannot extend two classes but interface

No alt text provided for this image

class C extends A implements X{}//OK, on class and multiple interfaces can be extended and implemented.

class C extends A ,B{} // wrong, two classes cannot be extended.

?

2) User / Programmer intent:

Depending what you choose between abstract class and interface to provide contracts, your contracts can be easily violated by users or programmer’s intent.

Let’s consider a scenario.

Let’s say you are a client and pass the?contracts in an interface?to implement them to development team for multiple types of documents e.g. pdf, rtf and word etc. Constricts example is below, that team need to implement header, body and footer.

No alt text provided for this image

?All development team understand easily that they must just implement and do nothing extra. this is what you want as a result.

?

Now you pass these?contracts in an abstract class as below.

?

abstract class Doc{
	abstract void header();
	abstract void body();
	abstract void footer();
}        

Programmers from team get intention that they are free to implement another interface besides given one in abstract class.

Also, they can write their own methods (private or public) into the abstract class, if they think it is required during implementation of sub classes i.e. pdf, rtf and word etc.?This is not the client want.

Reasons to use Abstract Classes in Java

  • You can offer default functionality with the help of abstract classes.
  • You can specify a particular template for future specific classes.
  • Abstract classes allow you to reuse code.
  • With the help of abstract classes you can define a common interface for your subclasses.

Reasons to use Interfaces in Java

  • You can achieve 100% abstraction with the help of interfaces in Java
  • You can resolve methods dynamically at runtime with the help of interfaces.
  • You can separate the definition of a method from its inheritance hierarchy with the help of interfaces.
  • You can also achieve loose coupling with the help of interfaces.

When should you use Java Interfaces?

You should use interfaces when:

  • You want 100 percent abstraction
  • You want to implement multiple inheritance in Java
  • You are more concerned with the behavior of the class rather than who is implementing the behavior.
  • You want a child class to implement all abstract methods in the interface.

When should you use Java Abstract Classes?

You should use abstract classes when:

  • Your application has some code that needs to be shared amongst different classes. An abstract class would contain all the abstract methods. The child classes can redefine each of these abstract methods separately.
  • There is a need to use non-static and non-final values inside a class so that an object can modify these variables.
  • You need to have private or protected methods and variables. Abstract Classes can be useful for containing protected and private methods as well as variables.




Mehtab Ansari

Senior Full-Stack Java Developer | J2EE | SpringBoot | React Js | AWS

3 年

Interface cannot implement several Interfaces but extend or you mean to say Class

Mohamed Ibrahim ????

Android Engineer | Kotlin Enthusiast

3 年

How Abstract class is faster than interface? What do you mean by fast? Also in modern java, interface could have fields.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了