Open In App

Can We Instantiate an Abstract Class in Java?

Last Updated : 07 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Abstract class, we have heard that abstract class are classes which can have abstract methods and it can’t be instantiated. We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.

Example 1

Java




// Java program to demonstrate abstract class
// cannot have instance
  
public abstract class ClassOne {
  
    public void printSomething()
    {
        System.out.println("Hello in abstract class");
    }
}
class CreateClassOne {
  
    public static void main(String[] args)
    {
        // instance of abstract 
          // class "ClassOne"
        ClassOne obj = new ClassOne();
    }
}


Output

prog.java:17: error: ClassOne is abstract; cannot be instantiated
        ClassOne obj = new ClassOne();
                       ^
1 error

Example 2

Java




// Java code to demonstrate
// Anonymous class
  
public abstract class ClassOne {
  
    public void printSomething()
    {
        System.out.println("Hello in abstract class");
    }
}
  
class InheritClassOne {
  
    public static void main(String[] args)
    {
  
        // obj points to anonymous subclass
        ClassOne obj = new ClassOne() {};
        
          // calls the implementation 
          // provided by ClassOne
        obj.printSomething();
    }
}


Output

Hello in abstract class

Is abstract class instantiated here! But that’s not possible!

Yes, the answer is still the same, the abstract class can’t be instantiated, here in the second example object of ClassOne is not created but the instance of an Anonymous Subclass of the abstract class. And then you are invoking the method printSomething() on the abstract class reference pointing to subclass object obj. When you have added { } while creating the object in second class, the compiler takes it as an anonymous class where { } denotes the body of the anonymous class

Example 1

Java




// java program to demonstrate anonymous class
  
interface NewClass {
    public void show();
}
  
class AnonymousClassExample {
  
    public static void main(String[] args)
    {
        // nc points to anonymous class inside
        // the {}
        NewClass nc = new NewClass() {
            public void show()
            {
                System.out.println(
                    "This is an anonymous class implementing interface");
            }
        };
  
        // calls the implementation
        // method of anonymous class
        nc.show();
    }
}


Output

This is an anonymous class implementing interface

 In this example, the interface is not instantiated but an anonymous class is implementing the interface New Class. Now, you may have understood that how the second example worked where instead of abstract class instantiation, an anonymous subclass was created or abstract class were implemented by an abstract class. 

Example 2

Java




// Anonymous class implementing abstract class
  
public abstract class ClassOne {
  
    public ClassOne()
    {
        System.out.println(
            "Anonymous(Unnamed) Subclass object Created");
    }
  
    public void printSomething()
    {
        System.out.println(
            "Hello,in abstract class:printSomething method");
    }
    public abstract void implementMethod();
}
  
class AnonymousClassExample {
  
    public static void main(String[] args)
    {
        // anonymous class
        ClassOne obj = new ClassOne() {
            @Override public void implementMethod()
            {
                System.out.println(
                    "Implemented abstract method in anonymous class");
            }
        };
  
        // calls abstract class implementation
        obj.printSomething();
  
        // calls anonymous class implementation
        obj.implementMethod();
    }
}


Output

Anonymous(Unnamed) Subclass object Created
Hello,in abstract class:printSomething method
Implemented abstract method in anonymous class


Similar Reads

Difference Between Abstract Class and Abstract Method in Java
Abstract is the modifier applicable only for methods and classes but not for variables. Even though we don't have implementation still we can declare a method with an abstract modifier. That is abstract methods have only declaration but not implementation. Hence, abstract method declaration should compulsory ends with semicolons. Illustration: publ
3 min read
Difference between Abstract Class and Concrete Class in Java
Abstract Class: An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along wit
5 min read
Why can't static methods be abstract in Java?
In Java, a static method cannot be abstract. Doing so will cause compilation errors.Example: Java Code // Java program to demonstrate // abstract static method import java.io.*; // super-class A abstract class A { // abstract static method func // it has no body abstract static void func(); } // subclass class B class B extends A { // class B must
3 min read
Why Java Interfaces Cannot Have Constructor But Abstract Classes Can Have?
Prerequisite: Interface and Abstract class in Java. A Constructor is a special member function used to initialize the newly created object. It is automatically called when an object of a class is created. Why interfaces can not have the constructor? An Interface is a complete abstraction of class. All data members present in the interface are by de
4 min read
Why a Constructor can not be final, static or abstract in Java?
Prerequisite: Inheritance in Java Constructor in java is a special type of method which is different from normal java methods/ordinary methods. Constructors are used to initialize an object. Automatically a constructor is called when an object of a class is created. It is syntactically similar to a method but it has the same name as its class and a
6 min read
Java | Abstract Class and Interface | Question 1
Which of the following is FALSE about abstract classes in Java (A) If we derive an abstract class and do not implement all the abstract methods, then the derived class should also be marked as abstract using 'abstract' keyword (B) Abstract classes can have constructors (C) A class can be made abstract without any abstract method (D) A class can inh
1 min read
Java | Abstract Class and Interface | Question 2
Which of the following is true about interfaces in java. 1) An interface can contain following type of members. ....public, static, final fields (i.e., constants) ....default and static methods with bodies 2) An instance of interface can be created. 3) A class can implement multiple interfaces. 4) Many classes can implement the same interface. (A)
1 min read
Java | Abstract Class and Interface | Question 3
Predict the output of the following program. abstract class demo { public int a; demo() { a = 10; } abstract public void set(); abstract final public void get(); } class Test extends demo { public void set(int a) { this.a = a; } final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj =
1 min read
Constructor in Java Abstract Class
Constructor is always called by its class name in a class itself. A constructor is used to initialize an object not to build the object. As we all know abstract classes also do have a constructor. So if we do not define any constructor inside the abstract class then JVM (Java Virtual Machine) will give a default constructor to the abstract class. I
4 min read
Implement Interface using Abstract Class in Java
Interface contains only abstract methods that can't be instantiated and it is declared by keyword interface. A class that is declared with the abstract keyword is known as an abstract class in Java. This is a class that usually contains at least one abstract method which can't be instantiated and It is also possible for the class to have no methods
3 min read