- Polymorphism: write code for the superclass and it works for every subclass (“program in the general”).
- Same message (move) → different behaviour per object (fish swims, bird flies).
- Polymorphic systems are extensible: add new subclasses with little or no change.
- A superclass reference can point to a subclass object; the OBJECT’s type decides which method runs.
🎯 How this can come up · 1 question
Employee e = new SalariedEmployee(); e.earnings(); runs which earnings()?
Correct: SalariedEmployee’s.
- A superclass object is NOT a subclass object; downcasting needs an explicit cast.
- Compile time: the variable’s type must have the method. Run time: the object’s type picks the version → dynamic binding.
- Abstract classes: never instantiated; used as superclasses; incomplete.
- abstract method = no body (public abstract void draw();). A class with an abstract method must be abstract. Concrete subclasses must implement it.
🎯 How this can come up · 2 questions
A class that contains an abstract method must itself be declared abstract.
True.
Constructors can be declared abstract.
False.
- You can’t create objects of an abstract class, but you can declare variables of its type.
🎯 How this can come up · 1 question
Given abstract class Shape, which is legal?
Correct: Shape s = new Circle();.
- Payroll case study: salaried, hourly, commission and base-plus-commission employees.
- Employee declares abstract earnings(); each subclass overrides it.
- Process objects polymorphically; give BasePlusCommission employees a 10% raise using instanceof.
- toString and earnings calls are resolved at run time: dynamic (late) binding.
- Downcasting to the wrong type throws ClassCastException → check with instanceof first.
🎯 How this can come up · 1 question
Before downcasting an Employee reference to BasePlusCommissionEmployee you should check with:
Correct: instanceof.
- getClass().getName() returns the object’s class name.
- Assigning a superclass reference to a subclass variable needs a cast.
- final method: can’t be overridden (static binding). private and static methods are implicitly final.
- final class: can’t be extended (e.g., String).
🎯 How this can come up · 1 question
A class declared final:
Correct: Cannot be extended.
- An interface lists methods to implement; a class can implement many interfaces.
- Interface methods are implicitly public abstract; fields are public static final.
🎯 How this can come up · 1 question
Fields declared in an interface are implicitly:
Correct: public static final.
- implements = a contract; a class that doesn’t implement every method must be abstract.
- Use an interface instead of an abstract class when there’s no default implementation to inherit.
- Implementing classes have an is-a relationship with the interface type.
- UML: «interface» stereotype; realization = dashed arrow with hollow head.
- One superclass, many interfaces: class X extends A implements I1, I2.
🎯 How this can come up · 1 question
A Java class can implement more than one interface.
True.
No slides match this filter.