Inheritance Flashcards
The process of creating derived classes from a base class to enhance code reusability.
Inheritance
Inheritance relationship can be described as
superclass-subclass
is-a/an, is-a-kind-of
has-a
the “is-a” rule states that __________ of the subclass is an object of the superclass
every object
You can use a subclass object whenever the program expects a superclass.
Substitution Principle
What features can be inherited by the subclass?
Public and Protected
The super constructor must always be the ____________ in another constructor.
first statement
The collection of classes extending from a common superclass is called an ________________________
Inheritance Hierarchy
The path from a particular class to its ancestor is its ______________________.
Inheritance chain
When some methods in superclass is not appropriate for the subclasses, the solution is to _________________________
override methods
When designing Classes, place the _______________ in the superclass
general methods
Place the more ______________ in the subclass(es)
specialized methods
Subclasses are more superior because they have more functionality than superclasses
TRUE
Benefits of Inheritance
- Forms ranking or ordering of abstractions
- Simplifies our understanding of a problem
- Allows code reuse
Java employs _______ inheritance
Single
top of hierarchy
java.lang.Object
All classes in the Java Platform are descendants of the _____________
Object class
user within the subclass to refer to its superclass, or to invoke constructor, method or attribute (discouraged) of the super class.
Super keyword
constructors are not inherited.
TRUE
Redeclaring and redefining a method of the superclass in the subclass
Method Overriding
In overriding a method, the _____ and _________ (name, parameter types) should be the same as the original method
return type, method signature
When overriding a method, the access modifier should be the same or less strict.
TRUE
Given the method:
int add (int x, int y);
is the method overriding:
protected int add (int a, int b);
valid?
YES, from default to a less strict access modifier (protected)
With ___________ keyword, there is no need to implement the method (no method body required)
Abstract
For added clarity, a class with one or more abstract methods must itself be declared abstract
TRUE
Other than the abstract methods, an abstract class can have attributes and
concrete methods, just like any normal class
TRUE
are classes designed to become superclasses
Abstract Classes
TRUE or FALSE:
You can instantiate abstract classes.
FASLE
TRUE or FALSE:
When a method is abstract in the superclass, the subclass has the option not to override it.
FALSE. All abstract methods from the superclass must be overridden by the derived class.
are classes designed to be leaf nodes in
the hierarchy. That is, final classes cannot be extended.
Final Classes
________________
cannot be overridden by the
subclasses
Final methods
Problems of Inheritance
Yoyo Problem
* When understanding the behavior of an object whose
class comes from a hierarchy, we might have to go up
and down the hierarchy to trace the behavior.
Dependency Problem
* Stability and existence of a subclass demands the
same from a superclass.
* Therefore when “transporting” a superclass B, it must
be transported with its superclasses.
Size and Efficiency Problem
* Note that subclasses are actually larger than their
superclasses (although they might appear to be
written with less code).
* Sometimes, not all inherited methods are required in a
given problem.
* It does save us programming time but performance
may suffer when dealing with larger than required
objects.
* Therefore, inheritance trees should be as shallow as possible.