Monday, May 12, 2008

The ‘abstract’ keyword of Java Programming Language

One of the frequently asked questions in Java interviews are about abstract classes and abstract methods.

‘abstract’ is a non-access modifier that can be applied to classes and methods.

This modifier cannot be applied to instance variables and local variables.

If a class is marked as abstract, it cannot be instantiated. i.e we cannot create objects of abstract class. An abstract class must be sub classed (extended) and the first concrete ( non-abstract) class extending an abstract class must implement all the abstract methods of the super class (if not implemented in any of the super classes.

If there is any method in an abstract class marked as abstract, then the class has to be marked as abstract. But we can have an abstract class without any abstract methods.

An abstract keyword has a significant role in java polymorphism and inheritance concepts.

Abstract methods ends with a semicolon – instead of curly braces.

We cannot mark a class both as abstract and final.

We cannot mark a method as both abstract and synchronized.

We cannot mark a method as both abstract and native.

We cannot mark a method as both abstract and strictfp.

We cannot mark a method as both abstract and private.

All abstract methods must be implemented in first non-concrete subclass unless it is implemented in any of the super class which are also abstract.

No comments: