levo

Objects, Classes and Data Structures

Overriding methods and run-time polymorphism

  • 3 things to know
  • AS Unit 1

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.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

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

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A null is the of one.
absencepresencetype

Blank Blitz makes facts automatic

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

AA null means no value has been recorded.
BZero and an empty string are values.
CA null is the absence of a value.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

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.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

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