Inheritance and method definition Flashcards Preview

Comp 2150 > Inheritance and method definition > Flashcards

Flashcards in Inheritance and method definition Deck (51):

What are the 9 forms of inheritance?

A image thumb

What is specialization?

A image thumb

What is specification?

A image thumb

What is construction?

A image thumb

What is generalization?

A image thumb

What is extension?

A image thumb

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 

A image thumb

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 

A image thumb

What is variance?

A image thumb

What is combination?

A image thumb

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 image thumb

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.

Q image thumb

A image thumb

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

Q image thumb

A image thumb

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

What happens if we try to make someB = someA?

A image thumb

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

  1. private: int x;
  2.  :Base(initX)
  3. What does Base::print()

Q image thumb

  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


A image thumb

What does the following code output?

Q image thumb

A image thumb

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

  1. private int x;
  2. super(initX)
  3. super.print()

Q image thumb

  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

A image thumb

What is the output of the following code?:


Q image thumb

A image thumb

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

A image thumb

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):

A image thumb

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!");}


Q image thumb

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!

A image thumb

What does the following code do and why?

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

Q image thumb

  • 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

A image thumb

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{...
    • C++ (C++ 11 is required):
      • virtual void aMethod() final {...}
      • class Parent final { //attempting to extend the Parent class is illegal


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.

A image thumb

What is a covariant argument?

A image thumb

What is contravariant argument?

A image thumb

Show an example of Covariance and Contravariance:

Animal class has a method isBigger(Animal a)

Dog (subclass of Animal) isBigger( )

A image thumb

Explain covariance and contravariance with the following return types:

Q image thumb

A image thumb

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. 

A image thumb

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.

A image thumb

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?

Q image thumb

A image thumb

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

A image thumb

What does inheriting publicly mean in C++?

A image thumb

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

A image thumb

How are superclass constructors and methods called in Java?

A image thumb

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?



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

A image thumb

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

A image thumb

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

A image thumb

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

A image thumb

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

A image thumb