Inheritance Ch8 Flashcards
What word used to create a subclass
Extends
Class BoxWeight extends Box
What happens when a superclass reference variable references a subclass object?
It only has access to the superclass parts of the object
How subclass gets private data from superclass
Call super() to construct superclass object
super(w,h,d)
True or false.
You can only specify one superclass for each subclass.
And super() refers only to the immediate superclass
Truth
T/F: super() constructor must be the first line if used in subclass constructor?
True.
What happens if subclass has same method and type signature?
It overrides the superclass method
Explain abstract class and why use it
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 do you prevent overriding a method?
final
final void meth()
Can you prevent a class from being inherited?
Yes use final
final class Figure
Explain the Object class
Object is the superclass to all classes.
All other objects are subclasses of Object
Defines the methods for all objects
GetClass
toString
clone
equals
what is inheritance
building on existing classes to create more specialized classes
explain the variable visibility in the subclasses if base class is…
public
protected
private
unspecifid
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 would you know if an object is a instance of another object?
user instanceof
if (anAnimal instanceof Dog )
((Dog) anAnimal).bark();
else
System.out.print(“anAnimal cant be cast to a Dog”)
What is downcasting and why do it?
making a superclass object and instance of the subclass. Do this to call a method on the subclass.
(dog) myAnimal.bark()
What are some guidelines for inheritance structures
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