Unit_5: Overriding, Shadowing, Refining Flashcards

Method replacement

1
Q

What is simulation? What 2 types of simulation?

A
  • simulation used to gather data in experimental setting.
  1. Time-driven: global clock is crucial element
  2. Event- driven: ordered list of future event (priority queue) is important. used to model system, gather data summarizing what happend, predict future
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What 4 things subclasses can do with methods?

A
  1. leave method from superclass unchanged
  2. add new methods
  3. replace methods from superclass: shadow, overriding, or redefine
  4. Refine method from superclass: supply new method that use version the superclass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Compare replacement vs inheritance

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Compare shadowing vs Overriding

A
  1. 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
  1. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to prevent overriding method/class?

A

Use final.
in Java: final class Parent{..} -> can’t make subclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is redefinition?

A
  • Redeclaring a method or variable with same name in child class, but not truly overriding it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is covariant argument? (allowable
values for the
argument is larger in
the parent than the
child)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is contravariant argument

A

When a child’s method
has an argument that is
more general than the
parent’s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is refinement? Why use it? How to use it?

A

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(); …..} <=====

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A <== Do what the parent does, then do more

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

B, D

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are 2 types of class data members in C++?

A
  1. Instance (non-static): each object get its own copy, declare in class (eg int speed)
  2. Static members: shared by all object, declare with “static”, define outside the class
    eg: class Car {
    static int carCount;
    };
    int Car::carCount = 0;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly