9.1 Inheritance
Large classes often rely on several different instance variables in order to successfully create comprehensive programming solutions. For example, a Person
class might have several instance variables, like name
or birthday
. These instance variables are said to have a “Has-A” relationship to their class objects. A Person
has a name, and has a birthday. Values that use a “has a” relationship represent instance variables in our projects.
Another type of relationship in object-oriented programming is the “Is-A” relationship. “Is-A” is used when one thing is a more specific version of another thing. As an example, think about the relationship of a Student
to a Person
. A Student
does not have a Person
, but rather, a Student
is a Person
. In this example, a Student
is a more specific example of a Person
. While it’s possible to say that every Student
is a Person
, it’s not possible to say that every Person
is a Student
. The Person
class is a more general class and the Student
class is a more specific class. Some other examples of is-a relationships are:
- A Dog is an Animal.
- A Square is a Rectangle.
- San Francisco is a City.
- Computer science is a Class.
Because many classes, like Person
and Student
have an is-a relationship, they share some common attributes. Just as a Person
has a name, and a birthday, a Student
has the same attributes. Since these two classes share many of the same attributes, there should be a way to consolidate these two classes so as to avoid writing the same attributes and instance variables in each class.
In Java, the is-a relationship is expressed in the form of superclasses and subclasses. A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass. In the current example, the attributes name
and birthday
belong to both Person
and Student
, so they should be written in a superclass.
Subclasses inherit the public attributes and behaviors from the superclass. As a result, a subclass object can use any public attribute or behavior without having to repeat these in its code. A superclass object does not have access to the attributes and behaviors of the subclass class. In this case, a Student
object will have access to all the attributes and behaviors of both the Student
class, like GPA and grade level, and the Person
class, but the Person
object will only have access to the attributes and behaviors of the Person
class.
To create a superclass/subclass relationship, the keyword extends
is added to a class header followed by the name of the superclass:
This creates the subclass. Student
, in this case, is a subclass of Person
, which is the superclass. Subclasses are only capable of extending a single superclass. While a class can only extend one class, it can extend a class that has already been extended:
In this case, a HighSchoolStudent
object will have access to all the attributes and behaviors of the HighSchoolStudent
class, the Student
class, and the Person
class.
As programs become more complicated, oftentimes creating a hierarchy of classes allows for the reuse of common attributes and behaviors. Figuring out which variables and methods are shared across class implementations is the first step to developing a successful hierarchical structure.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Which of the following best describes a Subclass / Superclass relationship?
For this exercise, you are going to design 3 classes:
Computer
- SuperclassLaptop
- SubclassDesktop
- Subclass
You will design these classes to optimize the superclass/subclass relationship by creating instance variables and getter/setter methods.
Include the following instance variables:
int screenSize
- Inches of monitor spaceint memory
- GB of ramdouble batteryLife
- Hours of battery lifeboolean monitor
- Whether or not a monitor is included
Each class should have at least one variable in it.
Once completed, the Tester
class should execute without error.