In this tutorial you will learn about the Dart Super Constructor and its application with practical example.
Dart Super Constructor
A subclass inherits the variables and methods from its superclass, but the superclass constructor is not inherited in the subclass. The superclass constructors can only be invoked from subclass constructors using the super() constructor. The super() constructor allows a subclass constructor to explicitly call the no-arg and parameterized constructor of superclass.
Syntax:-
1 2 |
SubClassConstructor():super(){ } |
Implicit super:-
When an object of subclass is created using the new keyword, it invokes the subclass constructor which implicitly invokes the parent class’s default (no-arg constructor) constructor. If no parameters are defined in a superclass constructor, you can bypass the call to super() in your subclass.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class ParentClass { ParentClass(){ print("Constructor of Parent Class"); } } class SubClass extends ParentClass { SubClass(){ /* Compiler implicitly adds super() here as the * first statement of this constructor. */ print("Constructor of Sub Class"); } display(){ print("Hello World!"); } } void main(){ print("W3Adda - Dart Implicit Super Constructor Example."); /* Creating object using default constructor. This * will invoke sub class constructor, which will * invoke parent class constructor */ SubClass obj= new SubClass(); //Calling sub class method obj.display(); } |
Output:-
Explicit super:-
If the constructor in superclass takes arguments then we are required to use parameterized super() method in subclass constructor to invoke parameterized constructor of superclass and pass the requested arguments.
Example 1:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class ParentClass { //parameterized constructor ParentClass(String str){ print("Parameterized Cconstructor Of Parent Class"); print(str); } } class SubClass extends ParentClass { SubClass() : super("Hello from Parent Class"){ print("Constructor of Sub Class"); } display(){ print("Hello World!"); } } void main(){ print("W3Adda - Dart Parameterized Super Constructor Example."); SubClass obj= new SubClass(); //Calling sub class method obj.display(); } |
Output 1:-
Example 2:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Person { String name; int age; Person(String pName, int pAge){ this.name = pName; this.age = pAge; } showPerInfo(){ print("Person's Name Is : ${name}"); print("Person's Age Is : ${age}"); } } class Employee extends Person { int empSalary; Employee(String eName, int eAge , int eSalary) : super(eName, eAge){ this.empSalary = eSalary; } showEmpInfo(){ print("Employee Name Is : ${name}"); print("Employee Age Is : ${age}"); print("Employee Salary Is : ${empSalary}"); } } main() { print("W3Adda - Dart Super Constructor Example."); Person p = new Person("John", 25); p.showPerInfo(); Employee e = new Employee("Keith", 30, 25000); e.showEmpInfo(); } |
Output:-