Vous êtes sur la page 1sur 18

Inheritance

Lecture 4
Inheritance
• Methods allows a software developer to reuse a
sequence of statements
• Inheritance allows a software developer to reuse
classes by deriving a new class from an existing
one
• The existing class is called the parent class, or
superclass, or base class
• The derived class is called the child class or
subclass.
• As the name implies, the child inherits
characteristics of the parent
• That is, the child class inherits the methods and
data defined for the parent class
28 March 2007 Java : Lecture 3 2
Inheritance
• Inheritance relationships are often shown
graphically in a class diagram, with the arrow
pointing to the parent class
Animal
weight : int
Inheritance should
+ getWeight() : int create an is-a
relationship,
meaning the child
Bird is a more specific
version of the
parent
+ fly() : void
28 March 2007 Java : Lecture 3 3
Deriving Subclasses
• In Java, we use the reserved word extends to
establish an inheritance relationship
class Animal
{
// class contents
int weight;
public void int getWeight() {…}
}

class Bird extends Animal


{
// class contents
public void fly() {…};
}
28 March 2007 Java : Lecture 3 4
Class Hierarchy
• A child class of one parent can be the parent of
another child, forming class hierarchies
Animal

Reptile Bird Mammal

Snake Lizard Parrot Horse Bat

• At the top of the hierarchy there’s a default class


called Object.
28 March 2007 Java : Lecture 3 5
Class Hierarchy
• Good class design puts all common features as high in the hierarchy as
reasonable
• inheritance is transitive
– An instance of class Parrot is also an instance of Bird, an instance
of Animal, …, and an instance of class Object. A child class can
override the definition of an inherited method in favor of its own
– that is, a child can redefine a method that it inherits from its parent
– the new method must have the same signature as the parent's
method, but can have different code in the body
• In java, all methods except of constructors override the methods of
their ancestor class by replacement. E.g.:
– the Animal class has method eat()
– the Bird class has method eat() and Bird extends Animal
– variable b is of class Bird, i.e. Bird b = …
– b.eat() simply invokes the eat() method of the Bird class
• If a method is declared with the final modifier, it cannot be
overridden
28 March 2007 Java : Lecture 3 6
Overloading vs. Overriding
• Overloading deals • Overriding deals with
with multiple methods two methods, one in a
in the same class with parent class and one in
the same name but a child class, that have
different signatures the same signature

• Overloading lets you • Overriding lets you


define a similar define a similar
operation in different operation in different
ways for different data ways for different
object types

28 March 2007 Java : Lecture 3 7


Controlling Inheritance
• Visibility modifiers determine which class
members are accessible and which do not
• Members (variables and methods) declared with
public visibility are accessible, and those with
private visibility are not
• Problem: How to make class/instance variables
visible only to its subclasses?
• Solution: Java provides a third visibility modifier
that helps in inheritance situations: protected

28 March 2007 Java : Lecture 3 8


The protected Modifier
• The protected visibility modifier allows a member of a base class
to be accessed in the child
– protected visibility provides more encapsulation than public does
– protected visibility is not as tightly encapsulated as private
visibility

Book
protected int pages
All these methods can access + getPages() : int
the pages instance variable. + setPages(): void

Note that by constructor Dictionary


chaining rules, pages is an
+ getDefinitions() : int
instance variable of every + setDefinitions(): void
+ computeRatios() : double
object of class Dictionary.
28 March 2007 Java : Lecture 3 9
Constructors
• The general rule is that when a subclass is created
Java will call the superclass constructor first and
then call the subclass constructors in the order
determined by the inheritance hierarchy

• If a superclass does not have a default constructor


with no arguments, the subclass must explicitly
call the superclass constructor with the appropriate
arguments

28 March 2007 Java : Lecture 3 10


Using super( ) Call Constructor
• The call to super must be the first statement in the
subclass constructor

• Example:
class C extends B {

public C ( … ) {
super( B’s constructor arguments );

}

28 March 2007 Java : Lecture 3 11


Creation of Subclass Instances
• Assuming that PreciseClock is a subclass of the
Clock class, the following is legal
Clock dawn;
dawn = new PreciseClock(3,45,30);

• The instance variable dawn will respond to all


PreciseClock messages
• It is not legal to write this since Clock objects
cannot respond to all PreciseClock messages
PreciseClock dawn;
dawn = new Clock(3,40);
28 March 2007 Java : Lecture 3 12
Recap: Class Hierarchy
• Classes are arranged in a hierarchy
• The root, or topmost, class is Object
• Every class but Object has at least one
superclass
• A class may have subclasses
• Each class inherits all the fields and methods
of its (possibly numerous) superclasses
28 March 2007 Java : Lecture 3 13
Object Class
The Object Class
• A class called Object is defined in the java.lang
package of the Java standard class library

• All classes are derived from the Object class


– even if a class is not explicitly defined to be the child of an existing
class, it is assumed to be the child of the Object class
– the Object class is therefore the ultimate root of all class
hierarchies

• The Object class contains a few useful methods, which


are inherited by all classes
– toString()
– equals()
– clone()

28 March 2007 Java : Lecture 3 15


The Object Class: the toString Method
• That’s why the println method can call toString for
any object that is passed to it – all objects are guaranteed to
have a toString method via inheritance

• The toString method in the Object class is defined to


return a string that contains the name of the object’s class
and a hash value

• Every time we have defined toString, we have actually


been overriding it

28 March 2007 Java : Lecture 3 16


The Object Class: the equals Method

• The equals method of the Object class determines


if two variables point to the same object (more
shortly)

• Many classes (which all implicitly extend an


Object class) override equals to define
equality in some other way, e.g.
Integer.equals looks at the represented
integer, etc.

28 March 2007 Java : Lecture 3 17


The Object Class: Other Methods
• finalize() – called by te GC to deallocate the
object’s memory.
• getClass() – returns an object of class Class type
that contains information about the object’s type
such as its class name.
• hashCode() – A hashtable is a DS that relates one
object, called the key, to another object called the
value. When initially inserting a value into a
hashtable, the keys hashCode() is called.

28 March 2007 Java : Lecture 3 18

Vous aimerez peut-être aussi