Week 5 Flashcards

1
Q

The process of acquiring the properties (methods and fields) of another

A

Inheritance

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

Types of inheritance

A

Single
Multi-level
Hierarchical
Multiple

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

In hierarchical inheritance, Class B and C both

A

Inherit from class A

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

In multiple inheritance, Class C

A

inherits from both A and B

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

The ______ keyword is used to enforce inheritance of the properties of a class

A

extends

Syntax: Class Subclass extends Superclass

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

T/F - The vehicle superclass extends the car subclass

A

False

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

What’s an advantage of inheritance?

A

Lets you reuse code instead of duplicating it.

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

What does a subclass inherit from a sueprclass?

A

All members (methods, fields)

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

T/F - Since a car is a special type of vehicle, we can use a car object in algorithms that manipulate vehicle objects

A

True

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

Explain the substitution principle

A

You can always use a subclass object when a superclass object is expected.

IE. A method that processes vehicle objects can handle any kind of vehicle

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

Is a quiz a subclass of a question?

A

No, quizzes and questions are not related in that way.

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

You should use a single class for variation in…
Whereas you should use inheritance for variation in….

A

You should use a single class for variation in values
Whereas you should use inheritance for variation in behavior

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

Class Manager and Class Employee, who would be the superclass? Who would be the subclass?

A

Manager would be a subclass of employee. You must first be an employee to be a manager.

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

T/F - A subclass will inherit all methods, unless it overrides them

A

True

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

What is important to specify when making a subclass?

A

Specify what makes it different from the superclass

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

Do you re-declare instance variables in a subclass?

A

No, unless you want an instance variable that is not already part of the superclass

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

Does a subclass inherit private members?

A

Subclasses do not.

Objects of a subclass contain the private members of the superclass. Despite not being able to access them, they are there.

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

When inheriting methods, what do you do if the inherited behaviour is not required in the subclass?

A

Change the implementation of the methods

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

T/F - When you provide a new implementation for an inherited method, you override the method

A

True

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

ChoiceQuestion is a subclass of Question. What are 3 ways the ChoiceQuestion objects class would differ from the Question objects class?

A
  1. There’s now a method for choosing/storing an answer
  2. There’s objects to store the possible choices
  3. There’s a display method to show the possible choices
21
Q

After making the 3 differences, the ChoiceQuestion class must _______ that these changes were made

22
Q

T/F - Subclasses cannot access private instance variables, therefore they must use the public methods of the superclass

23
Q

Since subclasses cannot access private variables of the superclass, that means they were not inherited.

A

False. Private fields are inherited, you just can’t access them.

24
Q

T/F - An overriding method can extend or replace the functionality of the superclass method

25
What is one possible solution when you need to access a private field of the superclass?
Use the reserved word "super" to call the required method super.method(parameters if any)
26
This reserved word forces the execution of the superclass method
super
27
Are constructors considered members? What does that entail for inheritance? What is one caveat?
Constructors are not members. Therefore they're not inherited by subclasses. The caveat is that constructors of the superclass can be invoked from the subclass.
28
The super keyword is similar to the ____ keyword. How?
this. It differentiates the members of the superclass from the subclass, if they have the same names.
29
Use the _____ keyword to differentiate between sub and super members
super
30
If you wish to call the superclass' getSalary() method from inside the subclass' getSalary() method you must
type super.getSalary()
31
Every class defined without the "extends" clause automatically extends what?
The class "object". Sometimes referred to as "the cosmic superclass"
32
Tell me about the class object
The "object" class is the direct or indirect superclass of every Java class.
33
Name some methods that are defined in Java "object" class
toString - (String describing the object) equals - (compares objects with each other) hashCode (generates a numerical code)
34
This method is called whenever you concatenate a string with an object
toString
35
Why can the compiler invoke the toString method to concatenate?
It knows that every object has a toString method, since every class the object class
36
When using toString on an object, what happens?
It prints the objects class name, and the hash code of the object BankAccount momsSavings = new BankAccount(5000); String s = momsSavings.toString(); // Sets s to something like "BankAccount@d24606bf"
37
T/F - You can override the toString method to return something more to your liking
True
38
How does equals differ from == ?
Equals checks whether two objects have the same content, == tests whether 2 references are identical.
39
T/F - It's not legal to store a superclass reference in a sublass variable Check this one to ensure it's accurate
False IE. ChoiceQuestion cq = new ChoiceQuestion() Object obj = cq
40
What operator can you use to detect the object type of unknown objects? IE. Object obj
instanceof
41
What is a ClassCast exception?
It's a runtime error (important). When the application attempts to cast an object to another class, of which the original object is not an instance.
42
How can do a safe cast using the instanceof operator?
if (obj instanceof Question) { Question q = (Question)obj; }
43
Why does System.out.println(System.out); produce java.io.PrintStream@7a84e4?
There's no toString() method inside of the PrintStream class
44
Will the following code compile? Object obj = "Hello"; System.out.println(obj.length());
The second line will not compile, as the class Object does not have the method "length"
45
Object obj = "Who was the inventor of Java?”; Question q = (Question) obj; q.display() Will it compile?
It will, but the 2nd line will throw a class cast exception. Question is not a superclass of String.
46
Why do we not store all objects in variables of type object?
There are only a few method that can be invoked on variables of type object.
47
If x is an object reference, what is the value of "x instanceof Object"
If x is null the value is false, true otherwise
48
instanceof returns a ______ value
boolean
49
Can every class be inherited?
No not every class. Any class declared final cannot be inherited.