Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing...
-
Upload
maude-gibbs -
Category
Documents
-
view
216 -
download
0
Transcript of Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing...
Inheritance• Inheritance allows a programmer to derive a new class
from an existing one
• The existing class is called the super class, or parent class, or base class
• The derived class is called the subclass or child class.
• As the name implies, the child inherits characteristics of the parent
• That is, the child class inherits (can use, has access to) the methods and variables defined in the parent class
Inheritance• To tailor a parent class, the programmer can
add new variables or methods, or can modify the inherited ones
• What’s the point of inheritance? Software reuse. It’s somewhat like using a class/client, but works best when there is a “parent-child” relationship.
Inheritance
• Inheritance relationships often are shown graphically in a class diagram, with an arrow with an open arrowhead pointing to the parent class
Inheritance should create an is-a relationship, meaning the child is a more specific version of the
parent
Vehicle
Car
Deriving Subclasses• In Java, we use the reserved word extends to
establish an inheritance relationship
• Demo: Book, Dictionary, InheritanceClient
public class Car extends Vehicle
{
// code
}
• Notice that in the examples, the parent class, Book, has a variable called pages. This makes sense, because all books have pages.
• Also, notice that the child class, Dictionary, has a variable called definitions. A dictionary is-a type of book, so it has pages, and has access to that variable, but only a dictionary has definitions.
• So, when you design child/parent classes, it is important where you place certain variables. It would be a mistake to place pages in Dictionary, or definitions in Book.
Visibility Modifiers• Visibility modifiers determine which class members are
inherited and which are not
• Variables and methods declared with public visibility are inherited
• Variables and methods declared with private visibility are not
• The problem with the Book class is that the pages variable is public. This violates the concept of encapsulation.
• Encapsulation: ALL variables should be private, and if a child class (or a client) wants to use that variable, then there should be a method they can call to access it.
The super Reference• Constructors are not inherited, even though they have
public visibility
• Yet we often want to use the parent's constructor to set up the "parent's part" of the object
• The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor
• Super can be used to call other methods in the parent class as well
• Demo: Book, Dictionary, InheritanceClient
• A child’s constructor is responsible for calling the parent’s constructor
• The first line of a child’s constructor should use the super reference to call the parent’s constructor
• The super reference can also be used to reference other variables and methods defined in the parent’s class
Assignment– Think of an “is-a” relationship in real life, such as
dictionarybook, in which the parent contains a common piece of information (such as pages), and the child contains a more specific piece of information (such as definitions).
– Now, create a parent class, as well as a child class that represents this relationship.
– In the child class, use the super reference to call the parent’s constructor from the child’s constructor.
– Finally, create a client program that tests the child and parent classes.