An abstract class can be extended, so so all class members(variables+methods)of an abstract class can be invoked/used from a subclass that extends the abstract class. So, why does JAVA prohibits from creating an object of the abstract class?
-
4Suppose a class has an abstract method `String foo()`. Suppose you create an instance of this abstract class, and then call `foo()` on this instance. What would you expect to happen? – JB Nizet Apr 01 '18 at 09:18
-
Possible duplicate of [Interview: Can we instantiate abstract class?](https://stackoverflow.com/questions/13670991/interview-can-we-instantiate-abstract-class) – MD Ruhul Amin Apr 01 '18 at 09:18
5 Answers
Java doesn't allow abstract classes to be instantiated directly because there would be no point... If you had:
abstract class Person {
abstract String getName();
int getAge() {
return 72;
}
}
And were allowed to call it like this
public static void main(String... args) {
System.out.println(new Person().getName());
}
Then things wouldn't make sense.
However, since abstract classes can have implemented methods, then your comment:
so all class members(variables+methods)of an abstract class can be invoked/used from a subclass that extends the abstract class.
Simply says that you missed the part from a subclass, the subclass being supposed concrete. In other words, those methods and variables can still be used, but they must be used on a concrete type (that would be a type that gives an implementation for getName
in the example above, to make things make sense again)

- 44,416
- 5
- 53
- 99
-
You can have an abstract class with no unimplemented/abstract methods. – lexicore Apr 01 '18 at 09:32
-
@lexicore true, but that's irrelevant in this case, because java will prevent you from instantiating it anyway... – ernest_k Apr 01 '18 at 09:33
-
Exactly this is my point. Java does not allow instantiating an abstract class no matter if it has abstract/unimplemented methods or not. So arguing with abstract methods (like you did) misses the point. – lexicore Apr 01 '18 at 09:35
-
@lexicore your logic is flawed. Most abstract classes do have abstract methods. Instantiating them makes no sense as the answer shows. Sure, you can, exceptionally, have abstract classes without any abstract method. But abstract in this case precisely means that the base class should always be subclassed, for whatever reason. The compiler shouldn't make an exception to the general rule in that case, because if the developer wants that class to be instantiatable, then the class simply shouldn't be abstract. – JB Nizet Apr 01 '18 at 09:39
-
1@lexicore No, but that exactly is the point. If it weren't because of abstract methods, Java would have a reason to let you instantiate it. **but the problem is that you can add abstract methods** - **ABSTRACTION hinges only on the fact that methods are unimplemented**... What other reasons do you see beside abstract methods (present or future)? – ernest_k Apr 01 '18 at 09:39
-
@ErnestKiwele it is the decision of the developer to make a class instantiatable or not. There may be technical reasons (like abstract methods) or non-technical reasons (like adequate modelling). I also somewhat object saying "Java .. lets you/does not let you instantiate it". It is the developer of the class who does it, not Java. – lexicore Apr 01 '18 at 10:08
-
@lexicore there's some fundamental misunderstanding there... Of course it's the developer's decision, but the question is about abstract classes as defined and enforced by the Java language ("So, why does JAVA prohibit..."). Yes, the developer designs, but that's done within the boundaries set by the language, and it's about these boundaries that the original question was asked... You can decide to make a type "instantiable", but you can't make an abstract type directly "instantiable" in Java... What are we missing here? – ernest_k Apr 01 '18 at 10:35
Making class abstract is an instrument for the developer to specify that certain class may not be instantiated.
There may be many reasons for this.
One of the typical usages of this is template method pattern where you declare an use an abstract method in the abstract base class and concrete implementations of subclasses implement the declared abstract method.
Or you may have a complex class hierarchy where superclasses are not concrete by nature. Here's for instance a hierarchy of PolygonType
in Geography Markup Language 3.2.1:
PolygonType extends
AbstractSurfaceType extends
AbstractGeometricPrimitiveType extends
AbstractGeometryType extends
AbstractGMLType
A designer may have desided here that only concrete classes like PolygonType
should be instantiatable, not higher-level abstractions like AbstractGeometryType
. Making all the Abstract*
abstract
actually enforces this idea on the developer who uses this schema or library.

- 42,748
- 17
- 132
- 221
There are reasons to create a abstract class. Suppose you have a factory class Car
to create a instance of car of different car type. So every car will have some common properties like, number of wheel, color, speed meter etc. But every car must have all these properties before you create one of them. But a car can be of different types like truck (heavy vehicle), Mini Van (medium range) and small family car etc. But each car has their specific property which is not common. For example engine. So as a designer of the car architecture you will put all the common properties of a car in a common class but instantiation of such a class does not have any meaning. So you will make it abstract and you will inforce to inherited the class to a particular vehicle type like below:-
public abstract class Utility{
}
public class Truck extends Utility{
}
public class MiniVan extends Utility{
}

- 5,521
- 2
- 11
- 17
So, why does JAVA prohibits from creating an object of the abstract class?
In the case where the abstract class has abstract methods, the reason for the restriction is that the abstract methods wouldn't work. For example, given this:
public abstract class Animal {
public abstract int getLegCount();
}
What should this statement do?
System.out.println("An animal has " + (new Animal()).getLegCount() + " legs");
It would be a bad thing to allow you to create instances that don't work.
In the case where there are no abstract methods, the restriction is for consistency. It wouldn't serve any real purpose in allowing you to instantiate some abstract methods. And in this case, if you want to instantiate the class, you should simply remove the abstract
keyword from the class declaration.

- 698,415
- 94
- 811
- 1,216
An abstract class is meant to provide a partial implementation that is shared by all of its direct and indirect subclasses. Normally, an abstract class will contain at least one abstract method. If so, such a method must be declared by every concrete subclass of the abstract class. Without an implementation for each abstract method, the abstract class cannot be fully realized.
Consider the example below.
public abstract class Shape {
public abstract void draw();
}
public class Circle extends Shape {
@Override
public void draw() {
// draw a circle
}
}
public class Square extends Shape {
@Override
public void draw() {
// draw a square
}
}
It wouldn't make sense to allow a Shape object to be instantiated, because if it doesn't know what type of shape it is, it doesn't know how to draw itself. The Shape class doesn't provide enough information for an instantiation to be meaningful.
The purpose of an abstract class is to reuse code and allow for different subclass types to be processed polymorphically through the abstract superclass reference.

- 26
- 2