+ 3

Do subclasses inherit the constructors of super class?

18th Jan 2017, 3:36 AM
Deep chand
Deep chand - avatar
2 Respostas
+ 3
Taken as is from the course material on inheritance, 3rd slide : Constructors are not member methods, and so are not inherited by subclasses. However, the constructor of the superclass is called when the subclass is instantiated. :)
18th Jan 2017, 3:46 AM
Sotiris Philippou
Sotiris Philippou - avatar
+ 2
If you want to call the constructor of the superclass explicitly, you have to do it in the subclasses constructor with super(); ! It also has to be the first thing you do in the subclasses constructor: class A { private string name; public A(string name) { this.name = name; } } class B extends A { public B(string name) { super(name); //do specific b constructor stuff here } } class Test { public static void main(String[] args) { B b = new B("i am an object of b");
18th Jan 2017, 7:28 AM
Jannick