Inheritance|Polymorphism Flashcards
What does a child class do?
inherits the accessible state and behavior of its
parent class.
○ private members are not accessible to the child class.
○ Child classes can access protected members in the parent.
○ Constructors are not inherited. The child class must define its own.
● Inheritance establishes an “is a” relationship between two classes.
○ The child is an instance of the parent.
What does inheritance allow?
Allows you to derive a specialized class from a more generalized class (parent, superclass, base class)
Derived class is the child class or subclass.
Child class inherits all states and behaviors, but adds specialized ones, can also modifying existing.
What is subclassing?
Refers to creating a child class that inherits parent class.
Using extends keywords:
public class Mammal extends Chordate {
}
Constructors are not inherited.
When do you use the super keyword?
If the parent class defines at least one constructor with parameters, the child class must invoke one of the constructors.
public class Child extends Parent {
private String name;
public Child(int x, String name) {
super(x);
this.name = name;
}}
super must be first line of constructor
What does Polymorphism mean?
A parent class can morph and access the state and behavior of it’s child.
A child class can be used anywhere it’s parent class is accepted.