Chapter 14 - Inheritance Flashcards

1
Q

What class is a superclass for all other classes?

A

The Object class

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

What is unique about the Object class compared to other superclasses?

A

You don’t have to specifiy it as a superclass, it is automatically a superclass.

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

What are the two most important Object methods?

A

equals and toString

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

If a class doesn’t have it’s own equals method, but an object from that class calls the equals method what happens?

A

It inherits and uses the Object class’s equals method.

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

What does the Object class’s equal method return if two reference variables that are being compared point to the same object?

A

The method returns true.

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

What operator works the same as the Object class’s equals method?

A

==

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

Why is the Object class’s equals method usually not good enough?

A

Because you usually want to compare the contents of two objects rather than just whether two reference variables point to the same object.

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

How do you compare the contents of two objects using the Object class’s equals method?

A

You need to have an equals method in the object’s class definition that compares the contents of the two objects.

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

The equals method is built into a lot of Java’s API classes?

T or F

A

True

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

What method in the Object class returns a string that’s a concatenation of the calling object’s class name, an @sign, and a sequence of digits and letters (called a hashcode)?

A

The toString method

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

What does toString prefix if a clas is stored in a package?

A

The class name with the class’s package.

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

Since retrieving the class name, an @ sign, and a hashcode is usually worthless, we want to almost always avoid calling the Object class’s toString method. What do we call instead?

A

An overriding toString method

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

In general, what should a toString method return?

A

A string that descirbes the calling object’s contents.

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

Since retrieving the contents of an object is a common need, what habit should a programmer get into?

A

Proving a toString method for most programmer-defined classes.

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

What should a programmer’s toString method typically do?

A

Simply concatenate the calling object’s store data and return the resulting string.

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

What should a toString method NOT do?

A

It should NOT print the concatenated string value, it should just RETURN it.

17
Q

When does the JVM automatically call the toString method?

A

When a reference variable is an argument in a System.out.print, println, or printf call.

Example:

System.out.println(car);

18
Q

What is a second circumstance that JVM will automatically call the toString method?

A

When a reference variable is concatenated (+ operator) to a string

Example:

String carInfo = “Car data:\n” + car;

19
Q

What is the standard method-call syntax for the toString method?

A

car.toString();

20
Q

What does the toString method do in regards to the primitive wrapper classes?

A

Returns a string representation of the given primitive value.

Example:

Integer.toString(22) : evaluates to string “22”

Double.toString(123.45) : evaluates to string “123.45”

21
Q

What is polymorphism?

A

When different types of objects respond differently to the same method call.

22
Q

How do you implement polymorphic behavior?

A

Declare a general type of reference variable that is able to refer to objects of different types.

23
Q

How do you declare a “general type of reference variable”?

A

Use a superclass (like the Object superclass)

24
Q

Since polymorphism is a concept, what is dynamic binding?

A

A description of how that concept is implemented.

25
Q

How does JVM use dynamic binding?

A

It uses it in order to match up a polymorphic method call with a particular method.

26
Q

What does the JVM do right before it execute’s a method call?

A

It looks at the method call’s calling object. More specifically, it looks at the type of the object that’s been assigned into the calling object’s reference variable.

27
Q

After the JVM looks at a method’s object what occurs through dynamic binding?

A

If the assigned object is from class X, the JVM binds class X’s method to the method call. If the assigned object is from class Y, the JVM bind class Y’s method to the method call. After the JVM bind the appropriate method to the method call, the JVM execute the bound method.

28
Q

What happens when a compiler sees a method call, “reference-variable”.”method-name”()?

A

It checks to see if the reference variable’s class contains a method definition for the called method.

29
Q

Normally, when an object is assigned into a reference variable, the object’s class and the reference variable’s class are the same. How can a different assignment work?

A

Such assignments only work if the right side’s class is a subclass of the left side’s class.

30
Q

When is polymorphism the most useful?

A

When you have an array of generic reference variables and assign different types of objects to different elements in the array.

31
Q

What does polymorphism with Arrays allow?

A

The ability to step through the array and for each array element, you call a polymorphic method.

32
Q

How does the JVM pick out the particular methods that apply to different types of objects that are in the array?

A

At runtime, the JVM use dynamic binding.

33
Q

When do you declare a method to be abstract?

A

If the method’s class is a superclass and the method is merely a “dummy” method for an overriding method(s) in a subclass(es).

34
Q

What does Java require when you define a method to be abstract?

A
  • Use an abstract method heading instead of a method definition. An abstract method heading is the same as a standard method heading except that it includes the abstract modifier and a trailing semicolon.
  • Define an overriding version of that method in each of the superclass’s subclasses.
  • Define the superclass to be abstract by using the abstract modifier.
35
Q

What are you telling the computer by defining a class to be abstract?

A

You’re telling the compiler to not allow the class to be instantiated

Example; if a program attempts to instantiate an abstract class, a compiliation error will be generated.

36
Q

Where does the protected access modifier lie in terms of accessibility?

A

It lies in between public and private in terms of how accessible something is.

37
Q

When can protected members (instance variables, class variables and methods) be accessed?

A

Only by classes within the subtree associated with the member’s class.

38
Q

When do you use the protected access modifier?

A
  • When you want a member to be directly accessible from any clas within its subtree of classes, but you don’t wan it to be accessible elsewhere.
39
Q

Protected example

A