elibraryportal Logo

Abstract Classes in Java | Example

→ A class is declared with the abstract keyword it is known as an abstract class .

Meaning of Abstraction in Java

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Example:- sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery.

Rule for Java Abstract class

  • An abstract class must be declared with an abstract keyword.
  • Abstract class that has an abstract method.
  • It is used for abstraction
  • Abstract classes are never instantiated.
  • It can have constructors and static methods also.
  • It can have final methods which will force the subclass not to change the body of the method.
  • An abstract class may or may not have an abstract method. But if any class has even a single abstract method, then it must be declared abstract.

Syntax:

Example1:

Output:


Example2: Upcasting example in abstract class

Output:


Abstract Method in Java

→ A method declared without any body (no implementation) within an abstract class are called abstract method..

→ A method which is declared as abstract and does not have implementation is known as an abstract method..

→ Abstract method can never be final and static.

→ Any class that extends an abstract class must, implement all the abstract methods.

Syntax:

Example1 :-

Abstract class having constructor, data member and methods

Output :-


Example2 :-

Output :-


Next Topic