Inheritance and method definition Flashcards

1
Q

What are the 9 forms of inheritance?

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

What is specialization?

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

What is specification?

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

What is construction?

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

What is generalization?

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

What is extension?

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

What is limitation?

A

Behaviour of subclass is smaller or more restrictive than parent (excludes some operations)

  • Often occurs when working with base classes that can’t be modified (similar to generalization in that sense)
  • e.g. taking a list and making other data structures out of it via inheritance (e.g. a queue, a stack) – Need to turn OFF features we don’t want used
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is containment?

A
  • Specialization (#1): is-a
  • Containment: has-a
  • Suppose UnprintableCharAtom has a CharAtom x as a data member.
  • x can be private.
  • We just don’t allow access to print() method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is variance?

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

What is combination?

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

What does the stack frames look like for (instance of B is a subclass of A):

  • Instance of A
    • x = 55
    • y = “Hello”
  • Instance of B
    • x = 77
    • y = “Pizza”
    • z = 1.135
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Pointer-based objects (C++/Java) - someA is a pointer-to-A, someB is a pointer-to-B. Draw the the stack frame of what happens when we make someA point to a B instance.

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

In Stack-Based or Static Objects (C++ only), what happens if we try someA = SomeB?

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

For Stack-Based or Static objects(C++ only)

What happens if we try to make someB = someA?

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

In the following code, what does the following code snippets do?:

  1. private: int x;
  2. :Base(initX)
  3. What does Base::print()
A
  1. making int x private means it is Base’s job to manipulate it
  2. the :Base(initX) causes Base’s constructor to be invoked directly from Derived’s
  3. Base::print() invokes Base’s print method from within Derived’s
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the following code output?

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

In the following code what do the following lines of code mean?:

  1. private int x;
  2. super(initX)
  3. super.print()
A
  1. private means it’s Parent’s job to manipulate that variable
  2. sends the variable to the super constructor
  3. uses the super classes’ print method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the output of the following code?:

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

In general, what are the 4 things subclasses can do?

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

What is the difference between replacement and inheritance?

A

Inheritance: If a subclass recieves a message for which it has no definition, the message is passed to the superclass.

Replacement (see below):

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

What is Shadowing?

A
  • There are subtle differentces between these
  • In shadowing, the signatures are the same in parent and child methods, but the method is NOT virtual(no polymorphism)
  • Similar to the concept of shadowing data members, which exists in many languages, like in Java.
22
Q

For the following classes, what does the attached code print and what is happening?

class Parent {

public int x = 12;

public void doIt( ) { System.out.println(“In Parent!”);}

}

class Child extends Parent{

public int x = 6;

public void doIt( ) { System.out.println(“In Child!”);}

}

A

Data members are shadowed: Child’s x shadows the parent’s x. Can bypass child’s shadowing data by referring to object throught he parent type. Done at compile time - compiler “knows” which x to refer to by pointer type being used.

p.doIt( ) still prints “in child” because it’s polymorphic, java is always looking at the dynamic type (the variable’s data) and routing the message there - you can’t turn this off!

23
Q

What does the following code do and why?

If we made the methods virtual, would they be polymorphic?

A
  • Only if we were using heap-based variables - then we would print “Child”, even when accessing via parent variable P, as we saw in the Java example.
  • However, then we’d be doing OVERRIDING - where the type signatures in parent and child are the same but the method IS virtual.
    • Java methods are always overridden rather than shadowed, because there is always polymorphism
  • remember “virtual” in C++ does not amke an abstract class - only means we can use polymorphism via pointer-based variables
    • i.e. I can still make a direct instance of Parent that would use Parent’s method
24
Q

How do you force or forbid overriding?

A
  • Recall that virtual in C++ permits overring (as opposed to shadowing)
    • Unless it’s pure virtual though, it’s not an abstract method: it doesn’t force the coder to properly override everything
  • Abstract in Java forces overriding
  • Forbiding overriding:
    • Java:
      • public final void aMethod(int{…}//in the parent class prevents implemenation in the child class
        • a class itself can be made final by final class Parent{…
    • C++ (C++ 11 is required):
      • virtual void aMethod() final {…}
      • class Parent final { //attempting to extend the Parent class is illegal
25
Q

What is redefinition?

A
  • If the type signatre of a given method name differs from parent to child, the we’re doing REDEFINITION
    • Same method because it’s the same name, but obviously not the same definintion because the arguments don’t match
26
Q

What are the 3 types of replacement? What are the differences between them?

A

Shadowing, Overriding and redefinition

27
Q

Provide examples how there can be both good and poor types of inheritance.

A
28
Q

What is a covariant argument?

A
29
Q

What is contravariant argument?

A
30
Q

Show an example of Covariance and Contravariance:

Animal class has a method isBigger(Animal a)

Dog (subclass of Animal) isBigger(<state> )</state>

A
31
Q

Explain covariance and contravariance with the following return types:

A
32
Q

What is co/contravariance technically?

A

redefinition (because it’s a different signature)

33
Q

In general how would you implement a comparison function in an abstract class?

A

You cannot redefine these method implementations in the subclasses, your subclass methods must also take superclass type to compare to.

34
Q

What does C++ allow with regard to covariance that Java does not?

A

C++ allows differing return types, however, the subclass return type needs to be a subset of those produced by the parent.

35
Q

In general, when should you use covariant return types when overriding methods?

A

In general you should always try to follow conventions of nonvariance, even if a languages allows otherwise

  • mostly for understanability
36
Q

What does the following code do? How are the subclasses working with the constructor of the parent class?

A
37
Q

In C++ what is the syntax for invoking a method from the superclass?

A
38
Q

What does inheriting publicly mean in C++?

A
39
Q

What responsibility does an object have with regard to the data members it defines?

A
40
Q

How are superclass constructors and methods called in Java?

A
41
Q

What is the syntax for self-reference in Java and C++?

A

Java: this.x = x

C++: this->x = x

42
Q

What should method names start with?

A

a descriptive verb:

insertData(), print()

43
Q

If you are outside of a class trying to refer to a static member, what is the syntax?

A

Java: ClassName.memberName

C++: ClassName::memberName

44
Q

Since Java cannot have global variables, what is the only method for creating an equivalent to a global variable/constant in a non-oo programming language?

A

ClassName.memberName

45
Q

How do you define a static variable and method in Java?

A
46
Q

What is the differnce between shadowing and overriding

A

Shadowing overrides based on scope, so if there’s a parent and child class with x and we call c.x or p.x it will print that scopes x. However if we make p = c and then call p.x it will not print c.x, it will print p.x instead. Overriding is a method that runs in the subclass instead of the parent class because we wrote the same method in the subclass as well.

47
Q

In general what is a static variable or method

A
48
Q

How do you create a static data member in C++

A
49
Q

How do you refer to a static variable in C++?

A
50
Q

What happens if you fail to initialize the static variable in C++?

A
51
Q
A