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 the abstraction. We can achieve 0 – 100% abstraction by using an abstract
  • An abstract class can have abstract and concrete method 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 implements 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


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu