final Flashcards

1
Q

what are the 9 forms of inheritance?

A
  1. specialization
  2. specification
  3. construction
  4. generalization
  5. extension
  6. limitation
  7. containment
  8. variance
  9. combination
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Forms of Inheritance

what is specialization?

A
  • subclass is a specialized form
  • subclass may add more or override parent methods

eg. Honda is a Car

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

In JavaScript how are constants made? How are they accessed?

A

Constants are made by making a method with “get”

Class constants use the static keyword and are accessed using the name of the constructor.

eg. static get COST_FACTOR() { return 1.5; }
ClassName.COST_FACTOR();
this.constructor.COST_FACTOR; //can also do this

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

Forms of Inheritance

what is specification?

A

used to guarantee common methods of interaction
- parent is used to describe required behaviour, using abstract methods
- focus is filling the holes left incomplete by the parent

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

what is the principle of substitution?
(aka the Liskov Substitution Principle)

A

If we have 2 classes A and B such that B is a subclass of A: it should be possible to substitute instances of class B for instances of class A in any situation with no observable effect

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

Interfaces can be seen as a form of inheritance. Which one?

A

specification

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

Forms of Inheritance

what is construction?

A

Child inherits most or all of its functionality from parent, making changes to the behaviour to build/construct something different

  • Principle of substitution does NOT hold
  • Subclass is not a subtype, can’t be substituted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The whole idea of an object is to provide ___________ of the representation of the state and behaviours of something

It’s very important to be able to hide implementation details

  • if an object defines a field, it should …
  • don’t assume subclasses will…
  • these things lead to increased…
A

encapsulation

  • define how it can be used/accessed/modified
  • use it properly
  • independence and reliability
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Forms of Inheritance

what is generalization?

A

A subclass modifies the behaviour of the parent to make a more GENERAL kind of object (opposite of specialization)

  • generally want to avoid this
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

C++

class X: {}

  • public: visible to…
  • private: visible only to…
  • protected: visible in…
A
  • public: visible to any user of a class X object
  • private: visible only to X’s own member functions
  • protected: visible in X’s member functions and member functions of subclasses of X
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Forms of Inheritance

what is extension?

A

Extension only adds new methods to those of the parent, rather than overriding anything (inherited behaviour does not change)

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

Why use private at all when protected seems to give us what we want?

A

Private gives us better security and reliability, and better control of data locally

protected gives away too much power

nothing stopping subclasses from managing the data/methods in unintended ways

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

Forms of Inheritance

what is limitation?

A
  • Behaviour of subclass is smaller or more restrictive than parent (excludes some operations)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

which one of these is done by default if no access modifier is used?

class B: public A
class B: private A
class B: protected A

A

private

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

In C++, The status of the base class defines…

A

what the status of its members are in the inherited class*

*status only changes if it’s getting more strict

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

Forms of Inheritance

what is containment?

A

has-a relationship

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

private inheritance is essentially a ______ relationship

A

has-a

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

private and protected inheritance do not allow…

A

substitution / polymorphism

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

Forms of Inheritance

what is variance?

A
  • > =2 classes have similar implementations but don’t seem to have a hierarchical relationship between the abstract concepts they represent. We arbitrarily pick one to be the subclass of the other
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

C++ Friendship

friends have access to…

A

the class’ private members and makes it part of the object’s scope (allowing it to use the object’s members)

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

class Node {
friend class GenericList;
}
* all of GenericList’s members are now friends of
Node and can access…

A

Node’s private members

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

If class B is a friend of class A, who decides that?

A

Class A.

the class giving up control of its members is the class that defines who its friends are

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

Friendship is NOT:
* __________
* __________ (the friend of my friend is not my friend!)
* __________

A
  • inherited
  • transitive
  • reciprocal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Forms of Inheritance

what is combination?

A

A class requires a combination of features from >=2 parent classes

  • This requires multiple inheritance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Friendship is not a way to fix a bad OO implementation. Why?

A

It gives away a lot of power and can possibly defeat the purpose of encapsulation when not used properly

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

In Java, an _____ makes reference to already
compiled byte code stored outside of your program,
and allows a dynamic search for it

A

import

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

