What is the use of creating a constructor for an abstract class in Java?

前端 未结 8 2042
时光取名叫无心
时光取名叫无心 2020-12-03 18:50

I would like to know what purpose a constructor for an abstract class serves; as we do not instantiate abstract classes, why would we ever need such a constructor?

相关标签:
8条回答
  • 2020-12-03 19:13

    Constructors for abstract classes are used by subclasses (invoked from subclass constructors using super(params) ).

    You should make these constructors protected to make that clear.

    0 讨论(0)
  • 2020-12-03 19:16

    I agree, Constructors are created assuming there will be instances. If you have lot of common code you can think of creating a Constructor but it is much better to put it in a init() method.

    0 讨论(0)
  • 2020-12-03 19:18

    De-duplicating common knowledge/behaviour.

    E.g. a Car: all your cars will be constructed out of a body and four wheels and an engine. So you do this part of the construction in the constructor for the abstract Car class by calling functions like Body(), Wheel(int x), Engine(). Each particular car class will have their own implementation of Body(), Wheel() and Engine() - but they all will do the same steps to construct the car from them, so there is no need to duplicate those steps in each of those classes. In this case you implement that common behaviour in the ancestor.

    0 讨论(0)
  • 2020-12-03 19:19

    there will be times when you have some common initialization of instance variables that all the inheriting classes need to set up. You do instantiate an abstract class when you extend it and that concrete class has a constructor that will either supply the parameters to the constructor of the abstract class.

    0 讨论(0)
  • 2020-12-03 19:25

    You don't instantiate abstract classes but the constructor is invoked when a subclass is instantiated.

    The use could be to initialize common attributes ie.

    import java.util.List;
    import java.util.ArrayList;
    abstract class BaseClass {
        protected List list; // visible from subclasses
    
        public BaseClass() {
            System.out.println("to abstract...");
            // common initialization to all subclasses
            list = new ArrayList();
            list.add("a");
            list.add("a");
            list.add("a");
        }
    }
    
    class ConcreteClass extends BaseClass {
        public ConcreteClass(){
            // The list is initialized already
            System.out.println("now it is concrete and the list is: = "+ this.list );
    
    
        }
    }
    
    class TestAbstractClass {
        public static void main( String [] args ) {
            BaseClass instance = new ConcreteClass();
        }
    
    }
    

    Output

    $ java TestAbstractClass
    to abstract...
    now it is concrete and the list is: = [a, a, a]
    
    0 讨论(0)
  • 2020-12-03 19:26

    They can still be invoked by constructors of classes that inherit from that one, making code refactoring a good use for having a constructor in the abstract class.

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