Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Can We Create Object of Abstract Class?

Last Updated on April 12, 2023 by Prepbytes

In Java, an abstract class is a class that is declared with the abstract keyword which cannot be instantiated. This means that you cannot create an object of an abstract class directly. However, you can create an instance of a subclass that extends the abstract class.

What is an Abstract Class in Java?

The class which is defined with the abstract keyword is the Abstract Class in Java. It can have abstract methods, concrete methods, and instance variables. The abstract method in Java is the one that is only declared but does not have any implementation. Whereas normal methods have an implementation. An abstract class is used as a blueprint for other classes to extend and implement abstract methods.

Can We Create Object of Abstract Class?

Since an abstract class is incomplete, i.e., it contains abstract methods that do not have an implementation, it cannot be instantiated. If we try to instantiate an object of the abstract class, we will get the compilation error on the output screen. The reason is that the abstract class is not fully defined and, hence, cannot be instantiated.

The following code will show you what happens if we try to create an object of an Abstract Class in Java.

Code:

abstract class Shape {
   abstract void draw();
}

class Circle extends Shape {
   void draw() {
      System.out.println("Drawing Circle");
   }
}

public class Main {
   public static void main(String[] args) {
      Shape s = new Shape(); 
   }
}

Output:

Main.java:13: error: Shape is abstract; cannot be instantiated
      Shape s = new Shape(); 
                ^
1 error

Explanation:
In the above code, we have an abstract class Shape that has an abstract method draw(). We have also created a concrete subclass Circle that extends the Shape class and provides an implementation for the draw() method. In the main method, we are trying to create an object of the abstract class Shape, which results in a compilation error as shown in the output.

Creating an Object of a Subclass

As mentioned earlier, we cannot create an object of an abstract class directly. However, we can create an instance of a subclass that extends the abstract class. Let’s modify our previous example to create an object of the Circle class.

Code:

abstract class Shape {
   abstract void draw();
}

class Circle extends Shape {
   void draw() {
      System.out.println("Drawing Circle");
   }
}

public class Main {
   public static void main(String[] args) {
      Shape s = new Circle();
      s.draw();
   }
}

Output:

Drawing Circle

Explanation:
In the above code’s main class, we have created an instance of the Circle class, which is a subclass of the Shape abstract class. We have assigned this instance to a variable of the abstract class Shape. We are then calling the draw() method on the Shape object, which actually invokes the implementation of the draw() method in the Circle class. This will print “Drawing Circle” on the output screen.

Anonymous Class in Java

An anonymous class in Java is a local class that does not have a name and is created on the fly. It is typically used to implement an interface or extend a class and override its methods. Since an abstract class cannot be instantiated directly, we can create an anonymous class from an abstract class to provide an implementation for its abstract methods.

Here is an example of creating an anonymous class from an abstract class in Java.

Code:

abstract class Animal {
   public abstract void makeSound();
}

public class Main {
   public static void main(String[] args) {
      Animal dog = new Animal() {
         public void makeSound() {
            System.out.println("Woof");
         }
      };
      dog.makeSound();
   }
}

Output:

Woof

Explanation:
In the above code, we have declared an abstract class named “Animal”. This class has an abstract method makeSound(). We then create an anonymous class from Animal and provide an implementation for its makeSound() method. We can then create an object of this anonymous class and call its makeSound() method to print "Woof" to the console.

Creating an anonymous class from an abstract class allows us to provide a concrete implementation for its abstract methods without having to define a new class explicitly. This is particularly useful when we only need to implement the abstract class once and do not want to clutter our code with additional classes.

Conclusion
An abstract class is a class that cannot be instantiated directly because it contains abstract methods that do not have an implementation. However, we can create an instance of a subclass that extends the abstract class and provides an implementation for the abstract methods. It is important to understand this concept to effectively use abstract classes in Java.

Frequently Asked Questions(FAQs)

Some Frequently Asked Questions on “Can We Create Object of Abstract Class” are given below.

Ques 1. Give a reason why we cannot create an object of the abstract class in Java.
Ans. We cannot create an object of an abstract class in Java because it is an incomplete class that contains abstract methods without any implementation. Therefore, it cannot be instantiated directly.

Ques 2. What happens if we try to create an object of an abstract class in Java?
Ans. If we try to create an object of an abstract class in Java, we will get a compilation error.

Ques 3. Enlist the difference between the Abstract Class and interface in Java.
Ans. An abstract class can have both abstract and non-abstract methods, whereas an interface can only have abstract methods. Also, an interface can be implemented by multiple classes, whereas an abstract class can only be extended by one class.

Ques 4. Can we provide an abstract class with a constructor in Java?
Ans. Yes, an abstract class in Java can have a constructor which is used to initialize the fields of the abstract class.

Ques 5. Can we declare a method as abstract and final in an abstract class in Java?
Ans. No, we cannot declare a method as abstract and final in an abstract class in Java. An abstract method is intended to be implemented by the subclass, whereas a final method cannot be overridden

Ques 6. Can we create an anonymous class from an abstract class in Java?
Ans. Yes, we can create an anonymous class from an abstract class in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *