09.02. Abstract Classes

Abstract Classes

  • Eclipse: Oxygen
  • Java: 1.8

In Java, Abstract classes are created by using the abstract keyword in the class definition.

Syntax

Some facts related to Abstract class in Java

  • An abstract class is used to achieve abstraction. A class that is declared with the abstract keyword is known as an abstract class in Java.
  • It can have abstract and non-abstract methods (method with the body).
  • An abstract class can have abstract and concrete methods both.
  • An abstract class cannot be instantiated, it means we cannot create an object of the class. We need a different class for the implementation of the abstract class.

Let’s see the implementation of the abstract class

In the following example, we have an abstract class “Person” which has an abstract method “eat”.

Note: A class must be an abstract class to contain an abstract method.

A concrete class (a class which does not contain any abstract method in it) must extend the abstract class to implement the abstract method. As soon as we extend an abstract class, Java displays an error message to implement the abstract method of the class.

We have two concrete class which extends Person class and implement the abstract method “eat” as per the requirement.

Output

share his/her thoughts

Eats Vegan

=======================

share his/her thoughts

Eats NonVegan

Contributed by: Poonam Tomar

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments