Possible Duplicate:
Use of Java [Interfaces / Abstract classes]
Why we use interface and abtract class and in which situation one should use abstract class and interface ?? can anyone explain this with simplest example .....
Possible Duplicate:
Use of Java [Interfaces / Abstract classes]
Why we use interface and abtract class and in which situation one should use abstract class and interface ?? can anyone explain this with simplest example .....
You want to use an Interface when you want all your sub-types to implement different specific logic, like in this case:
interface GeometricalShape{
# only abstract methods are allowed
GetArea();
GetPerimeter();
}
In this case each geometric shape will have to implement specific logic to calculate Area and Permimeter.
You want to use an Abstract Class when although your sub-types have to implement some specific logic, you still have logic common to all sub-types like in this case:
abstract class GeometricalShape{
name;edgeCount;
protected GeometricalShape(name,edgeCount)
#abstract methods
abstract GetArea();
abstract GetPerimeter();
#Concrete methods
GetName(){return name;}
GetEdgesCount(){return edgesCount}
In this example each geometric shape will have to implement specific logic regarding Area and Perimeter like before, but now all sub-types will share common methods for retrieving the name and edge count of the geometrical, which would be redundant to be defined in each and every sub-class.