17

I understand:

  1. Since an abstract class is nothing on its own, e.g. vehicle, we want to create an object of an concrete implementation, like Car, Bike, etc.
  2. The constructor of an abstract class gets called during object chaining.
  3. We can never directly create an object of an abstract class, even if it contains a constructor and all methods are implemented.

But from the compiler's perspective, why does Java enforce these rules?

ruakh
  • 175,680
  • 26
  • 273
  • 307
sandejai
  • 931
  • 1
  • 15
  • 22
  • 2
    If the author had wanted you to be able to instantiate it, they could have made it non-abstract. The fact that they didn't -- even if they did implement all methods -- is a software architecture choice; they're telling you that they don't believe using the default implementation makes sense. Ask them why, or accept it and create a subclass. – keshlam Feb 10 '14 at 15:30
  • You can't do it because the author marked the class as `abstract`, and that is part of the meaning of the word in Java. – user207421 Apr 24 '16 at 02:47
  • 1
    best answer http://stackoverflow.com/questions/13670991/interview-can-we-instantiate-abstract-class – Raymond Chenon Jul 19 '16 at 22:28
  • Because the Java language defines it so. – NomadMaker May 20 '20 at 19:54
  • @Raymond Chenon In that answer, the abstract class is not instantiated. An anonymous subclass of the abstract class is instantiated. – NomadMaker May 20 '20 at 19:56

16 Answers16

31

An abstract class is not complete! The author marked it abstract to tell you that some implementation is missing in the code. The author has done some of the work, but you must fill in some bits yourself to get it working. The abstract keyword ensures that no one would accidentally initiate this incomplete class.

Think of repairing a car. Someone has removed the brake pads and is going to replace them in the next day. Now, to prevent someone accidentally driving this car(which has no brakes installed), the mechanic installs a lock on the steering wheel. It's a fail-safe measure.

kevin
  • 2,196
  • 1
  • 20
  • 24
12

This is not a technical limitation, rather (as you have pointed out) a logical one. Java (and many other languages) enforce various rules not because they are impossible to break, but because this is an intentional part of the language.

Eli Iser
  • 2,788
  • 1
  • 19
  • 29
  • I agree. Some objects just do not exist, they are 'abstract' terms. – kboom Feb 10 '14 at 15:27
  • @eli iser : Thanks, I Guess its more like a rule, why it's because of the conceptual design, hence making compiler to force the error... – sandejai Feb 10 '14 at 15:50
7

rocketboy shows some mechanistic reasons, but there's a conceptual reason.

An Abstract class represents an abstract concept. Take your vehicle example. You cannot build a vehicle that is not something more specific. You can have a set of vehicles, that could be made of 2004 corolla's and '98 ford escorts and 1984 cs36 (a kind of yacht), a mark 4 firefly class mid-range bulk transport(the one with the stabilizers), you can take any one of those individually and call them a vehicle but you cannot have something that is only a vehicle and not one of those or some other specific type of vehicle.

Abstract classes represent such abstract concepts as vehicle. Hence the idea of instantiating one is non-sensical because to actually instantiate it you need to know what you're instantiating.

Taylor
  • 3,942
  • 2
  • 20
  • 33
  • 2
    Thanks, I Guess its more like a rule, why it's because of the conceptual design, hence making compiler to force the error. – sandejai Feb 10 '14 at 15:49
  • 1
    In addition to representing a concept that it makes no sense to instantiate (like a 'vehicle' in the example given in this answer), it allows the program designer to specify things about that concept that are required, therefore assisting programmers that come afterward to understand and extend the Abstract class. AFAIK, there is no requirement that any such concept, be an abstract class -- the 'vehicle' or whatever could be a superclass and allow instantiation, IF the program designer thought that was the best way to implement it. But the language designers allow you to do it this way. – arcy May 20 '20 at 18:25
6

An abstract class can't be instantiated by using new operator. Because an abstract class may have abstract methods i.e. methods without any implementation. Because an object can't have abstract methods, JVM can't allocate memory of these objects abstract methods

Vipul Verma
  • 115
  • 1
  • 7
3

Because an Abstract Class is a skeleton structure(an incomplete construct if you may), hence the term Abstract.

abstract class Person(){
   abstract void Speak();
}

Means every Person must speak. That means every person should know how to speak (implement the speak()). new Person() cannot have that, so it is not allowed.

