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:


















