Overriding Methods

 ///Subclasses can override instance methods (including operators), getters, and setters.

///You can use the @override annotation to indicate that you are intentionally overriding a member.

void main() {
var obj = B();
obj.display();
}
class A {

@override
void display() {
print("CLASS A");
}
}
class B extends A {

@override
void display() {
super.display(); // Accessing Super class(parent class) method in sub class(child class) using "super" keyword
print("CLASS B");
}
}

///Advantages of super keyword:
// It can be used to access the data members of parent class when both parent and child have member with same name.
// It is used to prevent overriding the parent method.
// It can be used to call parameterized constructor of parent class.
///Syntax:
// // To access parent class variables
// super.variable_name;
// // To access parent class method
// super.method_name();

Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

Pagination First Practical in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter