+ 2

In java, will there ever be a specific need to call a super class's constructor using super keyword?

In general, before a sub class is created, a super class is created for that subclass. While reading "super" keyword I came accross a sentence that denotes that it can be used to invoke the superclass's constructor. Can there be any example where we will need to explicitly use the super keyword to call the superclass's constructor. If anyone has an example, please do tell.. Thanks

13th Dec 2021, 11:19 AM
Ruchika Sehgal
Ruchika Sehgal - avatar
1 Réponse
+ 2
class Person { String name; Person(String name) { this.name=name; } } class Employee extends Person { String career; Employee( String name, String career) { super(name); this.career = career; } public static void main(String[] args) { Employee e = new Employee( "Joshua Bloch", "programmer"); System.out.println(e.name); } }
13th Dec 2021, 3:23 PM
zemiak