Abstract Class in Java
A class that is declared using “abstract” keyword is known as abstract class. It can have abstract methods(methods without
body) as well as concrete methods (regular methods with body). A normal class(non-abstract class) cannot have abstract
methods. In this guide we will learn what is a abstract class, why we use it and what are the rules that we must remember
while working with it in Java.
An abstract class can not be instantiated, which means you are not allowed to create an object of it.
Abstract class cannot be instantiated which means you cannot create the object of it. To use this class, you need to create
another class that extends this class and provides the implementation of abstract methods, then you can use the object of that
child class to call non-abstract methods of parent class as well as implemented methods(those that were abstract in parent but
implemented in child class).
Why can’t we create the object of an abstract class?
Because these classes are incomplete, they have abstract methods that have no body so if java allows you to create object of
this class then if someone calls the abstract method using that object then What would happen ? There would be no actual
implementation of the method to invoke. Also because an object is concrete. An abstract class is like a template, so you have
to extend it and build on it before you can use it.
Key Points:
1. An abstract class has no use until unless it is extended by some other class.
2. If you declare an abstract method in a class then you must declare the class abstract as well. you can’t have abstract
method in a concrete class. It’s vice versa is not always true: If a class is not having any abstract method then also it can
be marked as abstract.
3. It can have non-abstract method (concrete) as well.
Example of Abstract class and method
abstract class MyClass{
public void disp(){
[Link]("Concrete method of parent class");
}
abstract public void disp2();
}
Key Features :
A class which contains the abstract keyword in its declaration 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 has at least one abstract method, then the class must be declared abstract.
If a class is declared abstract, it cannot be instantiated.
To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in
it.
If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
Abstract Methods
If you want a class to contain a particular method but you want the actual implementation of that method to be determined
by child classes, you can declare the method in the parent class as an abstract.
abstract keyword is used to declare the method as abstract.
You have to place the abstract keyword before the method name in the method declaration.
An abstract method contains a method signature, but no method body.
Instead of curly braces, an abstract method will have a semicolon (;) at the end.
Following is an example of the abstract method.
public abstract class Employee {
private String name;
private String address;
private int number;
public abstract double computePay();
// Remainder of class definition
}
Declaring a method as abstract has two consequences −
The class containing it must be declared as abstract.
Any class inheriting the current class must either override the abstract method or declare itself as abstract
Abstract class Example
//abstract parent class
abstract class Animal{
//abstract method
public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal{
public void sound(){
[Link]("Woof");
}
public static void main(String args[]){
Animal obj = new Dog();
[Link]();
}
}
Output:
Woof
Since the Animal class has an abstract method, you must need to declare this class abstract. Now each animal must have a
sound, by making this method abstract we made it compulsory to the child class to give implementation details to this
method. This way we ensures that every animal has a sound.
Hence for such kind of scenarios we generally declare the class as abstract and later concrete classes extend these classes and
override the methods accordingly and can have their own methods as well.
Abstract class declaration
//Declaration using abstract keyword
abstract class A{ //This is abstract method
abstract void myMethod();
//This is concrete method with body
void anotherMethod(){
//Does something abstract class outlines the methods but not necessarily implements all the methods.
}}
Rules
1. There are cases when it is difficult or often unnecessary to implement all the methods in parent class. In these cases,
we can declare the parent class as abstract, which makes it a special class which is not complete on its own.
2. A class derived from the abstract class must implement all those methods that are declared as abstract in the parent
class.
3. Abstract class cannot be instantiated which means you cannot create the object of it.
4. To use this class, you need to create another class that extends this class and provides the implementation of abstract
methods, then you can use the object of that child class to call non-abstract methods of parent class as well as
implemented methods(those that were abstract in parent but implemented in child class).
5. If a child does not implement all the abstract methods of abstract parent class, then the child class must need to be
declared abstract as well.
6. Since abstract class allows concrete methods as well, it does not provide 100% abstraction. You can say that it
provides partial abstraction. Abstraction is a process where you show only “relevant” data and “hide” unnecessary
details of an object from the user.
Why can’t we create the object of an abstract class?
7. Because these classes are incomplete, they have abstract methods that have no body so if java allows you to create
object of this class then if someone calls the abstract method using that object then What would happen ?There would
be no actual implementation of the method to invoke.
Also because an object is concrete. An abstract class is like a template, so you have to extend it and build on it before
you can use it.
Abstract class vs Concrete class
Key Points:
1. An abstract class has no use until unless it is extended by some other class.
2. If you declare an abstract method in a class then you must declare the class abstract as well. you can’t have abstract
method in a concrete class. It’s vice versa is not always true: If a class is not having any abstract method then also it can
be marked as abstract.
3. It can have non-abstract method (concrete) as well.
1) Abstract method has no body.
2) Always end the declaration with a semicolon(;).
3) It must be overridden. An abstract class must be extended and in a same way abstract method must be overridden.
4) A class has to be declared abstract to have abstract methods.
Note: The class which is extending abstract class must override all the abstract methods.
Example
abstract class MyClass{
public void disp(){
[Link]("Concrete method of parent class");
abstract public void disp2();
class Demo extends MyClass{
/* Must Override this method while extending MyClas
*/
public void disp2()
[Link]("overriding abstract method");
public static void main(String args[]){
Demo obj = new Demo();
obj.disp2();
Output:
overriding abstract methode