Java Abstract Class and Method: Definition and Examples

Rumman Ansari   Software Engineer   2025-03-19 03:38:17   8965  Share
Subject Syllabus DetailsSubject Details 4 Questions 9 Program
☰ TContent
☰Fullscreen

Abstraction is the process of separating ideas from their action. A class that is declared with abstract keyword, is known as abstract class in java. also if a class contain any abstract method then the class is declared as abstract class. It can have abstract and non-abstract methods (method with body).

Abstraction in Java

Abstraction is a process of hiding the implementation details and showing only functionality to the user. for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery. Therefore, to send an e-mail you just need to type the content, mention the address of the receiver, and click send.

Figure: Abstraction

Likewise in Object-oriented programming, abstraction is a process of hiding the implementation details from the user, only the functionality will be visible to the user. Abstraction lets you focus on what the object does instead of how it does it.i.e the user will have the information on what the object does instead of how it does it.


Ways to achieve Abstaction

Here are two ways to achieve abstraction in java

  1. Abstract class (partial abstraction)
  2. Interface (full abstraction)

Abstract class in Java

A class that is declared as abstract is known as abstract class.

  • Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )

  • But, if a class contain at least one abstract method, then the class must be declared abstract.

  • If a class is declared abstract, it cannot be instantiated.

  • To utilize an abstract class, you have to inherit it from another class, provide implementations for the abstract methods in it.

    It needs to be extended and its method implemented.
  • If you inherit an abstract class, you have to provide implementations for all the abstract methods in it.

Syntax:

abstract class class_name { }

Abstract method

Method that are declared without any body within an abstract class are called abstract method. The method body will be defined by its subclass.Abstract method can never be final and static. Any class that extends an abstract class must implement all the abstract methods declared by the super class.

  • The abstract keyword is used to declare the method as abstract.

  • You have to mention the abstract keyword before the method name in the method declaration.

  • An abstract method has a method signature, but no method body.

  • Instead of curly braces, an abstract method will have a semicolon (;) at the end.

Syntax:

abstract return_type function_name ();    // No definition

Example of Abstract class

Program:
abstract class ClassA
{
 abstract void callme();
}

class ClassB extends ClassA
{

 void callme()
 {
  System.out.println("this is call me inside child.");
 }

 public static void main(String[] args)
 {
  ClassB b = new ClassB();
  b.callme();
 }
}
Output:
this is call me inside child.
Press any key to continue . . .

Abstract class with concrete(normal) method.

Program:

Abstract classes can also have normal methods with definitions, along with abstract methods.

abstract class ClassA
{
 abstract void callme();
 public void normal()
 {
  System.out.println("this is concrete method");
 }
}

class ClassB extends ClassA
{
 void callme()
 {
  System.out.println("this is callme.");
 }
 public static void main(String[] args)
 {
  ClassB b = new ClassB();
  b.callme();
  b.normal();
 }
}
Output:
this is callme.
this is concrete method
Press any key to continue . . .

Key Insights on Abstract Classes

Here are some important points about abstract classes:

  • An abstract method cannot exist in a non-abstract class. If a subclass of an abstract superclass does not implement all abstract methods, it must also be declared abstract. In non-abstract subclasses, all abstract methods must be implemented. Abstract methods are always non-static.
  • Abstract classes cannot be instantiated using the new operator. However, they can have constructors, which are called when a subclass instance is created. For example, the constructors of GeometricObject are invoked in the Circle and Rectangle classes.
  • A class with at least one abstract method must be abstract. However, an abstract class can exist without any abstract methods. In this case, it serves as a base class for other subclasses and cannot be instantiated directly.
  • A subclass can be abstract even if its superclass is concrete. For instance, the Object class is concrete, but its subclass, GeometricObject, may be abstract.
  • A subclass can override a method from its superclass and declare it abstract. Although uncommon, this is useful when a method's implementation in the superclass is not valid in the subclass, requiring the subclass to be abstract.
  • Although you cannot instantiate an abstract class using new, it can still be used as a data type. For example, the following statement correctly creates an array of type GeometricObject:
GeometricObject[] objects = new GeometricObject[10];

You can then assign subclass instances to this array:

objects[0] = new Circle();



Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.