Home >>Java Tutorial >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:
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; } }
Here are some of the important points that are important to be remembered when working with the abstract class in the 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(); } }
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()+" %"); } }
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(); } }