Module 09: Inheritance Flashcards
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
- Create a hierarchy of classes that allow us to reuse common attributes and behaviors
- A subclass uses the “IS A” Relationship signifying that it is a more specific example of the broader superclass
- The keyword extends used to establish the relationship between a superclass and subclass
- A class can only extend one superclass
9.1 Inheritance
How do you create a class hierarchy?
public class Person {
}
public class Student extends Person {
}
- Use keyword extends to create a subclass relationship
- A class can only extend one superclass
- “Person” = class name that is the superclass

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?
- implements
- public
- extends
- subclass
- extends
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?
- User input for the subclass
- Common Attributes and Behaviors
- Private methods only available for the subclass
- All of these
- Common Attributes and Behaviors
9.2 Writing Constructors for Subclasses
Question: 5
Which of the following best describes a Subclass / Superclass relationship?
- Dog / Color
- Dog / Animal
- Dog / Tail
- Dog / Age
- Dog / Animal
9.2 Writing Constructors for Subclasses
What are subclass constructors?
Don’t inherit constructor from the superclass
Need to create its own
- Since constructors are not inherited, a subclass needs to create its own constructor
- Superclass constructor should be called as the first line of the subclass constructor and the parameters to satisfy the superclass constructor should be passed
- No call is made to the superclass constructor, Java inserts a call to the no-argument constructor
- When no superclass is defined, the Object class is the superclass
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
- To call the superclass constructor, use the keyword super on the first line of our subclass constructor, then pass it the required parameters
- Actual parameter passed in the call to superclass and use the superclass constructor can use to initialize the object’s instance variables
- If a no-argument constructor doesn’t exist, then the program will not compile without an explicit call to a constructor

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?
- Don’t extend any class, then by default we are extending Object which is the top of the Java class hierarchy
- Object class constructor takes no arguments, so we don’t need to call it explicitly
Each subclass either implicitly or explicitly call a superclass constructor
- Continues until Object constructor is called
- All constructors are executed beginning with the Object constructor

9.2 Writing Constructors for Subclasses
Question: 1
Does a Subclass need to have a constructor?
- No, it can inherit the constructor from the Superclass.
- Yes, it must always have an explicit constructor to take the parameters of the superclass.
- No, subclasses objects are part of the superclass object, so the superclass constructor is called.
- Yes, the subclass has its own constructor, either explicit or implicitly created.
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?
- Using the super keyword in a separate method within the subclass
- Using the Superclass name in a separate method within the subclass
- Using the super keyword in the first line of the subclass constructor.
- Using the Superclass name in the first line of the subclass 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?
- Java implicitly calls the no-argument superclass constructor
- Java doesn’t create a superclass object
- The program will not compile and will generate an error.
- Java initializes all superclass variables with default values.
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?
- Nothing. Not every class needs to have a superclass.
- The program will not compile and will throw an error.
- The class with the main method becomes the superclass.
- By default, we extend the Object class
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;
}
- Both methods have sam name and input parameters
- Person objects use one method and Student objects use their method
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:
- Methods in Superclass/Subclass
- Methods have the same name and same parameters
Method overload:
- Methods in the same class
- Methods have same name, but different parameters
9.3 Overriding Methods
What is override notation?
Use @Override notation:
@Override
//Student Class: public String toString(){
return super.getName() + “ is in grade “ + grade;
}
- @Override notation not required if it is not included, a method with the same signature as the superclass will still be overriden
Use @Override notation is best practiced for…
- It helps with debugging. When you use @Override the compiler double checks that you correctly override the method from the superclass
- It makes your code more readable since it becomes clear that you are overriding a superclass method
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
- When calling methods for an object, Java first looks in that object class. If it defined in that class, use that method, otherwise it will look to the superclass for the method
- Any method called must be defined within its own class or superclass



















