Unit 9 - Inheritance Flashcards
Superclass
Class hierarchy can be developed by putting common attributes & behaviors of related classes into a single class
Subclasses
Extend a superclass Can draw upon existing attributes & behaviors of superclass without repeating in code
Extending a subclass from a superclass creates an
"is-a" relationship from subclass to superclass (Lower class) is a (Upper class)
Inheritance
Parent classes have attributes & behaviors that can be inherited by child classes
Why use inheritance
Code reusability
Prevents repeating code
Readability & organization
Ease of maintenance
Each subclass can only have
one superclass
All subclasses inherit
attributes and methods of their superclasses
A superclass can’t
inherit attributes and methods of subclass implicitly
keyword extend
Used to establish an inheritance relationship between subclass and a superclass Can only extend one superclass
Constructors are
NOT inherited
When a subclass’s constructor does not explicitly call a superclass’s constructor using super,
Java inserts a call to superclass’s no-argument constructor
Superclass constructor can be called from first line of subclass constructor by using
keyword super and passing appropriate parameters
Actual parameters passed in call to superclass constructor provides
values that constructor can use to intialize the object’s instance variabes
Using super will call
No-argument constructor of superclass MUST be the first line of body of constructor
Whether superclass constructor is called implicitly or explicitly, process of calling superclass constructors continue up
inheritance hierarchy with each constructor calling constructor of superclass until Object constructor is called
Method overriding occurs when
a public method in a subclass has the same method signature as a public method the superclass
Any method that is called must be defined withint tis
own class or superclass
Subclass is usually designed to have
modified or additional methods or instance variables
Subclass inherit all
public methods of superclass and will remain public in subclass
What are options with methods when we extend a superclass?
Inherit methods
Write new methods
Override methods
Inherit methods
Public methods in superclass become valid public methods of subclass Important to acces private instance variables
Write new methods
Additional methods completely independent of methods in superclass Includes overloaded methods and treated as independent methods
Override methods
Write a new or different implementation of a method that already exists in superclass
keyword super
Can be used to call superclass’s constructors and methods
Superclass method can be called in a subclass by using
keyword super with method name and passing appropriate parameters
Subclass
Class that extends another class
Superclass
Class being extended
Polymorphic reference variable
Can refer to objects from different classes at different points in code Can store a reference to its declared class or tan any subclass of its declared class
Why would we declare a variable using a superclass if we plan to store a reference to a subclass object?
A collection needs to be declared as a data type
If s is a subclass of T, then assigning an object of type s to a reference of type t facilitates
polymorphism
T obj = new S(arguments)
If s is a subclass of T, then a reference of type T can be used to refer to
An object of type T or S
Declaring references of type T when s is a subclass of T is useful in followin declarations
Formal method parameters
Arrays T[ ] var or ArrayList ,T> var
Method is considered polymorphic when it is
overridden in at least one subclass
Polymorphism
Act of executing an overridden non-static method from correct class at runtime based on actual object type
Object superclass
Superclass of all other classes in Java
When a class does not expliciting extend another class, then
it implicitly extends Object
Subclasses of Object often
override the equals and toString methods with class specific implementations
When an object is passed as a parameter to the print() or println() method,
object’s toString() method is implicitly called