Why can't we instantiate a abstract class in J

2020-02-07 19:38发布

I understand:

  1. Since Abstract class are nothing on its own i.e. vehicle that's why we want to create an object of an concrete implementation like Car,Bike etc.
  2. Constructor of an Abstract class gets called during object chaining.

  3. We can never directly create an object of Abstract class even if it contains a Constructor and all methods are implemented

why ? I am looking to understand from Compiler perspective, why Java forces these ?

Thanks

15条回答
你好瞎i
2楼-- · 2020-02-07 20:04

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.

查看更多
劫难
3楼-- · 2020-02-07 20:08

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

查看更多
甜甜的少女心
4楼-- · 2020-02-07 20:09

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

查看更多
放荡不羁爱自由
5楼-- · 2020-02-07 20:09

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.

查看更多
手持菜刀,她持情操
6楼-- · 2020-02-07 20:11

An abstract classs can NOT be instantiated by using new operator. Becuase an abstract may have abstract methods i.e. methods without any body (or implementation). Because an object can NOT have an abstract methods and JVM can NOT allocate memory of the abstract methods

查看更多
The star\"
7楼-- · 2020-02-07 20:12

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

查看更多
登录 后发表回答