Can we Instantiate and Abstract Class in Java?
What is an abstract class in java?
In Java, we cannot instantiated an abstract class directly but we can use subclass that help to instantiated abstract class. With the help of abstract class we can serves as a model or we can say template from which other classes can inherit and it gives us a subclasses a common interface and functionality.
In the class definition, an abstract class is declared using the abstract keyword. It may contain both abstract and non-abstract methods in addition to fields, constructors, and other members. The abstract class cannot be instantiated on its own, so you cannot directly create objects of it.
An abstract class' primary function is to specify the common traits and behaviours that subclasses should implement or override. It may also contain abstract methods, which are those that are declared but not implemented and which all concrete subclasses must implement. Subclasses are designed to override abstract methods in order to provide their own implementation.
Can we instantiate an abstract class in java
No, Java does not support direct instantiation of abstract classes. An abstract class cannot be instantiated on its own; instead, it serves as a template from which other classes can derive. It is intended for extension by other classes, which then offer implementations for the abstract methods specified in the abstract class.
A concrete class that extends the abstract class can be instantiated, though. All the abstract methods inherited from the abstract class must have implementations in this concrete class. You can indirectly create an instance of the abstract class through inheritance by making an instance of the concrete class.
For example:
abstract class AbstractClass { public abstract void doSomething(); } class ConcreteClass extends AbstractClass { public void doSomething() { System.out.println("Doing something."); } } public class Main { public static void main(String[] args) { ConcreteClass instance = new ConcreteClass(); instance.doSomething(); } }
Output:
Doing something.
Feature of abstract class in java
There are some main feature of an abstract class in java:
- An abstract class cannot be instantiated: As we all know an abstract class can be base class for the other classes but it cannot be used to produce objects directly.
- Abstract methods may be found in abstract classes: abstract methods are declared without a body, if any class extends the abstract class must implement these methods. In other words, abstract class allows us to define a common interface for its subclasses.
- Non-abstract methods may also be found in abstract classes: these methods contain a body and it doesn’t required any additional implementation before being utilised by the subclasses.
- Instance variables may be present in an abstract class: With the help of abstract class and its subclasses we can make use of these variables.
- A non-abstract class may extend an abstract class: as we all know that every abstract method defined by an abstract class that a concrete class extends must have an implementation provided.
- An abstract class has ability to implement interfaces: abstract class can give a default implementation for any interfaces methods it provides, which subclasses can override.
- A contract can be enforced using an abstract class: With the help of abstract class we can impose a contract on its subclasses by specifying a set of abstracts methods, requiring that they fulfil a certain set of requirements.
In other words, abstract classes are a useful tool for creating hierarchies of related classes in java. It helps us to define a common interface for a group of related classes while allowing for flexibility in the implementation of each individual classes.