Use of abstract class in java

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We all have come across the articles which describe the concept of abstract class in java and the differences between abstract class and interfaces. Although the differences are listed quite well. Still a question remains what exactly is the use of abstract class in java and in what circumstances it is required to use?

According to me, there isn’t any Requirement for use of abstract class in java. It is solely based on what you decide to do.

For example, we have a requirement such that,

  • Some vehicles are to be created.
  • They can be cars, bikes or similar automobiles those will run on the ground.
  • Every vehicle has its own operating method.

We have learned the requirement from these statements. We can infer as follows

  • Every vehicle has an engine. Without it it won’t be able to run
  • Every vehicle has some number wheels as it will operate on the ground.
  • We can create bikes and cars and write their own specific operating methods.
  • But we cannot create a Vehicle as the operating method cannot be generic to all.

Well… our inferences are done. Now, we know one thing about abstract classes. They cannot be instantiated. But we can specify fields in them and the extending classes can inherit them. Well its suites our needs. We do not want Vehicle objects to be created directly. In this scenario, we can make use of abstract class in java. We can make Vehicle as abstract class and put Engine and wheels there.

 

 

Now we can simply create object of Bike and access the Engine provided by  abstract Vehicle  class.

Remember, We can make a class as abstract class when

  • You don’t want you to allow to create object of the class
  • You want the properties of class inherited across its subtypes.

I hope this helps understand the use of abstract class in java

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

One thought on “Use of abstract class in java

  • June 11, 2018 at 11:52 am
    Permalink

    Abstract Classes are classes in Java, that declare one or more abstract methods. For design purpose, a class can be declared abstract even if it does not contain any abstract methods . Many thanks for sharing this.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *