Polymorphism, overriding, overloading

Declared type and actual type, dynamic dispatch, overriding an inherited method, overloading by signature, calling super.method(), casting and type testing in Java.

  • 26 cards
  • 4 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios

A few cards from the deck

  • Polymorphism: what is it?

    Show the answer

    The ability of one method call to produce different behaviours depending on the actual class of the object that receives it.

    A loop calling speak() on an array of animals does not need to know which are dogs or cats: each object answers in its own way.

  • What does this loop print?

    ```java
    class Animal {
    void speak() { System.out.println("..."); }
    }
    class Dog extends Animal {
    void speak() { System.out.println("Woof"); }
    }
    class Cat extends Animal {
    void speak() { System.out.println("Meow"); }
    }

    Animal[] t = { new Dog(), new Cat(), new Animal() };
    for (Animal x : t) {
    x.speak();
    }
    `

    • Woof, Meow, ...
    • ..., ..., ...
    • Woof, Woof, ...
    • Meow, Woof, ...
    Show the answer

    Woof, Meow, ...

    The variable x is always of type Animal, but the call is resolved at run time from the actual object: Dog, then Cat, then Animal.

  • An overriding method keeps the same … as the inherited method; the … annotation has the compiler check this rule.

    Show the answer

    An overriding method keeps the same signature as the inherited method; the @Override annotation has the compiler check this rule.

    Without the annotation, a typo in a parameter type silently creates a new method. With it, the error is reported: method does not override or implement a method from a supertype.

  • Overriding: what is it?

    Show the answer

    Rewriting, in a subclass, an inherited method with the same signature, to change its behaviour.

    The parameters must be identical, otherwise you write another method. The @Override annotation has the compiler check this.

  • This program compiles, but l.contains(new Point(1)) prints false although the list holds a point with x equal to 1. Why?

    ```java
    class Point {
    int x;
    Point(int x) { this.x = x; }
    public boolean equals(Point p) { return p.x == x; }
    }

    List<Point> l = new ArrayList<>();
    l.add(new Point(1));
    System.out.println(l.contains(new Point(1)));
    `

    • contains compares references with == and never calls equals
    • equals(Point) overloads equals(Object) without overriding it
    • hashCode is missing, and contains checks it before equals
    • The x attribute should be declared private
    Show the answer

    equals(Point) overloads equals(Object) without overriding it

    contains calls equals(Object). The method written here takes a Point: another signature, so an overload that is ignored. With @Override the compiler would have rejected it; the right form takes an Object. An ArrayList does not look at hashCode; only hash tables do.