Inheritance and method definition Flashcards
What are the 9 forms of inheritance?

What is specialization?

What is specification?

What is construction?

What is generalization?

What is extension?

What is limitation?
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

What is containment?
- 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

What is variance?

What is combination?

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

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.


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


For Stack-Based or Static objects(C++ only)
What happens if we try to make someB = someA?

In the following code, what does the following code snippets do?:
- private: int x;
- :Base(initX)
- What does Base::print()

- making int x private means it is Base’s job to manipulate it
- the :Base(initX) causes Base’s constructor to be invoked directly from Derived’s
- Base::print() invokes Base’s print method from within Derived’s

What does the following code output?


In the following code what do the following lines of code mean?:
- private int x;
- super(initX)
- super.print()

- private means it’s Parent’s job to manipulate that variable
- sends the variable to the super constructor
- uses the super classes’ print method

What is the output of the following code?:


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

What is the difference between replacement and inheritance?
Inheritance: If a subclass recieves a message for which it has no definition, the message is passed to the superclass.
Replacement (see below):

What is Shadowing?
- 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.
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!”);}
}

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!

What does the following code do and why?
If we made the methods virtual, would they be polymorphic?

- 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

How do you force or forbid overriding?
- 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{…
- public final void aMethod(int{…}//in the parent class prevents implemenation in the child class
- C++ (C++ 11 is required):
- virtual void aMethod() final {…}
- class Parent final { //attempting to extend the Parent class is illegal
- Java:
What is redefinition?
- 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
What are the 3 types of replacement? What are the differences between them?
Shadowing, Overriding and redefinition
Provide examples how there can be both good and poor types of inheritance.

What is a covariant argument?

What is contravariant argument?

Show an example of Covariance and Contravariance:
Animal class has a method isBigger(Animal a)
Dog (subclass of Animal) isBigger(<state> )</state>

Explain covariance and contravariance with the following return types:


What is co/contravariance technically?
redefinition (because it’s a different signature)
In general how would you implement a comparison function in an abstract class?
You cannot redefine these method implementations in the subclasses, your subclass methods must also take superclass type to compare to.

What does C++ allow with regard to covariance that Java does not?
C++ allows differing return types, however, the subclass return type needs to be a subset of those produced by the parent.

In general, when should you use covariant return types when overriding methods?
In general you should always try to follow conventions of nonvariance, even if a languages allows otherwise
- mostly for understanability
What does the following code do? How are the subclasses working with the constructor of the parent class?


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

What does inheriting publicly mean in C++?

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

How are superclass constructors and methods called in Java?

What is the syntax for self-reference in Java and C++?
Java: this.x = x
C++: this->x = x
What should method names start with?
a descriptive verb:
insertData(), print()
If you are outside of a class trying to refer to a static member, what is the syntax?
Java: ClassName.memberName
C++: ClassName::memberName
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?
ClassName.memberName
How do you define a static variable and method in Java?

What is the differnce between shadowing and overriding
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.
In general what is a static variable or method

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

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

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