Explain about abstract classes in java?

Sometimes we may come across a situation where we cannot provide implementation to all the methods in a class. We want to leave the implementation to a class that extends it. In such case we declare a class as abstract. To make a class abstract we use key word abstract. Any class that contains one or more abstract methods is declared as abstract. If we donโ€™t declare class as abstract which contains abstract methods we get compile time error. We get the following error.

โ€œThe type <class_name> must be an abstract class to define abstract methods.โ€

Signature ; abstract class { } <class_name>

For example if we take a vehicle class we cannot provide implementation to it because there may be two wheelers , four wheelers etc. At that moment we make vehicle class abstract. All the common features of vehicles are declared as abstract methods in vehicle class. Any class which extends vehicle will provide its method implementation. Itโ€™s the responsibility of subclass to provide implementation.

The important features of abstract classes are :

1) Abstract classes cannot be instantiated.

2) An abstract classes contains abstract methods, concrete methods or both.

3) Any class which extends abstract class must override all methods of abstract class.

4) An abstract class can contain either 0 or more abstract methods.

Though we cannot instantiate abstract classes we can create object references . Through superclass references we can point to subclass.


Posted

in

Tags:

Comments

Leave a Reply