rocketboy
  • 9,573
  • 2
  • 34
  • 36
2

abstract it self tells : existing in thought or as an idea but not having a physical or concrete existence. In java term , abstract keyword definition , if abstract comes before a class name, then JDK tells to JVM about discard that class object initiation. It's correct, abstract class can have multiple constructor but only for constructor chaining.

Rudra21
  • 242
  • 3
  • 13
1

You can't instantiate an interface or an abstract class because it would defy the object oriented model. Read more

Community
  • 1
  • 1
Sunil Singh Bora
  • 771
  • 9
  • 24
1

What I understand that Abstract classes may contain abstract (empty without implementation) methods. If we instantiate an object and call the empty method, It's not going to work and may cause problem, hence compiler forces this RULE. any further in-sighter ?

sandejai
  • 931
  • 1
  • 15
  • 22
1

You CAN instantiate an abstract class. You only need to provide a concrete subclass.

1

In java, everything is represented as an object except a few cases. When you are about to instantiate the class (A blueprint), you must know the exact size of the class member variables, member methods, and the constructors. Since some/all of the methods are abstract, and JVM doesn't know the size. Hence allocating memory is useless.

But We can implement something to overcome this problem If we take the memory allocation part away from the JVM and assign this ownership to the USER.

Ajay Kumar
  • 586
  • 5
  • 21
0

You can't instantiate abstract class because it's just to give a structure your class which is extending it.

And if you want to initiate your class, then why you want to define it as abstract?

Ankita P.
  • 478
  • 10
  • 21
0

Because Java restricted it that's why we can not instantiated the abstract class. Because in general scenario abstract means incomplete so we can not make of object of incomplete things.We have to provide the complete implementation of an abstract class in a concrete class. But we cannot create the object of an abstract class.

dhS
  • 3,739
  • 5
  • 26
  • 55
Bharti Rawat
  • 1,949
  • 21
  • 32
0

very simple reason jvm playing to restrict us to instantiate abstract class and interface.

Assume compiler allow us to instantiate both ok.

So suppose my abstract class contains 5 abstract method means only method prototype no method body.

So as we know every method call creating a separate stack in jvm Java stack area That memory allocation happened based on method structure and after execute that stack is destroying right.

So if my method is not contains any body means how can jvm predict memory to allocate that method

Second if no body means no method execution so never it will destroy from your Java stack area there may be a chance u can get memoryout error.

So to consider these 2 case compiler restrict us to instantiate both interface and abstract class

naila naseem
  • 577
  • 3
  • 12
  • 21
0

Because an abstract class is an incomplete class (incomplete in the sense it contains abstract methods without body and output) we cannot create an instance or object; the same way you say for an interface.

Sonu patel
  • 353
  • 1
  • 8
-1

Well actually you can - but only if you implement any methods that have been declared abstract or left out.

/**
 * A classic adaptor pattern.
 *
 * @param <P>
 * @param <Q>
 */
public static interface Adapter<P, Q> {

  public Q adapt(P p);

}

/**
 * An I walks an iterator of one type but delivers items of a different type.
 *
 * Please fill in the `next()` method. Use an Adaptor for convenience.
 *
 * @param <S>
 * @param <T>
 */
public abstract static class I<S, T> implements Iterator<T> {

  protected final Iterator<S> it;

  public I(Iterator<S> it) {
    this.it = it;
  }

  @Override
  public boolean hasNext() {
    return it.hasNext();
  }

  @Override
  public void remove() {
    it.remove();
  }

}

/**
 * Use an adaptor to transform one type into another.
 *
 * @param <S>
 * @param <T>
 */
public static class IA<S, T> extends I<S, T> {

  private final Adapter<S, T> adaptor;

  public IA(Iterator<S> it, Adapter<S, T> adaptor) {
    super(it);
    this.adaptor = adaptor;
  }

  @Override
  public T next() {
    // Implement the abstract method on-the-fly.
    return adaptor.adapt(it.next());
  }

}

Added

The IA class instantiates an object of the I abstract class and implements the next method that was missing from the I class. You are actually creating an object of an anonymous that implements the implied abstract method.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
-1

The reason why Java doesnt allows an abstract class to be instantiated is logical. We haven't given the definition of a method and therefore, if it had allowd the creation of the object, there was no return address to pop the function from the stack and we get thus, stuck. Thus, its logical only not to allow the object instattion.