Constructor can be override or not ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Constructor can be override or not ??

8th Feb 2017, 4:52 PM
Taufique Sekh
Taufique Sekh - avatar
6 Answers
+ 4
No. Why do you want to do that? Keep in mind that the constructor of the super class is always invoked, when the extended class is constructed. But constructors can be overloaded.
8th Feb 2017, 5:03 PM
Tashi N
Tashi N - avatar
+ 2
// You may test it public class A { A(){ System.out.println ( "A()") ; } public void method(){ System.out.println ( "method() of A") ; } } public class B extends A { B(){ System.out.println ( "B()") ; } public void method(){ System.out.println ( "method() of B" ) ; } } public class Program { public static void main(String[] args) { A a = new A (); System.out.println ("----------"); A b = new B (); System.out.println ("----------"); a.method (); System.out.println ("----------"); b.method (); } }
8th Feb 2017, 5:40 PM
K.C. Leung
K.C. Leung - avatar
+ 1
Simply put no. A constructor is a special method used to initialize a class object. Unlike the other methods of a class that you inherit from it can not be overridden. If you were able to do so, it would most likely lead to breaking the parent class, causing a loss of funtionality, and causing errors.
8th Feb 2017, 5:03 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
No, a java constructor cannot be overwritten.
8th Feb 2017, 10:41 PM
Brant Roun
Brant Roun - avatar
0
No
17th Dec 2019, 5:00 AM
Sonic
Sonic - avatar
- 1
Yep constructor can be overloaded but can't be override in java don't know other languages
8th Feb 2017, 5:06 PM
Decodeworms Olamilekan
Decodeworms Olamilekan - avatar