
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create an Object of an Abstract Class in Java
No, we can't create an object of an abstract class. But, we can create a reference variable of an abstract class. The reference variable is used to refer to the objects of derived classes (subclasses of abstract class).
An abstract class hides the implementation and shows the function definition to the user. A Java abstract class can have instance methods that implement a default behavior and 0 or more abstract methods.
A method which does not have body is known as abstract method. It contains only the method signature and an abstract keyword before it as shown below:
public abstract myMethod();
To use an abstract method, you need to inherit it by extending its class and provide implementation to it.
Instantiating an Abstract Class
Once a class is abstract, it indicates that it may contain incomplete methods, hence, you cannot create an object of the abstract class. If you try to instantiate an abstract class a compile-time error will be generated.
Example
In the following Java example code, we have an abstract class, MyClass, which contains a concrete method named display(). We are trying to instantiate this class using the new keyword.
abstract class MyClass { public abstract void display(); } public class Main { public static void main(String args[]) { MyClass obj = new MyClass(); } }
On compiling, this class generates a compile-time error as -
Main.java:6: error: MyClass is abstract; cannot be instantiated MyClass obj = new MyClass(); ^ 1 error
Creating Reference Variable of Abstract Class
As mentioned earlier in this article, we cannot instantiate an abstract class, but we can create its reference variable. Let's understand it with an example.
Example
In the example given below, we cannot create the object of type Diagram, but we can create a reference variable of type Diagram. Here we created a reference variable of type Diagram, and the Diagram class reference variable is used to refer to the objects of the class Rectangle and Triangle.
abstract class Diagram { double dim1; double dim2; Diagram(double a, double b) { dim1 = a; dim2 = b; } // area is now an abstract method abstract double area(); } class Rectangle extends Diagram { Rectangle(double a, double b) { super(a, b); } // override area for rectangle double area() { System.out.println("Inside Area for Rectangle."); return dim1 * dim2; } } class Triangle extends Diagram { Triangle(double a, double b) { super(a, b); } // override area for triangle double area() { System.out.println("Inside Area for Triangle."); return dim1 * dim2 / 2; } } public class Test { public static void main(String args[]) { // Diagram d = new Diagram(10, 10); // illegal now Rectangle r = new Rectangle(9, 5); Triangle t = new Triangle(10, 8); // This is OK, no object is created Diagram diagRef; diagRef = r; System.out.println("Area of Rectangle is: " + diagRef.area()); diagRef = t; System.out.println("Area of Triangle is:" + diagRef.area()); } }
Output:
Inside Area for Rectangle. Area of Rectangle is: 45.0 Inside Area for Triangle. Area of Triangle is:40.0