super Keyword -2
This section covers using super keyword to call the parent class constructor
Last updated
This section covers using super keyword to call the parent class constructor
Last updated
Whenever you call a child class constructor , there is an implicit call to the parent class constructor . Generally without any modification , the default/ no-argument constructor of the parent class get executed
void main() {
//super keyword -2
// To call parent class constructor
var manager1 = Manager();
manager1.id =1;
manager1.firstname="John";
manager1.lastname="Doe";
manager1.dayworked=30;
manager1.project ="Alpha";
}
class Employee {
//property
int id;
int dayworked;
String firstname;
String lastname;
// Methods
String fullname() {
return this.firstname + " " + this.lastname;
}
int salary(int perDaywage) {
return (this.dayworked * perDaywage);
}
// Constructor
Employee(){
print(" Inside Employee Constructor");
}
}
// Manager Class
class Manager extends Employee {
//property
String project;
//Method
String manageproject(){
return "Managing project " + " " + this.project;
}
// Constructor
Manager():super(){
print(" Inside Manager constructor");
}
}
You can pass argument inside the parent class constructor via passing an argument
inside "super( )"
void main() {
//super keyword -2
// To call parent class constructor
var manager1 = Manager();
manager1.id =1;
manager1.firstname="John";
manager1.lastname="Doe";
manager1.dayworked=30;
manager1.project ="Alpha";
}
class Employee {
//property
int id;
int dayworked;
String firstname;
String lastname;
// Methods
String fullname() {
return this.firstname + " " + this.lastname;
}
int salary(int perDaywage) {
return (this.dayworked * perDaywage);
}
// Constructor
Employee(String input){
print(input);
print(" Inside Employee Constructor");
}
}
// Manager Class
class Manager extends Employee {
//property
String project;
//Method
String manageproject(){
return "Managing project " + " " + this.project;
}
// Constructor
Manager():super("foo"){
print(" Inside Manager constructor");
}
}
You can pass an argument inside the child class constructor , and that argument can be passed into the parent class constructor
void main() {
//super keyword -2
// To call parent class constructor
var manager1 = Manager("baz");
manager1.id =1;
manager1.firstname="John";
manager1.lastname="Doe";
manager1.dayworked=30;
manager1.project ="Alpha";
}
class Employee {
//property
int id;
int dayworked;
String firstname;
String lastname;
// Methods
String fullname() {
return this.firstname + " " + this.lastname;
}
int salary(int perDaywage) {
return (this.dayworked * perDaywage);
}
// Constructor
Employee(String input){
print(input);
print(" Inside Employee Constructor");
}
}
// Manager Class
class Manager extends Employee {
//property
String project;
//Method
String manageproject(){
return "Managing project " + " " + this.project;
}
// Constructor
Manager(String input):super(input){
print(" Inside Manager constructor");
}
}
void main() {
//super keyword -2
var manager1 = Manager("Alpha",1,"John","Doe",12);
// manager1.id =1;
// manager1.firstname="John";
// manager1.lastname="Doe";
// manager1.dayworked=30;
// manager1.project ="Alpha";
print(manager1.id);
print(manager1.firstname);
print(manager1.lastname);
print(manager1.dayworked);
print(manager1.project);
}
class Employee {
//property
int id;
int dayworked;
String firstname;
String lastname;
// Methods
String fullname() {
return this.firstname + " " + this.lastname;
}
int salary(int perDaywage) {
return (this.dayworked * perDaywage);
}
// Constructor
Employee(){
print(" Inside Employee Constructor");
}
}
// Manager Class
class Manager extends Employee {
//property
String project;
//Method
String manageproject(){
return "Managing project " + " " + this.project;
}
// Constructor
Manager(String project,int id,String firstname , String lastname, int dayworked ):super(){
this.project= project;
this.id = id;
this.firstname =firstname;
this.lastname = lastname;
this.dayworked = dayworked;
print(" Inside Manager constructor");
}
}
void main() {
//super keyword -2
var manager1 = Manager("Alpha",1,"John","Doe",12);
// manager1.id =1;
// manager1.firstname="John";
// manager1.lastname="Doe";
// manager1.dayworked=30;
// manager1.project ="Alpha";
print(manager1.id);
print(manager1.firstname);
print(manager1.lastname);
print(manager1.dayworked);
print(manager1.project);
}
class Employee {
//property
int id;
int dayworked;
String firstname;
String lastname;
// Methods
String fullname() {
return this.firstname + " " + this.lastname;
}
int salary(int perDaywage) {
return (this.dayworked * perDaywage);
}
// Constructor
Employee(int id,String firstname , String lastname, int dayworked){
this.id = id;
this.firstname =firstname;
this.lastname = lastname;
this.dayworked = dayworked;
print(" Inside Employee Constructor");
}
}
// Manager Class
class Manager extends Employee {
//property
String project;
//Method
String manageproject(){
return "Managing project " + " " + this.project;
}
// Constructor
Manager(String project,int id,String firstname , String lastname, int dayworked ):super(id,firstname,lastname,dayworked){
this.project = project;
print(" Inside Manager constructor");
}
}
void main() {
//super keyword -2
var manager1 = Manager("Alpha",1,"John","Doe",12);
// manager1.id =1;
// manager1.firstname="John";
// manager1.lastname="Doe";
// manager1.dayworked=30;
// manager1.project ="Alpha";
print(manager1.id);
print(manager1.firstname);
print(manager1.lastname);
print(manager1.dayworked);
print(manager1.project);
}
class Employee {
//property
int id;
int dayworked;
String firstname;
String lastname;
// Methods
String fullname() {
return this.firstname + " " + this.lastname;
}
int salary(int perDaywage) {
return (this.dayworked * perDaywage);
}
// Constructor
Employee(int id,String firstname , String lastname, int dayworked){
this.id = id;
this.firstname =firstname;
this.lastname = lastname;
this.dayworked = dayworked;
print(" Inside Employee Constructor");
}
}
// Manager Class
class Manager extends Employee {
//property
String project;
//Method
String manageproject(){
return "Managing project " + " " + this.project;
}
// Constructor
Manager(this.project,int id,String firstname , String lastname, int dayworked ):super(id,firstname,lastname,dayworked){
print(" Inside Manager constructor");
}
}
void main() {
//super keyword -2
var manager1 = Manager("Alpha",1,"John","Doe",12);
// manager1.id =1;
// manager1.firstname="John";
// manager1.lastname="Doe";
// manager1.dayworked=30;
// manager1.project ="Alpha";
print(manager1.id);
print(manager1.firstname);
print(manager1.lastname);
print(manager1.dayworked);
print(manager1.project);
}
class Employee {
//property
int id;
int dayworked;
String firstname;
String lastname;
// Methods
String fullname() {
return this.firstname + " " + this.lastname;
}
int salary(int perDaywage) {
return (this.dayworked * perDaywage);
}
// Constructor
Employee(this.id,this.firstname ,this.lastname, this.dayworked){
print(" Inside Employee Constructor");
}
}
// Manager Class
class Manager extends Employee {
//property
String project;
//Method
String manageproject(){
return "Managing project " + " " + this.project;
}
// Constructor
Manager(this.project,int id,String firstname , String lastname, int dayworked ):super(id,firstname,lastname,dayworked){
print(" Inside Manager constructor");
}
}
void main() {
//super keyword -2
var manager1 = Manager("Alpha",1,"John","Doe",12);
// manager1.id =1;
// manager1.firstname="John";
// manager1.lastname="Doe";
// manager1.dayworked=30;
// manager1.project ="Alpha";
print(manager1.id);
print(manager1.firstname);
print(manager1.lastname);
print(manager1.dayworked);
print(manager1.project);
}
class Employee {
//property
int id;
int dayworked;
String firstname;
String lastname;
// Methods
String fullname() {
return this.firstname + " " + this.lastname;
}
int salary(int perDaywage) {
return (this.dayworked * perDaywage);
}
// Constructor
Employee(this.id,this.firstname ,this.lastname, this.dayworked);
}
// Manager Class
class Manager extends Employee {
//property
String project;
//Method
String manageproject(){
return "Managing project " + " " + this.project;
}
// Constructor
Manager(this.project,int id,String firstname , String lastname, int dayworked ):super(id,firstname,lastname,dayworked);
}
void main() {
//super keyword -2
var manager1 = Manager(project:"Alpha",id:1,firstname:"John",lastname:"Doe",dayworked:12);
// manager1.id =1;
// manager1.firstname="John";
// manager1.lastname="Doe";
// manager1.dayworked=30;
// manager1.project ="Alpha";
print(manager1.id);
print(manager1.firstname);
print(manager1.lastname);
print(manager1.dayworked);
print(manager1.project);
}
class Employee {
//property
int id;
int dayworked;
String firstname;
String lastname;
// Methods
String fullname() {
return this.firstname + " " + this.lastname;
}
int salary(int perDaywage) {
return (this.dayworked * perDaywage);
}
// Constructor
Employee({this.id,this.firstname ,this.lastname, this.dayworked});
}
// Manager Class
class Manager extends Employee {
//property
String project;
//Method
String manageproject(){
return "Managing project " + " " + this.project;
}
// Constructor
Manager({this.project,int id,String firstname , String lastname, int dayworked }):super(id:id,firstname:firstname,lastname:lastname,dayworked:dayworked);
}
void main() {
//super keyword -2
var manager1 = Manager.mynamedcons1(project:"Beta",id:1,firstname:"John",lastname:"Doe",dayworked:12);
// manager1.id =1;
// manager1.firstname="John";
// manager1.lastname="Doe";
// manager1.dayworked=30;
// manager1.project ="Alpha";
print(manager1.id);
print(manager1.firstname);
print(manager1.lastname);
print(manager1.dayworked);
print(manager1.project);
}
class Employee {
//property
int id;
int dayworked;
String firstname;
String lastname;
// Methods
String fullname() {
return this.firstname + " " + this.lastname;
}
int salary(int perDaywage) {
return (this.dayworked * perDaywage);
}
// Constructor
Employee({this.id,this.firstname ,this.lastname, this.dayworked});
Employee.mynamedcons2({this.id,this.firstname ,this.lastname, this.dayworked});
}
// Manager Class
class Manager extends Employee {
//property
String project;
//Method
String manageproject(){
return "Managing project " + " " + this.project;
}
// Constructor
Manager({this.project,int id,String firstname , String lastname, int dayworked }):super(id:id,firstname:firstname,lastname:lastname,dayworked:dayworked);
Manager.mynamedcons1({this.project,int id,String firstname , String lastname, int dayworked }):super.mynamedcons2(id:id,firstname:firstname,lastname:lastname,dayworked:dayworked);
}