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

A

specify

22
Q

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

A

True

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

A

True

25
Q

What is one possible solution when you need to access a private field of the superclass?

A

Use the reserved word “super” to call the required method
super.method(parameters if any)

26
Q

This reserved word forces the execution of the superclass method

A

super

27
Q

Are constructors considered members? What does that entail for inheritance? What is one caveat?

A

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
Q

The super keyword is similar to the ____ keyword. How?

A

this. It differentiates the members of the superclass from the subclass, if they have the same names.

29
Q

Use the _____ keyword to differentiate between sub and super members

A

super

30
Q

If you wish to call the superclass’ getSalary() method from inside the subclass’ getSalary() method you must

A

type super.getSalary()

31
Q

Every class defined without the “extends” clause automatically extends what?

A

The class “object”. Sometimes referred to as “the cosmic superclass”

32
Q

Tell me about the class object

A

The “object” class is the direct or indirect superclass of every Java class.

33
Q

Name some methods that are defined in Java “object” class

A

toString - (String describing the object)
equals - (compares objects with each other)
hashCode (generates a numerical code)

34
Q

This method is called whenever you concatenate a string with an object

A

toString

35
Q

Why can the compiler invoke the toString method to concatenate?

A

It knows that every object has a toString method, since every class the object class

36
Q

When using toString on an object, what happens?

A

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
Q

T/F - You can override the toString method to return something more to your liking

A

True

38
Q

How does equals differ from == ?

A

Equals checks whether two objects have the same content, == tests whether 2 references are identical.

39
Q

T/F - It’s not legal to store a superclass reference in a sublass variable

Check this one to ensure it’s accurate

A

False
IE.

ChoiceQuestion cq = new ChoiceQuestion()
Object obj = cq

40
Q

What operator can you use to detect the object type of unknown objects?

IE. Object obj

A

instanceof

41
Q

What is a ClassCast exception?

A

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
Q

How can do a safe cast using the instanceof operator?

A

if (obj instanceof Question)
{
Question q = (Question)obj;
}

43
Q

Why does System.out.println(System.out); produce java.io.PrintStream@7a84e4?

A

There’s no toString() method inside of the PrintStream class

44
Q

Will the following code compile?

Object obj = “Hello”;
System.out.println(obj.length());

A

The second line will not compile, as the class Object does not have the method “length”

45
Q

Object obj = “Who was the inventor of Java?”;
Question q = (Question) obj;
q.display()

Will it compile?

A

It will, but the 2nd line will throw a class cast exception. Question is not a superclass of String.

46
Q

Why do we not store all objects in variables of type object?

A

There are only a few method that can be invoked on variables of type object.

47
Q

If x is an object reference, what is the value of “x instanceof Object”

A

If x is null the value is false, true otherwise

48
Q

instanceof returns a ______ value

A

boolean

49
Q

Can every class be inherited?

A

No not every class. Any class declared final cannot be inherited.