Example: a sessional instructor could be a graduate
student and an instructor at the same time

  • SessionalInstructor class would extend
    GradStudent and Instructor classes

What form of inheritance is this?

A

multiple inheritance

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

Java 0rganizes classes into packages
- Partly for the sake of
- and

A

grouping code logically – putting a group of classes that serve a common purpose together

encapsulation: there are mechanisms that can allow broader access between items in the same package, whether they are subclasses or no
* i.e. serving much the same purpose as a set of
friend declarations in C++

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

package MyPackage.MyClassesA;

public class MyClass1 { …..}
class MyClass2 {…..}

  • The name of this package is __________
  • Its location is directory ________ inside directory __________
A

MyPackage.MyclassesA

MyClassesA inside directory MyPackage

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

How do package directories get created?

A

You need to create the folders and ensure the classes are compiled in the right folder

it is up to you to place the source in the appropriate place

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q
  • In dynamic languages (e.g. JavaScript, Python),
    variables have dynamic types – a variable’s type
    changes to suit what’s in it

what does this mean for dynamic class binding?

A

can assign subclass objects to superclass variables AND the reverse

  • only because there is no concept of a static type anymore
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

To allow polymorphism, a hidden tag is stored to allow identification at _______

A

run time

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

Implementing a Set class by inheriting from a general List class
* Set inherits all List functionality
* ensure duplicates don’t go in the list

What form of inheritance is this?

A

Construction

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

if no protection modifier is used in Java what do we get by default?

A

package private

  • in our assignments it essentially makes everything public
  • if using packages properly then anything inside the package has access
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Anything not belonging to the named package belongs to…

A

the default package (unnamed)

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

what are the 5 good forms of inheritance?

A
  1. specialization
  2. specification
  3. extension
  4. containment
  5. combination
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Java’s Protection Modes

  • Public: Visible to…
  • Protected: Visible to…
  • Private: Visible only to…
  • Blank (package access / friendly access / package protection / package private): visible to…
A

Public: Visible to anybody anywhere

Protected: Visible to the class in which it’s defined,
and its derived subclasses, AND classes in the
same package
- a lot more insecure than you would at first think

Private: Visible only to the class in which it’s defined

package private: visible to itself, and other
classes in the same package, but not from derived
subclasses

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

Does B have access to x?

(Package P1)
ClassA {
int x;
}

(Package P2)
ClassB extends ClassA {

}

A

No.

x is package private and classB is in a different package

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

in Java, protected members are accessible by…

A

subclasses AND classes in the same package

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

Suppose UnprintableCharAtom has a CharAtom x as
a data member
* x can be private
* We just don’t allow access to print() method

what form of inheritance is this?

A

containment

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

________ is often seen where we are working with a base class that can’t be modified (eg. library code)

A

generalization, limitation, variance

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

package MyPackage.MyClassesA;
public class MyClass1 { …..}
class MyClass2 {…..}

one of these are public – i.e. their existence will be known to…

A

anybody importing the package once it is compiled

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

package MyPackage.MyClassesA;
public class MyClass1 { …..}
class MyClass2 {…..}

When run through the java compiler, what .class files and .java files will be created?

A

MyClass1.class
MyClass2.class
MyClass1.java

  • since MyClass2 is in the same file, it doesn’t need its own .java file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

What is a Java Interface?

A

A Java facility for guaranteeing an object will be able to interface with other objects in a defined way (by forcing it to implement abstract methods) without actually using any traditional inheritance

An Interface in Java is a structure that lists (public)
methods that are to be required by classes that
implement the interface

  • It has no concrete methods*
    *possible with default methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

In an interface every method is automatically _________ and _________

A

public and abstract

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

What form of inheritance is used with interfaces?

A

specification

interfaces specify which methods must be used by any class that implements the interface

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

what’s the difference between an abstract class and an interface?

A

Classes extending an abstract class are related through a hierarchy; those implementing an Interface can be otherwise totally unrelated

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

e.g. taking a list and making other data structures out of it via inheritance (e.g. a queue, a stack)

what form of inheritance is this?

A

limitation

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

If we want to make a linked list where the data utilizes interfaces Printable and Comparable what should we do?

A

