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:

  1. The ‘abstract’ keyword can only be used with classes and methods.
  2. If a class has methods which do not have implementations defined for them, it should be defined as ‘abstract’.
  3. Abstract classes ensure that implementations can be done by the respective sub-classes
  4. The abstract class cannot be instantiated
  5. Abstract classes can only be extended
  6. 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?

  1. An abstract class cannot have final methods
  2. 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 

(Visited 26 times, 1 visits today)

Related Posts

Leave a Reply