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 July 5, 2024 by Abhishek Sharma

Abstract classes in object-oriented programming serve as blueprints for other classes but cannot be instantiated themselves. They typically contain abstract methods that must be implemented by subclasses. Despite this restriction, there are scenarios where creating objects directly from abstract classes can be beneficial or necessary. This article explores the concept of creating objects of abstract classes, the implications, and practical considerations.

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
Creating objects of abstract classes is not supported in most programming languages due to their incomplete nature and design principles. Abstract classes are meant to be extended and specialized by subclasses, providing a blueprint for related classes while ensuring adherence to a common interface or behavior. By adhering to these principles, developers can maintain code flexibility, scalability, and maintainability in object-oriented systems.

FAQs Related to Creating Objects of Abstract Classes

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

1. Why can’t we create objects of abstract classes directly?
Answer:
Abstract classes are designed to be incomplete and typically contain abstract methods without implementations. They serve as templates for subclasses to inherit and implement, ensuring consistency in behavior across related classes.

2. Can abstract classes have constructors?
Answer:
Yes, abstract classes can have constructors that are used to initialize attributes common to all subclasses. However, constructors of abstract classes are typically invoked by constructors of their subclasses.

3. How do we instantiate classes that inherit from abstract classes?
Answer:
To create objects, instantiate concrete subclasses that extend the abstract class. Concrete subclasses provide implementations for abstract methods defined in the abstract class.

4. What is the purpose of abstract classes if we cannot create objects from them?
Answer:
Abstract classes provide a blueprint for related classes by defining a common interface or behavior through abstract methods. They facilitate code reusability, enforce consistency in class hierarchies, and promote polymorphic behavior.

5. Can abstract classes have non-abstract methods?
Answer:
Yes, abstract classes can have both abstract and non-abstract (concrete) methods. Non-abstract methods in abstract classes provide default implementations that subclasses can override if necessary.

6. How do factory methods relate to abstract classes?
Answer:
Factory methods are static methods that create instances of classes based on specific conditions or parameters. They can be used to instantiate concrete subclasses of abstract classes, providing flexibility and encapsulating object creation logic.

Leave a Reply

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