Can an abstract class have a constructor? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

Can an abstract class have a constructor?

Can an abstract class have a constructor? If so, how can it be used and for what purposes?

7th Nov 2016, 7:23 PM
Redet Getachew
Redet Getachew - avatar
2 Respostas
+ 3
No. An abstract class has hat least one method which is declared, but not defined. The program does not know how the method exactly works. Now imagine the following: There is a constructor in an abstract class. This means that an instance of this class can be created. This would make sense - but what if you call the abstract method like myObject.myAbstractMethod()? The compiler/interpreter does not know what to do, because you never told him what to do. It just knows the name (myAbstractMethod()), but it does not know how the method actually works.
7th Nov 2016, 7:34 PM
Dominik Braun
Dominik Braun - avatar
- 1
Bro I have a doubt could you please make it clear to me...suppose i have abstact class with some fields and i have created a subclass of abstract class.so when i will initialize child class object parent constructor shoul be called first.so constructor is there for abstract classes
23rd Dec 2016, 9:43 PM
Vishall Kumar
Vishall Kumar - avatar