define an interface that is a child of any interface you want to implement (Printable and Comparable in this case), and then use that interface for you Node data

interface ListItem extends Comparable, Printable {}

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

How come multiple inheritance isn’t violated when implementing multiple interfaces

A

we aren’t inheriting anything, just receiving more demands

*one exception to this is using default methods

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

can you make an instance of an interface?

A

No. You make an instance of whatever class
implements the interface

it’s just a collection of methods that have to be implemented by somebody else – no memory to allocate

52
Q

Can the methods in an interface have code? Should they?

A

Typically the should not, and can not

But, default methods allow you to
eg. default void doSmth() { … }

  • an implementing class can keep the default behaviour or change it
53
Q

when are default methods useful?

A

when you want to add a new method an old interface, but you want your old code to keep working (your old code was written for the older version of the interface)

54
Q

what problem does using default methods create?

A

you get problems related to multiple inheritance

if a class implements two interfaces that both define different version of a default getIt() method then you will get a compile-time error

*can fix if the class defines a new behaviour for getIt()

55
Q

What happens if:
* Class Child extends class Parent
* Class Child implements interface Int1
* Both Parent and Int1 provide code for method foo()

A

The version from the parent class overrides the default behaviour provided by Int1

Extends has priority

56
Q

When defining a subclass that also implements an
interface, ________ goes before __________

implements/extends

A

public class Child extends Parent implements Int1 {}

57
Q

default String toString() {return “Hello!”;}

does this work? why or why not

A

Can’t override methods from Object with a default method in an interface

Gives error at compile time:
error: default method toString in interface Int1 overrides a member of java.lang.Object

58
Q

what kind of methods can be defined with code in an interface

A

default methods and static methods

59
Q

What are nested Interfaces? When/why would you use this?

A

when you declare an interface within another interface

used to group interfaces that have a similar/related goal or meaning

  • e.g.
    interface Printable {
    void print();
    interface Testable { //nested interface
    void test();
    }
    }
60
Q

how do you refer to a nested interface?

eg. Testable interface is nested inside Printable interface

A

To refer to a nested interface, you must specify the name of the outer interface, followed by a dot

class MyTest implements Printable.Testable {}

61
Q

Does a nested interface inherit requirements of its outer interface?

62
Q

JavaScript is a ________ language

C++ and Java are properly referred to as ____________ languages

A

dynamic

semi-static

63
Q

JavaScript has dynamic variable typing. What is that?

What is a special case of this in Java and C++?

A

a variable’s type is whatever it currently contains - which means it usually has no type declaration

in Java/C++ - putting an instance of a subclass in a superclass type variable

64
Q

dynamic languages access all memory via ________ internally

65
Q

dynamic languages are usually __________ rather than compiled

A

interpreted

66
Q

what are the 3* forms of inheritance that should be avoided

*(3 and a half)

A
  1. construction
  2. limitation
  3. variance
  4. generalization
67
Q

in some way, the programmer must play the role of
the compiler in dynamic languages. why?

A

When a program’s state can change so drastically at runtime, it means a lot of runtime checking that the programmer must do

68
Q

Does JavaScript require semicolons at the end of statements?

A

no, but we still use them by convention

69
Q

how do you declare a variable in JS?

A

let keyword

let x = 5;
let myString = “hello”;

70
Q

Using “let” gives us block scope. which means:

A

variables defined in a block are only accessible inside that block

71
Q

Does B have access to x?

(Package P1)
ClassA {
protected int x;
}

(Package P2)
ClassB extends ClassA {

}

A

yes

in Java, protected means accessible by subclasses as well as classes in the same package. ClassB is a subclass of ClassA

72
Q

In JS, at the top of your code (first line), you should put what? why?

A

“use strict”

  • This adds more restrictions
  • want all the error messages we can get since there aren’t many

eg. preventing the use of undeclared variables
x = 75; // ReferenceError: x is not defined

73
Q

In JS, you can use the _______ operator (not a method) to get the type of what is stored in a variable

74
Q

What happens if you pass too few parameters to a JS function?

Too many?

A

Too few: The variables not assigned a parameter will get the value “undefined”

