Is it good practice to use abstract classes in polymorphism in Java?

In all the examples, I saw that interfaces are used to achieve polymorphism. Now we have the following code with an abstract class

AbstractClass parent = new Child(); 

Here the man stated that

The general argument is that polymorphism applies only to interfaces and not abstract classes.

I think he meant that they are usually the interfaces that are used in polymorphism in Java. As I can see, many people found their question stupid and wanted a URL. This is what I found. So my question is, is it good practice to propagate abstract classes in polymorphism (as in my example - because polymorphism is a very broad definition) in Java?

+7
java polymorphism oop abstract-class
source share
7 answers

It is good practice to use the most common parent that matches the contract; if the interface defines all the function signatures you need, then use them, not abstract classes that implement this interface. The article Design Principles from Bill Wenner's design patterns is discussed in detail with Erich Gamma.

+3
source share

One of the best ways to use it is that you have common behavior between "childs".

Your interface

 interface Drawable{ void paint(); } 

Abstract class with common code

 abstract class AbstractRectangularObject implements Drawable{ public void paint(){ paintVertices(); //your code to fill body } //Abstract method that all subclases needs to implement protected abstract void paintVertices(); } 

Real subclasses

 class Rectangle extends AbstractRectangularObject { protected void paintVertices(){ //code to pain vertices } } 

-

 class RoundRectangle extends AbstractRectangularObject { protected void paintVertices(){ //code to pain vertices } } 
+1
source share

If you have common functionality and properties for sharing between child classes, and at the same time the class itself is too abstract to have an instance, it is good practice to use an abstract class. If not, I prefer to use interfaces.

0
source share

In general, yes. It is always useful to use the least specific type. This also applies to specific superclasses, not just abstract classes and interfaces.

 public class MyClass{} // not an interface and not abstract public class SubClass extends MyClass{} public class OtherClass{ public MyClass getMyClass(){ return new SubClass(); } ] 

In practice, it depends on the situation. If everything is included in the scope of one method, it really does not matter.

 public void doStuff(){ // void method, so never going return any details AbstractFoo foo1= new ConcreteFoo(); // no better than ConcreteFoo foo2 = new ConcreteFoo(); // because nothing external to this method will ever know } 

However, the reason developers always use the least specific implementation (or interface) is simply a habit.

0
source share

The answer depends on your context.

Example:

  // Abstract class template abstract class AbstractFirst { public void doSomething(){ this.doOne(); this.doSecond(); System.out.println(""); System.out.println("Test"); } public abstract void doOne(); public abstract void doSecond(); } // Concrete implementation class ConcreteFirst extends AbstractFirst { public void doOne(){System.out.print("One"); } // must be implemented public void doSecond(){System.out.print("Second"); } // must be implemented public static void main(String[] args){ ConcreteFirst cf = new ConcreteFirst(); c.doSomething(); } } 

Will print

 OneSecond Test 

for the console. This is not possible with interfaces. This template is called the "Template Method Template" and is one of the GoF templates.

0
source share

As for your question, there is no opinion that says that it is bad practice to use polymorphism, it is good practice to use it where it is applied, but it depends on where you define the requirements for classes to be clean virtual aka interfaces.

0
source share

As for my experience, an abstract class always contains a partial implementation of some interface. For example, this applies to EJB. Its best design is to support the API and its implementation separately, even partially. Therefore, Id recommends creating an interface and an abstract class. But for links, use an interface, not an abstract class.

0
source share

All Articles