Abstract Class in Java

A class that is declared with abstract keyword, is known as abstract class in java.

It can have abstract and non-abstract methods (method with body). Before learning java abstract class, let's understand the abstraction in java first.

Abstraction in Java

Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message.

You don't know the internal processing about the message delivery. Abstraction lets you focus on what the object does instead of how it does it.

Ways to Achieve Abstaction

  • Abstract class (0 to 100%)
  • Interface(100%)

Points to Remember

1.Abstract classes are not Interfaces. They are different, we will study this when we will study Interfaces.

2. An abstract class must have an abstract method

3. Abstract classes can have Constructors, Member variables and Normal methods.

4. Abstract classes are never instantiated.

5. When you extend Abstract class with abstract method, you must define the abstract method in the child class, or make the child class abstract.

Abstract class in Java

If a class contain any abstract method then the class is declared as abstract class. An abstract class is never instantiated. It is used to provide abstraction. Although it does not provide 100% abstraction because it can also have concrete method.

Example of Abstract Class

abstract class A{}

abstract method

Method that are declared without any body within an abstract class is known as 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.

Example abstract method

abstract void print Status();//no body and abstract

Interview Tips of Abstraction

  1. Abstraction reduces the complexity by hiding low level(internal) details.
  2. We use abstract keyword before abstract class or abstract method.
  3. Abstract class may have 0 to 100% abstraction.
  4. An abstract class is a class that is declared abstract it may or may not include abstract methods.
  5. Every class having abstract method must be declare as abstract.
  6. We can also define a class as abstract if there is no abstract method while we don't want to create object of that class.
  7. We cannot create an object of abstract class but we can create a constructor.
  8. If we want to hide implementation then we use abstract method (non- implemented method ) In that class.
  9. Non-implemented method must be declare with abstract keyword and that class must be declare as abstract.
  10. An abstract method is a method that is declared with abstract keyword, without implementation .(without braces, and followed by a semicolon).
  11. Abstract method gives you a blueprint to follow while implementing the details.
  12. Abstract method must be implemented in inherited concrete(non-abstract) class.
  13. Abstract class may have static fields and static methods. We can call them by the class name.

Example of abstract class that has abstract method

In this example, Bike the abstract class that contains only one abstract method run. It implementation is provided by the Honda class.

abstract class Bike{ 
abstract void run(); 
class Honda4 extends Bike{ 
void run(){System.out.println("running safely..");
public static void main(String args[])
Bike obj = new Honda4(); 

	Output:running safely..

Understanding the real scenario of abstract class

In this example, Shape is the abstract class, its implementation is provided by the Rectangle and Circle classes. Mostly, we don't know about the implementation class (i.e. hidden to the end user) and object of the implementation class is provided by the

factory method.

A factory method is the method that returns the instance of the class. We will learn about the factory method later. In this example, if you create the instance of Rectangle class, draw() method of Rectangle class will be invoked.

File: TestAbstraction.java

abstract class Shape{ 
abstract void draw(); 
//In real scenario, implementation is provided by others i.e. unknown by end user 
class Rectangle extends Shape{ 
void draw(){System.out.println("drawing rectangle");} 
class Circle1 extends Shape{ 
	void draw(){System.out.println("drawing circle");} 
	//In real scenario, method is called by programmer or user 
	class Test Abstraction{ 
	public static void main(String args[]){ 
	Shape s=new Circle1();
	//In real scenario, object is provided through method e.g. getShape() method 

Abstract class having constructor, data member, methods etc.

An abstract class can have data member, abstract method, method body, constructor and even main() method.

File: TestAbstraction1.java

	abstract class Bike{ 
	System.out.println("bike is created");} 
	abstract void run();
	void changeGear(){System.out.println("gear changed");} 
	class Honda extends Bike{ 
	void run(){System.out.println("running safely..");} 
	class TestAbstraction1{ 
	public static void main(String args[]){ 
	Bike obj = new Honda(); 
Rule: If there is any abstract method in a class, that class must be abstract.
	class Bike12
	abstract void run(); 

				 Output:compile time error
Rule: If there is any abstract method in a class, that class must be abstract.
Rule: 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.
Next ConceptInterface in Java