
Java Abstract Class and Method: Definition and Examples
Table of Content:
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 JavaAbstraction 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.

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
- Abstract class (partial abstraction)
- 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 ofGeometricObject
are invoked in theCircle
andRectangle
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 typeGeometricObject
:
GeometricObject[] objects = new GeometricObject[10];
You can then assign subclass instances to this array:
objects[0] = new Circle();
- Question 1: What is Abstraction?
- Question 2: What is Abstract class?
- Question 3: When Abstract methods are used?
- Question 4: What is the difference between an Interface and an Abstract class?
Related Questions
- Assignment 1: Abstract classes may contain abstract methods, To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.
- Assignment 2: Abstract classes may not contain abstract methods
- Assignment 3: If a class has at least one abstract method, then the class must be declared abstract.
- Assignment 4: If a class is declared abstract, it cannot be instantiated.
- Assignment 5: If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
- Assignment 6: Understanding the important scenario of abstract class
- Assignment 7: Understanding the important real world abstract class
- Assignment 8: If you are extending any abstract class that have abstract method, you must either provide the implementation of the method or make this class abstract.
- Assignment 9: Abstract class having constructor, data member, methods etc.