Home >>Java Tutorial >Abstract class in Java

Abstract class in Java

Abstract class in Java

Abstract class in Java language is basically the part of the data abstraction. And the data abstraction is the procedure of hiding the certain details and displaying only the essential information to the user. In Java the abstraction can be achieved basically with either the abstract classes or the interfaces.

The abstract keyword in the Java language is basically a non-access modifier that is generally used for the classes and methods:

  • Abstract class: This is a very restricted class in Java that cannot be used in order to create objects and in order to access it; it should be inherited from another class.
  • Abstract method: This method in Java can only be used in an abstract class and this does not possess a body. The body to this method is generally provided by the subclass.

An abstract class in Java can posses both abstract and regular methods. A class that contains the abstract keyword inside its declaration is generally called as an abstract class. Abstract classes may or may not consist of the abstract methods, that are basically the methods without body. However, if a class possesses at least one abstract method then it is mandatory that the class must be declared abstract. In case a class is declared as abstract then it cannot be instantiated. In order to use an abstract class the user have to inherit it from another class and deliver the implementations to the abstract methods that are present in it.

Here is an example of the abstract class in the Java language and in this example it has been depicted that in order to create an abstract class there is a need to use the abstract keyword just before the class keyword that too in the class declaration:

public abstract class Student 
{
   private String name;
   private String address;
   private int number;

   public Student(String name, String address, int number) 
   {
      System.out.println("Constructing an Student");
      this.name = name;
      this.address = address;
      this.number = number;
   }
   
   public double computePay() 
   {
     System.out.println("Inside Student computePay");
     return 0.0;
   }
   
   public void mailCheck() 
   {
      System.out.println("Mailing a check to " + this.name + " " + this.address);
   }

   public String toString() 
   {
      return name + " " + address + " " + number;
   }

   public String getName() 
   {
      return name;
   }
 
   public String getAddress() 
   {
      return address;
   }
   
   public void setAddress(String newAddress) 
   {
      address = newAddress;
   }
 
   public int getNumber() 
   {
      return number;
   }
}

Points to Remember

Here are some of the important points that are important to be remembered when working with the abstract class in the Java Language:

  • An abstract class should be declared with an abstract keyword in the language.
  • This class can possess abstract and non-abstract methods.
  • Generally, this class cannot be instantiated.
  • This class can possess constructors and the static methods too.
  • This class in Java language can possess the final methods that will force the subclass not to change the body of the method that has been provided.

Example of the Abstract class in Java Language

Here are the various example of the abstract class that will help you to understand the various concepts related to it and will boost your concept from a depth and will depict you the physical aspect of it.

1. In the following example, the instance of Rectangle class is created then the draw() method of Rectangle class will be invoked.

abstract class Shapes
{  
	abstract void draw();  
}  
class Rect extends Shapes
{  
	void draw()
	{
		System.out.println("draw rectangle");
	}  
}  
class Circle extends Shapes
{  
	void draw()
	{
		System.out.println("draw circle");
	}  
}  
class DemoAbstraction
{  
	public static void main(String args[])
	{  
		Shapes s=new Circle();  
		s.draw();  
	}  
}  
Output:
draw circle

2. In this example, the use of the abstract class has been depicted, observe the example carefully to get the concept:

abstract class Bank
{    
	abstract int getRateOfInterest();    
}    
class ICICI extends Bank
{    
	int getRateOfInterest()
	{
		return 5;
	}    
}    
class HDFC extends Bank
{    
	int getRateOfInterest()
	{
		return 6;
	}    
}    
    
class TestBank
{    
	public static void main(String args[])
	{    
		Bank b;  
		b=new ICICI();  
		System.out.println("Rate of Interest ICICI: "+b.getRateOfInterest()+" %");    
		
		b=new HDFC();  
		System.out.println("Rate of Interest HDFC : "+b.getRateOfInterest()+" %");    
	}
}    
Output:
Rate of Interest ICICI: 5 %
Rate of Interest HDFC: 6 %

3.This is another example of the Abstract class in Java language that will enhance your concept as this example is of the different context and different functionality:

//Example of an abstract class that has abstract and non-abstract methods  
abstract class Bike
{  
	Bike()
	{
		System.out.println("bike is created");
	}  
	abstract void run();  
	void changeGear()
	{
		System.out.println("gear changed");
	} 
}  

class Hero extends Bike
{  
	void run()
	{
		System.out.println("running safely..");
	}  
 }  

class TestAbstract
{  
	public static void main(String args[])
	{  
		Bike obj = new Hero();  
		obj.run();  
		obj.changeGear();  
	}  
}  
Output:
bike is created
running safely..
gear changed

No Sidebar ads