Can an abstract class have a constructor?

前端 未结 22 2178
甜味超标
甜味超标 2020-11-22 05:25

Can an abstract class have a constructor?

If so, how can it be used and for what purposes?

相关标签:
22条回答
  • 2020-11-22 06:07

    Yes, an Abstract Class can have a Constructor. You Can Overload as many Constructor as you want in an Abstract Class. These Contractors Can be used to Initialized the initial state of the Objects Extending the Abstract Class. As we know we can't make an object of an Abstract Class because Objects are Created by the "new" keywords and not by the constructors...they are there for only initializing the state of the subclass Objects.

    0 讨论(0)
  • 2020-11-22 06:08

    In a concrete class, declaration of a constructor for a concrete type Fnord effectively exposes two things:

    • A means by which code can request the creation of an instance of Fnord

    • A means by which an instance of a type derived from Fnord which is under construction can request that all base-class features be initialized.

    While there should perhaps be a means by which these two abilities could be controlled separately, for every concrete type one definition will enable both. Although the first ability is not meaningful for an abstract class, the second ability is just as meaningful for an abstract class as it would be for any other, and thus its declaration is just as necessary and useful.

    0 讨论(0)
  • 2020-11-22 06:10

    Since an abstract class can have variables of all access modifiers, they have to be initialized to default values, so constructor is necessary. As you instantiate the child class, a constructor of an abstract class is invoked and variables are initialized.

    On the contrary, an interface does contain only constant variables means they are already initialized. So interface doesn't need a constructor.

    0 讨论(0)
  • 2020-11-22 06:11

    Of Course, abstract class can have a constructor.Generally class constructor is used to initialise fields.So, an abstract class constructor is used to initialise fields of the abstract class. You would provide a constructor for an abstract class if you want to initialise certain fields of the abstract class before the instantiation of a child-class takes place. An abstract class constructor can also be used to execute code that is relevant for every child class. This prevents code duplication.

    We cannot create an instance of an abstract class,But we can create instances of classes those are derived from the abstract class. So, when an instance of derived class is created, the parent abstract class constructor is automatically called.

    Reference :This Article

    0 讨论(0)
  • 2020-11-22 06:12

    Yes! Abstract classes can have constructors!

    Yes, when we define a class to be an Abstract Class it cannot be instantiated but that does not mean an Abstract class cannot have a constructor. Each abstract class must have a concrete subclass which will implement the abstract methods of that abstract class.

    When we create an object of any subclass all the constructors in the corresponding inheritance tree are invoked in the top to bottom approach. The same case applies to abstract classes. Though we cannot create an object of an abstract class, when we create an object of a class which is concrete and subclass of the abstract class, the constructor of the abstract class is automatically invoked. Hence we can have a constructor in abstract classes.

    Note: A non-abstract class cannot have abstract methods but an abstract class can have a non-abstract method. Reason is similar to that of constructors, difference being instead of getting invoked automatically we can call super(). Also, there is nothing like an abstract constructor as it makes no sense at all.

    0 讨论(0)
  • 2020-11-22 06:15

    An abstract class can have a constructor BUT you can not create an object of abstract class so how do you use that constructor?

    Thing is when you inherit that abstract class in your subclass you can pass values to its(abstract's) constructor through super(value) method in your subclass and no you don't inherit a constructor.

    so using super you can pass values in a constructor of the abstract class and as far as I remember it has to be the first statement in your method or constructor.

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