When designing classes in architecture, there are two things.
- The behavior of the object.
- implementation of objects.
If an object has more than one implementation, then separating the behavior of the object from its implementation is one of the keys for convenience and decoupling. Separation can be achieved either by an abstract class or by an interface, but which one is better? Let's look at an example.
Let's look at a development scenario in which things (query, class model, etc.) change very often, and you need to deliver certain versions of applications.
Initial statement of the problem : you need to create a class "Train" for the Indian railway, which has the behavior of maxSpeed in 1970.
1. Business modeling with an abstract class
V 0.0 (initial problem) Initial statement of the problem: you need to create the Train class for the Indian railway, which has maxSpeed behavior in 1970.
public abstract class Train { public int maxSpeed(); }
V 1.0 (Modified Problem 1) Modified Statement of the Problem: You need to create a Diesel Train class for the Indian Railways, which had maxSpeed behavior in 1975.
public abstract class DieselTrain extends train { public int maxFuelCapacity (); }
V 2.0 (change 2 problem ) chanded problem: you need to create the ElectricalTrain class for the Indian Railway, which has the behavior maxSpeed, maxVoltage in 1980.
public abstract class ElectricalTrain extends train { public int maxvoltage (); }
V 3.0 (issue with change 3)
chanded problem statement: you need to create a HybridTrain class (uses both a diesel and an electric car) for the Indian railway, which has the behavior maxSpeed, maxVoltage, maxVoltage in 1985.
public abstract class HybridTrain extends ElectricalTrain , DisealTrain { { Not possible in java } } {here Business modeling with abstract class fails}
2. Business modeling with an interface
Just change the word abstract to interface and ...... your business modeling with an interface will succeed.
http://javaqna.wordpress.com/2008/08/24/why-the-use-on-interfaces-instead-of-abstract-classes-is-encouraged-in-java-programming/