Method overriding
Method overriding, in object-oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. Some languages allow a programmer to prevent a method from being overridden.
Language-specific examples
Ada
provides method overriding by default.To favor early error detection,
it is possible to specify when a method
is expected to be actually overriding, or not. That will be checked by the compiler.
type T is new Controlled with......;
procedure Op;
type NT is new T with null record;
overriding -- overriding indicator
procedure Op;
overriding -- overriding indicator
procedure Op;
-- ^ compiler issues an error: subprogram "Op" is not overriding
C#
does support method overriding, but only if explicitly requested using the modifiers and or.abstract class Animal
class Cat : Animal
When overriding one method with another, the signatures of the two methods must be identical. In C#, class methods, indexers, properties and events can all be overridden.
Non-virtual or static methods cannot be overridden. The overridden base method must be virtual, abstract, or override.
In addition to the modifiers that are used for method overriding, C# allows the hiding of an inherited property or method. This is done using the same signature of a property or method but adding the modifier in front of it.
In the above example, hiding causes the following:
Cat cat = new Cat;
cat.Name = …; // accesses Cat.Name
cat.Eat; // calls Cat.Eat
cat.Go; // calls Cat.Go
.Name = …; // accesses Animal.Name!
.Eat; // calls Cat.Eat!
.Go; // calls Animal.Go!
C++
does not have the keyword that a subclass can use in Java to invoke a superclass version of a method that it wants to override. Instead, the name of the parent or base class is used followed by the scope resolution operator. For example, the following code presents two classes, the base class, and the derived class. overrides the class's method, so as also to print its height.- include
class Rectangle ;
//---------------------------------------------------------------------------
void Rectangle::Print const
//---------------------------------------------------------------------------
class Box : public Rectangle ;
//---------------------------------------------------------------------------
// Print method of derived class.
void Box::Print const
The method in class, by invoking the parent version of method, is also able to output the private variables and of the base class. Otherwise, these variables are inaccessible to.
The following statements will instantiate objects of type and, and call their respective methods:
int main
In C++11, similar to Java, a method that is declared
final
in the super class cannot be overridden; also, a method can be declared override
to make the compiler check that it overrides a method in the base class.Delphi
In Delphi, method overriding is done with the directive override, but only if a method was marked with the dynamic or virtual directives.The inherited reserved word must be called when you want to call super-class behavior
type
TRectangle = class
private
FLength: Double;
FWidth: Double;
public
property Length read FLength write FLength;
property Width read FWidth write FWidth;
procedure Print; virtual;
end;
TBox = class
public
procedure Print; override;
end;
Eiffel
In Eiffel, feature redefinition is analogous to method overriding in C++ and Java. Redefinition is one of three forms of feature adaptation classified as redeclaration. Redeclaration also covers effecting, in which an implementation is provided for a feature which was deferred in the parent class, and undefinition, in which a feature that was effective in the parent becomes deferred again in the heir class. When a feature is redefined, the feature name is kept by the heir class, but properties of the feature such as its signature, contract, and/or implementation will be different in the heir. If the original feature in the parent class, called the heir feature's precursor, is effective, then the redefined feature in the heir will be effective. If the precursor is deferred, the feature in the heir will be deferred.The intent to redefine a feature, as in the example below, must be explicitly declared in the clause of the heir class.
class
THOUGHT
feature
message
-- Display thought message
do
end
end
class
ADVICE
inherit
THOUGHT
redefine
message
end
feature
message
-- Precursor
do
end
end
In class the feature is given an implementation that differs from that of its precursor in class.
Consider a class which uses instances for both and :
class
APPLICATION
create
make
feature
make
-- Run application.
do
.message;
.message
end
end
When instantiated, class produces the following output:
I feel like I am diagonally parked in a parallel universe.
Warning: Dates in calendar are closer than they appear.
Within a redefined feature, access to the feature's precursor can be gained by using the language keyword. Assume the implementation of is altered as follows:
message
-- Precursor
do
Precursor
end
Invocation of the feature now includes the execution of, and produces the following output:
Warning: Dates in calendar are closer than they appear.
I feel like I am diagonally parked in a parallel universe.
Java
In Java, when a subclass contains a method that overrides a method of the superclass, it can also invoke the superclass method by using the keyword .Example:
class Thought
public class Advice extends Thought
Class represents the superclass and implements a method call. The subclass called inherits every method that could be in the class. However, class overrides the method, replacing its functionality from.
Thought parking = new Thought;
parking.message; // Prints "I feel like I am diagonally parked in a parallel universe."
Thought dates = new Advice; // Polymorphism
dates.message; // Prints "Warning: Dates in calendar are closer than they appear."
The reference can be
public class Advice extends Thought
Kotlin
In Kotlin we can simply override a function like this:note that function must be open
fun main
open class test
class test2 : test
Python
In Python, when a subclass contains a method that overrides a method of the superclass, you can also call the superclass method by calling instead of.Example:
class Thought:
def __init__ -> None:
def message -> None:
class Advice:
def __init__ -> None:
super.__init__
def message -> None:
super.message
t = Thought
- "I'm a new object of type Thought!"
- "I feel like I am diagonally parked in a parallel universe.
- "I'm a new object of type Thought!"
- "Warning: Dates in calendar are closer than they appear"
- "I feel like I am diagonally parked in a parallel universe.
- ------------------
- Introspection:
- True
- True
- True
Ruby
In Ruby when a subclass contains a method that overrides a method of the superclass, you can also call the superclass method by calling super in that overridden method. You can use alias if you would like to keep the overridden method available outside of the overriding method as shown with 'super_message' below.Example:
class Thought
def message
puts "I feel like I am diagonally parked in a parallel universe."
end
end
class Advice < Thought
alias :super_message :message
def message
puts "Warning: Dates in calendar are closer than they appear"
super
end
end