9.1 Inheritance
What is a “Has A” Relationship?
Represent instance variables in our class
9.1 Inheritance
What is a “Is A” Relationship?
A relationship where one class is a more specific example of another class
(not symmetrical)
9.1 Inheritance
What is a superclass and how does it relate to a subclass?
📌 Extending a subclass from a superclass creates an “IS A” relationship from the subclass to the superclass
9.1 Inheritance
How do you create a class hierarchy?
public class Person {
}
public class Student extends Person {
}

9.1 Inheritance
How do you create a multi-level class hierarchy?
public class Person {
}
public class Student extends Person {
}
public class HighSchoolStudent extends Student {
}
Extending a class that has been extended is okay
9.1 Inheritance
Question: 1
When we talk about two classes having a Superclass/Subclass relationship, which type of relationship do we have?
Is A
Has A
Is A
A subclass can be described using a ‘is a’ relationship, such as a Student is a Person
9.1 Inheritance
Question 2
Which keyword do we use to establish the inheritance relationship between a subclass and a superclass?
9.1 Inheritance
Question: 3
True or False: ‘Is A’ relationships are symmetrical.
True
False
False
9.1 Inheritance
Question: 4
What types of things do we put in the Superclass?
9.2 Writing Constructors for Subclasses
Question: 5
Which of the following best describes a Subclass / Superclass relationship?
9.2 Writing Constructors for Subclasses
What are subclass constructors?
Don’t inherit constructor from the superclass
Need to create its own
9.2 Writing Constructors for Subclasses
How does the subclass constructor call superclass?
Must call the parent constructor
When a subclass is created, a superclass object is also created
Subclass must make a call to the superclass constructor when it exists

9.2 Writing Constructors for Subclasses
What are subclasses parameters?
Even though a subclass has to pass parameters to the superclass, they don’t need to take them as parameters for their constructor

9.2 Writing Constructors for Subclasses
How are implicit calls to super constructor?
When non-argument student object is created, the no-argument Person class will be called

9.2 Writing Constructors for Subclasses
What if we don’t extend any class?
Each subclass either implicitly or explicitly call a superclass constructor

9.2 Writing Constructors for Subclasses
Question: 1
Does a Subclass need to have a constructor?
4. Yes, the subclass has its own constructor, either explicit or implicitly created
9.2 Writing Constructors for Subclasses
Question: 2
How does a Subclass call the Superclass constructor?
3. Using the super keyword in the first line of the subclass constructor
9.2 Writing Constructors for Subclasses
Question: 3
If a superclass constructor exists and a subclass doesn’t explicitly call the that constructor, what happens?
1. Java implicitly calls the no-argument superclass constructor
9.2 Writing Constructors for Subclasses
Question: 4
If a class does not extend any class, what happens?
4. By default, we extend the Object class
9.2 Writing Constructors for Subclasses
Question: 5
True or False: A subclass can be extended by another class to become a superclass.
True
9.3 Overriding Methods
What is Method Override?
💻 Method Overrides allow a subclass to redefine a method instead of inheriting that method form superclass
When a public method in a subclass has the same method signature as a public method
//Person Class:
public String toString(){
return name + “ was born “ + birthday;
}
//Student Class:
public String toString(){
return super.getName() + “ is in grade “ + grade;
}
Any method called by object needs to be defined someplace in class hierarchy
Subclass inherits all public methods from the superclass. It can then add additional variables and/or methods, or modify them with an override
Methods overriding occurs when a public method in a subclass has the same method signature as a public method in the superclass
9.3 Overriding Methods
What is the difference between override and overload?
Different implementations
Method override:
Method overload:
9.3 Overriding Methods
What is override notation?
Use @Override notation:
@Override
//Student Class: public String toString(){
return super.getName() + “ is in grade “ + grade;
}
Use @Override notation is best practiced for…
9.3 Overriding Methods
What are class hierarchy considerations?
💻 A subclass is usually designed to have modified (overridden) methods, or additional methods and instance variables