Too many: Extras get put in the arguments array which can be accessed
- console.log(arguments); // print all args

75
Q

All primitive types in JS are __________
- if you assign the value of a variable to another one, a copy will be made

76
Q

Remember that JS is weakly typed.

  1. 0 == “0” //true or false?
  2. 0 == false
  3. 0 === “0”
  4. “” == 0
  5. undefined == null
  6. “0” == 0
  7. 0 === false
  8. “0” == “”
  9. ‘’ == false
  10. undefined === null
  11. 0 == “”
A
  1. true
  2. true
  3. false
  4. true
  5. true
  6. true
  7. false
  8. false
  9. false
  10. true
  11. true
77
Q

In most cases, you should use === and !== instead of == and !=

why?

A
  • JS is weakly typed
  • === checks strict equality
  • does the comparison without type conversion
78
Q

Does JS need a main function?

A

no but by convention we create one and then run it as the only executable line in our script

79
Q

Say generic list provides insert@front, insert@end,
remove@front, remove@end

creating a stack out of this would be limitation. Whats the appropriate solution?

A

The appropriate solution is using containment (i.e. build the list, then use it as a data member inside a stack)

The stack can then use the functions from list that it needs to do push and pop and leave anything else alone

80
Q

how do you create a function in JavaScript?

A

by using the function keyword

function myFunction(param1, param2){

}

(dont need it for class method definitions)

81
Q

how to make a constructor for ClassB that is a subclass of ClassA
- classA takes int param
- classB takes string

in all three languages

A

Java:
ClassB(int x , String s){
super(x);
this.s = s;
}

C++ .cpp:
#include <string>
ClassB::ClassB(int x, std::string s) : ClassA(x) {
this->s = s
}</string>

JS:
constructor(x, s){
super(x);
this.#s = s;
}

82
Q

in JS instance variables are _______ by default

private/protected/public?

83
Q

in JS, instance variables that are public don’t need to be declared. When are they created?

What about private variables?

A
  • they are created when used (either in the
    constructor [best] or any other method)
  • they don’t exist until that code is executed

private variables must be declared
- use #

eg.
class MyClass {
#id; // declaration needed cuz private
constructor(id) {
this.#id = id;
}
}

84
Q

How do we create multiple constructors in JS?

A

Technically it’s not allowed (no overloading)
we have to simulate it using the args array

// null constructor and constructor with one param
constructor() {
if(arguments.length == 0) {

} else if (arguments.length == 1) {

}
}

85
Q

What happens when you try to overload a function in JS? How can you simulate overloading?

A

The version furthest down gets used.

Must simulate using the args array, same as how we simulate having multiple constructors.

86
Q

How is getting/setting different in JS?

A

special keywords “get” and “set”
- don’t need parentheses when calling

eg.
// in class MyClass
get id() {
return this.#id;
}

// in main
let mc = new MyClass(1234);
console.log(mc.id); //using getter

87
Q

How is a private class variable accessed in JS?

A

through the class name, even within the class
- class methods are also accessed through the class name

class MyClass {
#id;
static #counter = 1; //private class variable
constructor(){
this.#id = MyClass.#counter++;
}
}

88
Q

How do we do class constants in JS?

A
  • cant use const keyword with class variables
  • solution: make getter static method

static get HOURS_PER_DAY(){
return 24;
}

//accessing the constant
console.log(MyClass.HOURS_PER_DAY);
// because we used the “get” keyword it really looks like we’re accessing a constant

89
Q

what’s another way to make objects in JS that you can’t do in Java/C++

how is it different from how we normally create objects?

A

you can make single instance objects in JS like this:
- methods can be defined inside
- “function()” is actually not necessary

aChair = { material: “wood”, weight: 5.5
print: function() {
console.log(“Weight: ” + this.weight);
}
}

  • only get single instance
  • no class model defined that can be reused
90
Q

What if you forgot something in your unnamed object in JS?

A

You can add fields and methods to it after it’s definition
- “function()” is required in this case

These new members would only exist after the code that adds new members

eg.
aChair.getWeightInLbs = function() {
return this.weight * 2.205;
}

91
Q

How can you update a class in JS?

A

