Inheritance Ch8 Flashcards

1
Q

What word used to create a subclass

A

Extends

Class BoxWeight extends Box

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

What happens when a superclass reference variable references a subclass object?

A

It only has access to the superclass parts of the object

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

How subclass gets private data from superclass

A

Call super() to construct superclass object

super(w,h,d)

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

True or false.

You can only specify one superclass for each subclass.
And super() refers only to the immediate superclass

A

Truth

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

T/F: super() constructor must be the first line if used in subclass constructor?

A

True.

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

What happens if subclass has same method and type signature?

A

It overrides the superclass method

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

Explain abstract class and why use it

A

Superclass with common traits but that the subclasses must override a method.
abstract class Figure
Isn’t a,b
abstract area()

Subclasses override area when define the shape

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

How do you prevent overriding a method?

A

final

final void meth()

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

Can you prevent a class from being inherited?

A

Yes use final
final class Figure

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

Explain the Object class

A

Object is the superclass to all classes.
All other objects are subclasses of Object

Defines the methods for all objects

GetClass
toString
clone
equals

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

what is inheritance

A

building on existing classes to create more specialized classes

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

explain the variable visibility in the subclasses if base class is…

public
protected
private
unspecifid

A

public is accessible by subclass

protected are directly accessible by subclasses only

private are not inherited are not accessible

unspecified are only accessible by classes in same package

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

how would you know if an object is a instance of another object?

A

user instanceof

if (anAnimal instanceof Dog )
((Dog) anAnimal).bark();
else
System.out.print(“anAnimal cant be cast to a Dog”)

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

What is downcasting and why do it?

A

making a superclass object and instance of the subclass. Do this to call a method on the subclass.

(dog) myAnimal.bark()

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

What are some guidelines for inheritance structures

A

look for objects with common attributes and behaviors

factor common items into a single class

determine which subclasses need specialized behaviors

examine groups of subclasses that may exhibit common behaviors

determine which behaviors should be abstract methods

appy the Is-A test

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