Ch.11 Flashcards
Inheritance and Polymorphism
What is Inheritance?
Defining a new class by extending an existing class. (like child class from parent class)
What is a Superclass?
AKA “parent class”,
A general class that can be used to create specialized classes (subclasses).
What is a Subclass?
AKA “child class”,
A specialized class that is created using a general class (Superclass).
What are Subtypes and Supertypes?
Ex: Circle is “subtype” of GeometricObject,
GeometricObject is “supertype” for Circle.
How do you write the class declaration for a Subtype?
public class Subclass extends Superclass
Is it possible to change parent/supertype data fields from the child/subtype class?
Yes, using the parent/supertype’s public methods: getter/setter/etc
Multiple Inheritance
Capability NOT ALLOWED BY JAVA.
Means a subclass can be derived from multiple classes.
Single Inheritance
Restriction only allowing a subclass to be derived from a single superclass.
“super” Keyword
Refers to the superclass of the class the keyword appears in. Used to call the superclass constructor and reference superclass’s accessible(public) members.
Calling Superclass Constructors from Subclass
Superclass constructors aren’t inherited by a subclass. They can only be invoked by subclass constructors using keyword ‘super’.
Ex: super() or super(arguments);
What Superclass Constructor is invoked automatically by a Subclass Constructor?
The Superclass Default Constructor.
super();
Why should every class contain a no-arg/default constructor?
It’s easier to extend the class and avoid errors with reuse.
What constructor does a subclass invoke first?
The superclass constructor.
If that superclass is derived from its own superclass, then that ‘ancestor’ class would be invoked first.
Constructor Chaining
A subclass invokes its superclass’s constructor before it performs other tasks.
If that superclass has its own superclass, then that parent class would also invoke the parent class before doing its own functions.
Calling Superclass Methods
super.method(arguments);