use .prototype
- Note that usually, if you have access to the class definition, you would want to make any permanent changes there
- can be used to add to built in classes such as Array

eg.
function main() {

//add field
MyClass.prototype.name = “NoName”;

//add method:
MyClass.prototype.getName = function(){ return this.name; };
}

92
Q

let a = new MyClass(3333);
let b = a; //a and b are pointing to the same memory
console.log(a == b); //true or false?

A

true. compares pointers

93
Q

Object.assign can be used to copy objects
- use getPrototypeOf to copy the methods as well

function main() {
let mc = new MyClass(1234);
let mcCopyProto = Object.assign(Object.getPrototypeOf(mc), mc);

This will work if the fields in MyClass are public. So does Object.assign create a deep copy?

A

not a true deep copy
- Object.assign (with or without getPrototypeOf) still does not deep copy nested objects (objects contained within objects)

94
Q

If you want to separate your classes into different files, they will not get detected automatically by Node.js, even if they are in the same folder

You will need 2 things:

A
  • module.exports -> to export a class (make it available to be “imported”)
  • require -> to import what is exported in another file
95
Q

In JS _______________ are not inherited

A

instance variables

  • they are created when the code creating them is
    run, i.e. when calling the super constructor
  • Only the methods are inherited
96
Q

What happens if you try to access a private field from a parent class?

class GradStudent extends Student {
#thesis;
constructor(name, sId, thesis) {
super(name, sId);
this.#thesis = thesis;
this.#name = “Jane”; //?
}
}

A

SyntaxError: Private field ‘#name’ must be declared in an enclosing class
- JS thinks you want to define a new field but private fields must be declared

97
Q

In JS you can assign a super class in two ways:

A
  1. using extends like Java
  2. at runtime using prototype

The following will declare that MyClass is a subclass of
OtherClass:
MyClass.prototype.__proto__ = new OtherClass();

  • MyClass will now inherit any methods from OtherClass
98
Q

Overloading in JS

In some cases, it might make sense to use default parameter values to simulate overloading

  1. In the example below, parameter message will be initialized to “Hello” if…
  2. Similarly, parameter number will be initialized to 0 if…

myMethod(message = “Hello”, number = 0)
{
//body
}

A
  1. no parameter is used when calling the method
  2. less than 2 parameters are used when calling the method
99
Q

How is overriding different in JS?

A

overriding a method in JS doesn’t require using the same parameters in the signature: only the same method name

100
Q

How does shadowing work in JS? Think about private instance variables vs public. How about methods?

A

shadowing only occurs in private instance variables

shadowing is not possible with public variables because fields are not inherited

shadowing is not possible with methods because there is no tool to turn polymorphism on/off

101
Q

What is duck typing?

A

“if it walks like a duck and quacks like a duck, it probably is a duck”

  • duck typing is when your main concern is if the object contains the required method
  • can verify using typeof
    eg. if (typeof s1.print == “function”)
102
Q

polymorphism is always on in JS. why?

A

no static type. always getting the version of the dynamic type

103
Q

Why is instanceof less useful in JS?

A
  • no need to cast variables
  • care more about if it has the right method rather than if it belongs to the right class (duck typing)
104
Q

what are the goals of an abstract class?

A
  • No instantiation possible
  • Enforce requirements on the subclasses (required methods)
  • Potentially have some code too that can be inherited
105
Q

How do you make an abstract class in JS?

A

simulate

  1. make instantiation impossible
    - throw new Error() in constructor if this.constructor.name === “ThisClassName”
  2. create abstract methods
    - throw new Error() in any method you want to be abstract
    - this way the child class must override the method to get rid of the error message
106
Q

What are mixins?

A
  • a way to include outside code in a class without
    using inheritance
  • it’s used to bring in methods (mostly) and constants
    to different classes that are unrelated
  • To add a mixin to a specific class, you need to add it to the prototype using Object.assign
107
Q

There is the potential for ambiguity in interfaces and multiple inheritance. How might this happen? How does the programmer handle it?

What’s a similar situation in C++?

A
  • If two interfaces provide the same constant, there can be ambiguity and we have to prefix it with the interface name
  • doesn’t apply to methods (unless they both have the same default method)
  • in C++, we can get ambiguity with methods when using multiple inheritance. We have to handle it in a similar way. Use fully qualified names (e.g. A::foo())
108
Q

allowing multiple inheritance leads to two complications:

A
  1. Implementation difficulties (how is the compiler going to deal with this?
    - it’s not the programmer’s problem, it’s the language’s responsibility, but good to know
  2. Problems with ambiguities
109
Q

What is the diamond problem

How do you deal with the ambiguity it creates?

A

When you have a class hierarchy that looks like a diamond. The bottom class (C) will have two versions of the stuff from the top class (A)

Can deal with it by explicitly saying which version we want in the bottom class:
B1::a = 6; // B1’s copy of a
B2::a = 7; // B2’s copy of a

Can use virtual inheritance (let the compiler solve the problem)
class B1: public virtual A {…}
class B2: public virtual A {…}
- C becomes responsible for initializing A’s stuff

110
Q

What does virtual inheritance mean?

A

there may be more than one appearance of the superclass, and if that is the case, only one copy should be included

111
Q

There are special rules to ensure that virtual base class constructors/destructors are only called once per object (2)

A
  1. virtual base classes are constructed before all other subclasses
  2. constructors for virtual base classes anywhere above in the hierarchy must be called by the constructors of each derived class
    * this is one time when a subclass can directly call a grandparent’s constructor
112
Q

How do we simulate multiple inheritance in Java
(1 good way, 2 bad)

A
  1. use interfaces
  2. Duplicate code: you can inherit from one of the two classes you need to be a child of, and duplicate the code of the other (retain one hierarchy, but not both)
  3. Use Limitation: insert objects lower into a hierarchy, and override methods that you don’t want with empty bodies
113
Q

Why is duplicating code a bad way to simulate multiple inheritance?

A

one of OOP’s goals is to reuse code

114
Q

Why is using limitation a bad way to simulate multiple inheritance?

A

limitation breaks the principle of substitution
- but in some cases it’s one of the only choices

115
Q

How does the compiler deal with multiple inheritance? For example, if C extends A and B

This is how an instance of C would look internally:
_________
|Tag: C |
|A’s stuff|
|B’s stuff|
|C’s stuff|
‾‾‾‾‾‾‾‾‾‾‾‾‾

Where would A pointers enter? B? C?

A
  • Need additional checks
  • Ensure that we access data from the appropriate entry points for each type

_________
|Tag: C |
|A’s stuff| entry point for A* or C*
|B’s stuff| entry point for B*
|C’s stuff|
‾‾‾‾‾‾‾‾‾‾‾‾‾

B* shouldn’t enter at the top because it doesn’t know about A’s stuff

116
Q

In a Java interface There are no true data members (not in the sense of inherited variables anyway) – how come?

A

any data members we declare are implicitly public, static, and final, i.e. class-level constants

117
Q

A powerful feature of Interfaces is that they can act like a _____, just as a class does

A

type

can have variables of type Measurable, Printable, Viewable, etc.

118
Q

T/F

In JavaScript, parameters are more of a suggestion

A

true

it doesn’t complain if you have too few or too many

119
Q

Name the type of inheritance

class Adult{…}
class Student{…}
class GradStudent : public Adult, public Student{}

A

Combination

120
Q

Name the type of inheritance

abstract class 2DShape{
public abstract double calcArea();
}

class Circle extends 2DShape{

}

A

specification

121
Q

Name the type of inheritance

class Walker{
public void walk() {…}
}

class Runner extends Walker{
public void run() {…}
}

122
Q

Name the type of inheritance

class Feline{…}

class Cat extends Feline{…}

A

specialization

123
Q

Name the type of inheritance

class Node{…}

class MyList{
private Node first;
public Node getFirst(){
return first;
}
}

A

containment

124
Q

Name the type of inheritance

class Car{
private int age;
public int getAge()…
public void setAge()…
public void startCar(
…code to start car…
)
}

class EnginelessCar extends Car{
public void startCar(){}
}

A

limitation

125
Q

Name the type of inheritance

class Tiger {…}
class Tiger extends Lion {…}

126
Q

Name the type of inheritance

class Flower {…}
class Plant extends Flower {…}

A

generalization