Java TOPIC – INHERITANCE & POLYMORPHISM I LESSON NOTE TERMINOLOGY In Java, we
can make a class (subclass) have
all of the properties and functionality of another class (superclass). This class inherits all of the other class’
instance variables and instance methods.
We can then add more instance variables, constructors and
methods. This concept is called inheritance. Let’s
consider a real world scenario for a moment.
We will consider the four classes:
Notice that
as we move down the list, we are getting more and more specific in terms of
the animal. So, a Bulldog
has all characteristics of a Dog. A
Victorian Bulldog has all of the characteristics of a Domestic Animal. Notice that
the opposite is not true. For example,
a Domestice Animal does not have all of the
characteristics of a Dog. THE IS A RELATIONSHIP Since a Dog
has all of the characteristics of a Domestic Animal, we can say that a Dog is a Domestic Animal. Again, note that we cannot say the opposite
because a Domestic Animal is not necessarily a Dog.
A simple
class hierarchy diagram shows how different classes are related to one
another. For the four classes from the
example above, the diagram would like like the
following:
Of course,
not all class diagrams are this simple.
If we added a Cat class to the above diagram, we would then have two
classes under Domestic Animal.
HOW DOES ALL OF THIS
RELATE TO JAVA? In Java, we
can create a class that has all of the characteristics of another. In other words, a class will get all of the
instance variabless and methods of another
class. When we do this, it is called inheritance. Inheritance
allows us to build complex programs incrementally without having to
understand the implementation of all the classes. It also allows us to have a nice
consistency between classes that inherit from the same superclass. TERMINOLOGY The class
that inherits functionality is called the subclass (or child class). The class
that provides the functionality is called the superclass (or parent
class). In the class
diagram, the superclass appears directly above the subclass. SPECIFYING INHERITANCE
IN JAVA To specify
that a class should inherit from another class, we simply use the extends
keyword at the end of the line that creates the class. Like this:
public class SubclassName
extends SuperclassName Any objects
of type Subclass now inherits all the functionality and properties (methods
and instance variables) of objects of type Superclass. The Subclass
should also contain: 1 – its own instance variables 2 – constructors 3 – new methods (including ones
overriding superclass methods) SUBCLASS TEMPLATE Below is the
general organization of a Subclass.
Note that the method and overriding methods can be mixed in any order. CONSTRUCTORS IN
SUBCLASSES Subclasses
must have their own constructor.
However, on the first line of their constructors, they must call the superclasses’ constructor and let that constructor take
care of initializing the superclass’ instance variables. To call the
superclass constructor, we use:
super(argList); where the argument list must match one
of the constructors in the superclass.
We have
finally looked at enough concepts to look at an example. Let’s
consider two classes: Person and Student.
First, we need to determine if inheritance makes sense here. The answer is yes,
we can make use of inheritance because a Student is a Person. So Person is the superclass and student is
the subclass. So let’s
start with the Person class. Because
it is the superclass, nothing is different here compared to before. public class Person { public String name; public int age; public Person(String
n, int a) { name = n; age = a; } public String toString() { return name + " is " + age +
" years old."; } } We will now
create the Student class which will inherit from the Person class. So the first line will be: public class Student
extends Person We will add
our instance variables like normally. public
int studentNumber; public
String school; The next part
that is different is the constructor. The parameter
list for the constructor receives values for all instance variables
(including the inherited ones). On the
first line, we must call the superclass’ constructor and pass any required
arguments to it. Then, the constructor
takes care of initializing the instance variables in the subclass. public Student(String mName, int mAge, String mStudentNumber,
String mSchool) { super(mName, mAge); //call the superconstructor studentNumber = mStudentNumber;
school = mSchool; } We now add
any methods that we want. Remember
that the methods have access to the instance variables, including the
inherited ones (unless they were private – but more on this later). Here is the
entire class with the parts related to inheritance in blue. Notice that very little is in blue.
public class Student extends Person { public int studentNumber; public String school; public Student(String mName, int mAge, String mStudentNumber,
String mSchool) { super(mName, mAge);
//call the superconstructor studentNumber
= mStudentNumber; school = mSchool; } public String toString() { return name + " is " + age + " years old and attends " +
school + " as student # " + studentNumber; }
} |