We have already seen the public access modifiers such as private, public, protected and default in an earlier post. We have also seen the ‘final’ and ‘static’ modifiers in the ‘Other Java modifiers‘ post. Let us see the ‘abstract’ modifier in this post:
The ‘abstract’ modifier may sound ‘abstract’ but once we understand it, it is easy as well! 🙂
The ‘abstract’ modifier has a lot of Do’s and Dont’s associated with it.
Let us see some of the rules associated with the ‘abstract’ keyword:
- The ‘abstract’ keyword can only be used with classes and methods.
- If a class has methods which do not have implementations defined for them, it should be defined as ‘abstract’.
- Abstract classes ensure that implementations can be done by the respective sub-classes
- The abstract class cannot be instantiated
- Abstract classes can only be extended
- If an abstract class is extended, it must implement all the methods defined in the abstract super class or it should declare itself as ‘abstract’.
Now, that is a lot of abstract talk…:) let us see a simple program to see it in action:
We can see three classes in this program – a public class, an abstract class and a class that extends the abstract class. Change the code, play and see how it works!! 🙂
Bonus question for Java Geeks!! 🙂 (This question is not my own!!)
Which of the following is true?
- An abstract class cannot have final methods
- A final class cannot have abstract methods
Comment your answer below…if you want you can refer to the other chapters as well…:)
Come back again tomorrow for another post… 🙂
I’m participating in #BlogchatterA2Z by BlogchatterÂ