An abstract class with all abstract methods - A practical example

I ask a very simple question, and it can be marked as a duplicate (I could not find the answer though):

Is there any practical example of an abstract class with all methods declared as Abstract?

In most cases, and as mentioned in the Java tutorial, a class with all abstract methods is an interface.

But since an abstract class and an interface are two different concepts, I am looking for an example to have a "full abstract class"

+4
source share
9 answers

The only practical approach that I believe is that the Abstract class can contain state. Thus, you can have internal properties with the protected access level, and you can make protected abstract methods that in the interface you cannot call all public .

A practical example might be, for example, this, a protected method in java has “inheritance access” and “package access”.

 public interface Operation{ void operate(); } public abstract class AbstractClase implements Operation{ protected Operation delegate; public AbstractClase(Operation delegate){ this.delegate=delegate; } //delegate implementation responsability to children protected abstract doSomething(); } 

The disadvantage of using an abstract class is that you lose the ability to extend something else.

+5
source

As with the hold state, it is worth remembering that all interface members are implicitly public . Thus, limiting the visibility of abstract methods in itself can be a convincing basis for using an abstract class instead of an interface.

+4
source
  • Adding to the two answers above, interfaces can only have constants (variables that are public, static, and finite), while there are no such restrictions for abstract classes.

  • Abstract classes can have constructors that will be implicitly called when an instance of the child class is created (if it is not parameterized). But this is not possible with interfaces.

Here is an example using an abstract class

 abstract class Animal{ public int noOfLegs; public boolean isAlive; Animal(){ isAlive = true; } public abstract void walk(); } class Cow extends Animal{ Cow(){ noOfLegs = 4; } public void walk(){ if(isAlive){ //Code for walking } } } 
+3
source

Another common goal of an abstract class is to prevent an instance of the class. For instance,

  abstract class Mammal{ int i=0; } public class Man extends Mammal{ public setMeValue(int i){ this.i=i; } public static void main(String args[]){ Mammal m= new Man(); man.setMeValue(10); } } 

In the above code, I convincingly made sure that there would never be a Mammal instance object.

0
source

The interface can be applied to completely different classes. Classes that are not related to each other are Serializable or Cloneable . However, subclasses of an abstract class are interconnected. It may mean nothing when implementing an interface or extending an abstract class, but it means something semantically.

There is a programming style where all methods of the base class are either public final , protected abstract , protected and empty, or private . But even this is not what the OP was interested in.

0
source

The simplest practical example I can imagine is a class that has a protected variable:

 public abstract class RoadVehicle { protected int numberOfTires; protected String vinNumber; protected VehicleRegistration registration; public abstract void drive(); public abstract double calculateToll(); public abstract void changeTires(); // so on and so forth... } 

You cannot do this using the interface.

0
source

Add more to the answers below:

The interface provides you with a contract for implementation, where an abstract class can also provide you with a template. For a simple scenario, you can use an interface or an abstract class without thinking. But having an abstract class just to maintain state can give you a ton of problems in a complex implementation. In such cases, you should carefully consider what you really want to achieve in your code and make a decision. If you are considering the case of saving state in your code, you can always use the state template in your implementation so that you can use the interface in your code. You should always consider the expandability and maintainability of your code before deciding whether to use an abstract class over an interface.

0
source
 public abstract class animal{ public abstract void speak(){ System.out.println("animal voice"); } } public class dog extends animal{ public void speak(){ System.out.println("dog voice"); } } 
0
source

The biggest motive is that Pure abstract classes are a further extension. Suppose you have an abstract class (with all abstract members), then you inherit this abstract class in 20 derived classes. Sometime in the future you want to add a public method to your 5 derived classes, what are you doing?


Since you already inherit an abstract class, the easiest way is to add a method (with implementation) to the abstract class. Thus, you do not need to touch any derived classes. Interfaces are very rigid in this context, after creating, there is very little chance of changing the interface, since this will require changing all the classes that implement this interface.

0
source

Source: https://habr.com/ru/post/1486731/


All Articles