levo

Objects, Classes and Data Structures

Overriding methods and run-time polymorphism

  • 3 things to know
  • AS Unit 1
Start revising · £5/month

Overview

Method overriding and abstract method.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Explain what happens at run time when an overridden method is called through a base-class reference.

Key term

What does the annotation @Override do in Java?

Key term

What name is given to choosing the method version from the object's actual class while the program runs?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is method overriding?

    Model answer

    Method overriding is a subclass supplying its own version of a method it has inherited, keeping exactly the same name and parameter list so the new version replaces the old one for objects of that subclass.

  2. 2

    What must match between an inherited method and the method that overrides it?

    Model answer

    An overriding method must have the same name and the same parameter list as the inherited one, and its return type must be the same type or a subtype of it.

  3. 3

    What does the annotation @Override do in Java?

    Model answer

    The annotation @Override tells the compiler that the method is meant to replace an inherited one, so a mistyped name is reported as an error instead of quietly creating a brand new method.

  4. 4

    What is polymorphism?

    Model answer

    Polymorphism is the ability of one method call to run different code according to the actual class of the object the call is made on.

  5. 5

    A class Shape defines public double area() { return 0; }. A subclass Circle holds private double r = 2; and overrides area to return 3.14 * r * r. What does System.out.println(s.area()); print when s is declared as Shape s = new Circle(); ?

    Model answer

    The object is a Circle, so the Circle version of area runs and works out 3.14 multiplied by 2 and again by 2, giving 12.56, so the statement prints 12.56.

  6. 6

    Explain what happens at run time when an overridden method is called through a base-class reference.

    Model answer

    The declared type of the reference decides which method names the compiler will allow, but the actual class of the object decides which version executes, so the subclass version is the one that runs.

Other ways to work this

The same content, four other ways.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

Software and the Development Environment
Defining Data: Types, Variables and Classes
Program Control Structures
Objects, Classes and Data Structures

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

A null is the of one.
absencepresencetype

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Objects, Classes and Data Structures, on a schedule.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.