- Computers & Software»
- Computer How-Tos & Tutorials
why do we need abstract classes and interfaces in java and what is the use of abstract classes and interfaces
Abstract class in java definitioin
As you all know that abstract class is a one of the important concept in all object oriented programming.So java also have abstract class concept.So What is abstract class?Why do we use abstract class in our normal programming? and How to use abstract classes?.For all these questions this hub gives you the answers.
We can say abstract definition in two ways,
Way 1 - In General or Real time:
In general we can say that abstract class does not have any real time object.You can have question, if there is no object for class , then why do we need the abstract classes.I will give answer to this question by below example class hierarchy.See the below image.
Abstract class real time example
Abstract class realtime example explanation
You can see the above image is for example for Abstract class in real time.Take three classes Human, Man and Woman.All Man and Woman belong to Human category.In Original object exists for only for Man or Woman.NO object exist for Human category.But both Man and Woman have common characteristics where we can say it as Human characters.Now you can understand that why do we need abstract class.
Difference between Abstract class and interface
Now As you know that abstract class where we can not create object for abstract class.Same like abstract class, you can not create object for interface also.Then what is the difference between abstract class and interfaces in java.I can say that interface is for "PURE ABSTRACTION".Pure abstraction means you can not have concrete methods.But an abstract class can have concrete methods.Interfaces should not have any concrete methods, it should only have method declarations.
Which one is advantage over another:
Interface has advantage over Abstract class.Since you can not use abstract class for already inherited class.But you can use interface for already inherited class.