Differences between an Abstract Class and a Concrete Class in Java

DifferencesJavaObject Oriented ProgrammingProgramming

In Java, abstraction is achieved using Abstract classes and interfaces. Both contains abstract methods which a child class or implementing class has to implement.

Abstract Class

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.

Concrete Class

A concrete class is a blueprint from which individual objects are created using new operator

  • A concrete class cannot have abstract method.

  • A concrete class can be instantiated using new operator.

  • A concrete class can be declared final.

  • A concrete class can be extended without any boundation of overriding its method.

Differences

Following are the important differences between abstract class and a concrete class.

Sr. No. Key Abstract Class Concrete Class
1 Supported Methods Abstract class can have both an abstract as well as concrete methods. A concrete class can only have concrete methods. Even a single abstract method makes the class abstract.
2 Instantiation Abstract class can not be instantiated using new keyword. Concrete class can be instantiated using new keyword.
3 Abstract method Abstract class may or may not have abstract methods. Concrete class can not have an abstract method.
4 Final Abstract class can not be declared as a final class. Concrete class can be declared final.
5 Keyword Abstract class declared using abstract keyword. Concrete class is not having abstract keyword during declaration.

Example

Following example shows usage of abstract class and concreate class. Here Cat is an abstract class and Lion is a concrete class −

JavaTester.java

public class JavaTester { public static void main(String args[]) { Cat lion = new Lion(); lion.eat(); } } abstract class Cat { abstract public void eat(); } class Lion extends Cat{ public void eat(){ System.out.println("Lion eats"); } }

Output

Lion eats

Example

Following is another example of abstract class vs concrete class where concrete class can be final and can have mulitple non-abstract methods −

JavaTester.java

public class JavaTester { public static void main(String args[]) { Lion lion = new Lion(); lion.eat(); lion.hunt(); } } abstract class Cat { abstract public void eat(); } class Lion extends Cat{ public void eat(){ System.out.println("Lion eats"); } public void hunt(){ System.out.println("Lion hunts"); } }

Output

Lion eats
Lion hunts

Here we can see that both Abstract class and Interface are important in defining a certain behavior of an object. We generally use abstract class when it contains a fair amount of common functionality for subclasses and interface is used to specify a behavior in generic fashion.

raja
Updated on 19-Aug-2022 11:12:51

Advertisements