ArticleslgStudy

computer science

Method overriding

Method overriding is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Method overriding rather than just read about it. In short: 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. In addition to providing data-driven algorithm-determined parameters across virtual network interfaces, it also allows for a specific type of polymorphism (subtyping).

Method overriding — main illustration
Method overriding — illustration

Key takeaways

  • Method overriding belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Method overriding to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Method overriding from memory before moving on to harder problems.

Reference excerpt

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. In addition to providing data-driven algorithm-determined parameters across virtual network interfaces, it also allows for a specific type of polymorphism (subtyping). The implementation in the subclass overrides (replaces) 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. This helps in preventing problems associated with differential relay analytics which would otherwise rely on a framework in which method overriding might be obviated. Some languages allow a programmer to prevent a method from being overridden.

Language-specific examples

Ada Ada provides method overriding by default. To favor early error detection (e.g. a misspelling), it is possible to specify when a method is expected to be actually overriding, or not. That will be checked by the compiler.

C# C# does support method overriding, but only if explicitly requested using the modifiers override and virtual or abstract.

When overriding one method with another, the signatures of the two methods must be identical (and with same visibility). 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 new in front of it. In the above example, hiding causes the following:

C++ C++ does not have the keyword super like Java that can be used in a subclass method to access the superclass version of a method 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 Rectangle, and the derived class Box. Box overrides the Rectangle class's display method, so as also to print its height.

The method display in class Box, by invoking the parent version of method display, is also able to output the private variables length and width of the base class. Otherwise, these variables are inaccessible to Box. The following statements will instantiate objects of type Rectangle and Box, and call their respective display methods:

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

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 (abstract) in the parent class, and undefinition, in which a feature that was effective (concrete) 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 (respecting restrictions for preconditions and postconditions), 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 message in the example below, must be explicitly declared in the inherit clause of the heir class.

In class ADVICE the feature message is given an implementation that differs from that of its precursor in class THOUGHT. Consider a class which uses instances for both THOUGHT and ADVICE:

When instantiated, class APPLICATION produces the following output:

Within a redefined feature, access to the feature's precursor can be gained by using the language keyword Precursor. Assume the implementation of {ADVICE}.message is altered as follows:

Invocation of the feature now includes the execution of {THOUGHT}.message, and produces the following output:

Java In Java, when a subclass contains a method with the same signature (name and parameter types) as a method in its superclass, then the subclass's method overrides that of the superclass. Unlike C++ and C#, Java does not have an override keyword, but provides the annotation @Override to indicate that a method is being overriden. It is not necessary, but can be helpful to the compiler. For example:

Class Thought represents the superclass and implements a method call message(). The subclass called Advice inherits every method that could be in the Thought class. Class Advice overrides the method message(), replacing its functionality from Thought.

When a subclass contains a method that overrides a method of the superclass, then that (superclass's) overridden method can be explicitly invoked from within a subclass's method by using the keyword super. (It cannot be explicitly invoked from any method belongings to a class that is unrelated to the superclass.) The super reference can be

There are methods that a subclass cannot override. For example, in Java, a method that is declared final in the super class cannot be overridden. Methods that are declared private or static cannot be overridden either because they are implicitly final. It is also impossible for a class that is declared final to become a super class.

Kotlin In Kotlin we can simply override a function like this (note that the function must be open):

… excerpt ends here. Continue reading the full article.

Illustrations

Method overriding: Illustration
Illustration
Method overriding illustration

Worked examples

Example 1 — a first encounter with Method overriding

Start with the simplest possible case. Write down what Method overriding claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Method overriding before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Method overriding ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Method overriding

In research
Method overriding appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Method overriding in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Method overriding is common in secondary-school and first-year university syllabi. It links to neighbouring topics Method (computer programming), so understanding it makes those chapters shorter.
In everyday life
Look for Method overriding outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Method overriding” →

How to study Method overriding in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Method overriding means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Method overriding out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Method overriding in simple terms?

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. In addition to providing data-driven algorithm-determined parameters…

Why does Method overriding matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Method overriding?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Method overriding.

Tags

  • Method (computer programming)

Keep exploring