Why can't we instantiate an abstract class in Java?

前端 未结 16 972
挽巷
挽巷 2020-12-08 05:36

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.
相关标签:
16条回答
  • 2020-12-08 05:59

    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.

    0 讨论(0)
  • 2020-12-08 06:01

    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.

    0 讨论(0)
  • 2020-12-08 06:03

    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.

    0 讨论(0)
  • 2020-12-08 06:03

    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?

    0 讨论(0)
  • 2020-12-08 06:04

    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

    0 讨论(0)
  • 2020-12-08 06:04

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

    0 讨论(0)
提交回复
热议问题