Mohammad Ashari Rahman wrote:What is the Difference between abstract class and interface?
What are the conceptual differences and why we need the two distinctions if methods in both need to be defined?
The main conceptual difference is that an interface only describes what a type does; while a class - any class - also describes at least part of how it does it (ie, implementation code).
If only part of the "how" is implemented - ie, some methods are left "empty" - then it must be an abstract class; otherwise it is normally a "concrete" class.
This division between "what" and "how" is a very important aspect of OO design, because it allows us to specify a type without having to worry about how it works, which can be extremely useful for dividing a complex program or application up into discrete "tasks".
For example, if I know that my class needs to contain a stack, then I can simply write a Stack interface - perhaps with push(Object), pop() and peek() methods (the "classic" requirements for a stack) - and hand it off to someone else to implement while I concentrate on what MY class has to do.
Or, if I don't have anyone else to help me, I can simply leave it as an interface until I'm ready to tackle it myself.
Furthermore, providing I only ever use Stack in my class, I can make use of any future improvements to the design by simply changing the place(s) where they are initialized, viz:
Stack s = new BasicStack(); becomes:
Stack s = new ImprovedStack(); and nothing else in my program needs to change.
It's probably worth mentioning that the two things aren't mutually exclusive: Some interfaces lend themselves to "skeleton implementations" that work for most people and take a lot of the drudge out of implementing the interface from scratch.
One such class is AbstractList, which is an abstract implementation of Java's List interface that allows you to create fully-functional List classes by implementing as few as 2 methods (List itself has 32 these days).
HIH, and I hope I haven't "overloaded" you.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.