Sandeep Jindal
Sandeep Jindal

Reputation: 15368

Abstract class with all methods abstract - Practical Example

I am asking a very basic question and it may be marked duplicate (I could not find the answer though):

Is there any practical example of an Abstract Class with all the methods declared as Abstract?

In most cases and as mentioned in Java Tutorial also, class with all methods abstract shall be an interface.

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

Upvotes: 4

Views: 4154

Answers (9)

user3726565
user3726565

Reputation: 101

The biggest motive behind having Pure Abstract classes is to allow future extension. Assume you have an Abstract class (with all abstract members), then you inherit that abstract class in 20 derived classes. Sometime in future you wish to add a public method to 5 of your derived classes, what do you do ?


Since you already inherit the abstract class, an easier solution is to add the method (with implementation) to the abstract class. This way you don't have to touch any of the derived classes. Interfaces are very rigid in this context, once created there is very little chance to change an Interface, as it would require changing all the classes that implement that Interface.

Upvotes: 0

Ananth
Ananth

Reputation: 206

  1. Adding to the two answers given above, Interfaces can only have constants(Variables which are public,static and final) while there is no such restrictions for abstract classes.

  2. Abstract classes can have constructors which will be implicitly called when a child class is instantiated (if it is non-parameterised). But this is not possible with interfaces.

Here is an example for the usage of 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   
        }
    }
}

Upvotes: 3

Chandler Jia
Chandler Jia

Reputation: 1

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");
    }
}

Upvotes: 0

jahroy
jahroy

Reputation: 22692

The simplest practical example I can think of 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 can't do this with an interface.

Upvotes: 0

Sithum
Sithum

Reputation: 126

Adding more to the below answers:

Interface provide you with a contract to implement where abstract Class may provide you with a template as well. For a simple scenario you can use an Interface or an abstract Class without thinking much. But having an abstract class just for maintaining a state might give you lot of problems in a complex implementation. In such cases you have to carefully consider what you really want to achieve in your code and make the decision. If you consider the case of maintaining the state in your code, you can always use the State pattern in your implementation, so you will be able to use an interface in your code. You should always consider the extend-ability and maintainability of your code before deciding to use an abstract class over interface.

Upvotes: 0

Eric Jablow
Eric Jablow

Reputation: 7899

An interface can be applied to wildly different classes. Classes that have no relation to each other are Serializable or Cloneable. However, subclasses of an abstract class are all related. This may not mean anything when implementing the interface or extending the abstract class, but it means something semantically.

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

Upvotes: 0

nachokk
nachokk

Reputation: 14413

The only practical approach i think is that Abstract class can hold state. So you can have inside properties with access level protected, and you can make protected abstract methods that in interface you can't cause all are public.

A practical example could be for example this, the 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 downside of using abstract class is that you loss the possibility to extends of something else too.

Upvotes: 5

sathish_at_madison
sathish_at_madison

Reputation: 833

One other general purpose of an abstract class is to prevent an instance of the class., for example

 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 effectively make sure that there will never be an object of instance Mammal.

Upvotes: 0

Paul Bellora
Paul Bellora

Reputation: 55223

As well as for holding state, it's worth remembering that all interface members are implicitly public. So restricting visibility of abstract methods may itself be a compelling enough reason to use an abstract class instead of an interface.

Upvotes: 4

Related Questions