Unit_5: Overriding, Shadowing, Refining Flashcards
Method replacement
What is simulation? What 2 types of simulation?
- simulation used to gather data in experimental setting.
- Time-driven: global clock is crucial element
- Event- driven: ordered list of future event (priority queue) is important. used to model system, gather data summarizing what happend, predict future
What 4 things subclasses can do with methods?
- leave method from superclass unchanged
- add new methods
- replace methods from superclass: shadow, overriding, or redefine
- Refine method from superclass: supply new method that use version the superclass.
Compare replacement vs inheritance
- subclass receives message which it has no definition -> passed to superclass and inherit method invoked
- if class defines a matching method -> intercept and handle by it
Compare shadowing vs Overriding
- Shadowing: same signature w parent, but method is NOT virtual -> not polymorphic
- methods always polymorphic in Java, can’t turn on/off as in C++
- C++: shadowing both methods and data members is possible, done at compile time because polymorphism iff declare method to be virtual
- Overriding: same signature w parent, but the method IS virtual. -> subclass provide new method version
- C++: virtual doesn’t make abstract class, it means we can use polymorphism via pointer-based variables.
How to prevent overriding method/class?
Use final.
in Java: final class Parent{..} -> can’t make subclass
What is redefinition?
- Redeclaring a method or variable with same name in child class, but not truly overriding it.
class A {
void show() {
System.out.println(“A”);
}
}
class B extends A {
void show() {
System.out.println(“B”);
}
}
public class Test {
public static void main(String[] args) {
A obj = new B();
obj.show();
}
}
What gets printed?
B.
Because obj is an instance of B, and show() is overridden in B, Java uses dynamic binding at runtime to call the B version.
What is covariant argument? (allowable
values for the
argument is larger in
the parent than the
child)
When a child’s method
has an argument that is
more specific than the
one in its parent’s
method (like this), we
say that it is a covariant
argument
What is contravariant argument
When a child’s method
has an argument that is
more general than the
parent’s
What is refinement? Why use it? How to use it?
WHAT: a subclass (or derived class) adds more specific behavior or state to a base class, without changing the original meaning or behavior of the base class.
WHY: superclass handle their own data, reduce code duplication
HOW:
class A ……..
virtual void print;
class B: public A ………
void print() override;
void B::print() {A::print(); …..} <=====
When a subclass method has the same name as a method in the parent class, but a different signature (different parameters), this is called redefinition.
- Overriding → same name and same signature; happens with virtual methods; enables dynamic binding.=» Redefinition → same name, different signature; creates a new method that hides the parent one.
- Shadowing → usually refers to variables (fields), not methods.
- Refinement → conceptually specializing behavior, often overlaps with overriding.
Writing a method in a subclass that calls a method
of the same name in the parent class, then possibly
adds additional code is
A. refinement
B. redefinition
C. shadowing
D. overriding
A <== Do what the parent does, then do more
Writing a method in a subclass that has the same
signature as a method in the parent class, when
polymorphism is not used is
A. refinement
B. redefinition
C. shadowing
D. overriding
B <== Redefinition means the subclass replaces a method from the parent class with the same signature, but without polymorphism
Looks like overriding, but not virtual, so no dynamic binding.
Which of the following are reasons to use “final”?
A. To forbid the addition of any instance variables
in a subclass
B. To forbid overriding a method
C. To forbid use of a constructor in the superclass
D. To forbid creation of a subclass
B, D
What are 2 types of class data members in C++?
- Instance (non-static): each object get its own copy, declare in class (eg int speed)
- Static members: shared by all object, declare with “static”, define outside the class
eg: class Car {
static int carCount;
};
int Car::carCount = 0;