Posted by: Naveen Kapoor on: December 24, 2008
What is the difference between Interface and Abstract class or when to use Interfaces and when to use Abstract class ?
Different ppl give different reasons for the same and the major discussions on internet will give the basic differences or I can say the java specifications for this above discussions .
After going thru lot of discussions on internet what I got to know is………
We create an Abstract classes when we want the behaviour of that class to pass on to the child classes and we should create Interfaces in cases where we want the functionality to pass on the the child class.
Lets take an example of a room and lets suppose our home have
1) 4 Walls
2) 1 roof
3) 1 door
4 Windows ( depend on person to person need few ppl migh need only 1 window and few might need 2 and one or two may like to have 4 windows in there Home)
Now Above 3 specifications are constant for a home so we can set it as a permanent behaviour for the home and we can create an abstract class “General_Prop_Home” for the above behaviours. When ever our Home class will extends the General_Prop_Home all the behaviours from this call will get included in our Home class while we can consider 4th property as a functionality and different ppl can implement this functionailty as per there requirement.
Conclusion as per my analysis:
Abstract Classes ==> For Behaviour
Interfaces ==> For Functionalities