The abstract keyword can only be used on classes and methods in Java. An abstract class cannot be instantiated and an abstract method can have no implementation. Let’s dig further.
When a class is declared with abstract keyword then that particular class cannot be instantiated.It can only be extended and the all the methods of the abstract class needs to be implemented by the class which extends our abstract class
Example of abstract class
abstract class A{}
An abstract class can have both abstract or non-abstract methods as well
Abstract Method in Java
When a method has abstract keyword then the class should definitely be an abstract class and when a method is declared abstract then it cannot have implementation.
Example of abstract method
abstract void disp();
Abstract classes and Abstract methods are like skeletons. It defines a structure, without any implementation. let’s put it all together
abstract class A { abstract void disp(); public void show() { System.out.println("Show method"): } }
Example of Abstract Method and Abstract Class in Java
In this example, we have abstract class Car and an abstract method move(), the implementation of the Car class is provided by Bmw Class and Audi Class.
abstract class Car { public abstract void move(); } class Bmw extends Car { @Override public void move() { System.out.println("Move method of BMW"); } } class Audi extends Car { @Override public void move() { System.out.println("Move method of Audi"); } } public class Logic { public static void main(String args[]) { Car c = new Bmw(); c.move(); } }
Output
Move method of BMW
In the real world scenario we will not know the implementation class and object of the implementation will be provided by Factory Method (public method something like getCarObject() so that the user will not know about the implementation).But in this example, we have created an instance of Bmw and Audi classes directly.
Can Abstract Class have constructor?
An abstract class can have constructor, abstract method, non abstract method , data member and even main method as well.
abstract class Shape { Shape() { System.out.println("Shape constructor called"); } abstract void color(); void size() { System.out.println("Size method called"); } } class Rectangle extends Shape { void color() { System.out.println("Color of Rectange is Blue"); } } public class Logic { public static void main(String args[]) { Rectangle rect = new Rectangle(); rect.color(); rect.size(); } }
Output
Shape constructor called Color of Rectange is Blue Size method called
Important points to remember
- If we have declared a method as abstract then you must declare the class as abstract, abstract method cannot be present in a non-abstract class.
- Abstract Class can have concrete methods(non-abstract) as well.
- Abstract method should not have implementation(no body).
- Abstract method declaration should end with a semicolon (;)
- The class extending the abstract class should implement all the abstract methods.
Leave a Reply