Possible Duplicate:
Abstract class in Java
I got several explanations but so far I'm not able to understand that what are the abstract classes and methods in Java.
Some said it has to do something with the security of the program, other said it isn't anything like that.
Even from the Dietel & Dietel's book I don't get it's purpose. When,where and why do we use it?
Kindly explain it like you're teaching a beginner, I would really appreciate your help.
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body).
Yes, we can declare an abstract class with no abstract methods in Java. An abstract class means that hiding the implementation and showing the function definition to the user. An abstract class having both abstract methods and non-abstract methods. For an abstract class, we are not able to create an object directly.
Abstract class can have both an abstract as well as concrete methods. A concrete class can only have concrete methods. Even a single abstract method makes the class abstract.
- Abstract class
is one which can't be instantiated, i.e. its object
cannot be created.
- Abstract method
are method's declaration without its definition.
- A Non-abstract class
can only have Non-abstract methods.
- An Abstract class
can have both the Non-abstract as well as Abstract methods.
- If the Class
has an Abstract method
then the class must also be Abstract.
- An Abstract method
must be implemented by the very first Non-Abstract sub-class.
- Abstract class
in Design patterns
are used to encapsulate the behaviors that keeps changing.
An abstract class is a class that can't be instantiated. It's only purpose is for other classes to extend.
Abstract methods are methods in the abstract class (have to be declared abstract) which means the extending concrete class must override them as they have no body.
The main purpose of an abstract class is if you have common code to use in sub classes but the abstract class should not have instances of its own.
You can read more about it here: Abstract Methods and Classes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With