Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 958
Sandeep Jindal Avatar asked Dec 15 '22 09:12

Sandeep Jindal


1 Answers

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.

like image 170
nachokk Avatar answered Jan 18 '23 23:01

nachokk