According to docs
The annotation @override
marks an instance member as overriding a superclass member with the same name.
Example
class A {
void foo() {
print("Class A");
}
}
class B extends A {}
class C extends A {
@override foo() {
print("Class C");
}
}
void main() {
A a = A();
B b = B();
C c = C();
a.foo();
b.foo();
c.foo();
}
The Output is:
Class A
Class A
Class C
What happening is B extends A
and hence inherits all the methods of A
. Hence when b.foo()
is called it calls method from its parent class.
But in C
it has foo
with override
keyword which means it will override foo
declaration in its parent class.