Tải bản đầy đủ (.ppt) (49 trang)

Chapter 6 Inheritance

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (941.54 KB, 49 trang )

1
Inheritance
Inheritance
Chapter 6
Chapter 6
2
Outline
• Creating subclasses
• Class hierarchies
• The protected modifier
• Overriding methods
• Polymorphism via inheritance
3

Inheritance is the process in which a
new class is derived from an existing one

Inheritance allows a new class to be
written that extends an existing class

The existing class is called the parent
class, or super class, or base class

The derived class is called the child class
or subclass, derived class
A
B
B inherits A
Inheritance
Inheritance
4


Inheritance
Inheritance

The child class inherits characteristics of the parent class

That is, the child class inherits the variables and
methods defined by the parent class

The derivation process should establish a specific kind of
relationship between two classes: an is-a relationship

For example:

Car is-a Vehicle

Dictionary is-a Book
Book
Dictionary
Vehicle
Car
5
Creating subclasses
Creating subclasses

In Java, we use the reserved word extends to establish
an inheritance relationship
class Car extends Vehicle
{
// class contents
}

class Dictionary extends Book
{
// class contents
}
6
Outline
• Creating subclasses
• Class hierarchies
• The protected modifier
• Overriding methods
• Polymorphism via inheritance
7

A child class of one parent can be the parent of another
child

Furthermore, multiple classes can be derived from a
single parent

Therefore, inheritance relationships often develop into
class hierarchies
Class hierarchies
Class hierarchies
8

Two children of the same parent are called siblings

Common features should be put as high in the hierarchy

A child class inherits from all its ancestor classes

Animal
Snake Lizard
Bird
Parrot
Reptile
Class hierarchies
Class hierarchies
10
Example
Example

Cylinder inherits Circle
Circle
Cylinder
11
Example
Example
Circle
- radius : double
+ Circle()
+ Circle(r : double)
+ getRadius() : double
+ setRadius(r : double) : void
+ Area() : double
Cylinder
- length : double
+ Cylinder()
+ Cylinder(r : double, l : double)
+ getLength() : double
+ setLength(l : double) : void

+ Volume() : double
TestCylinder
+ main (args : String[]) : void
Class diagram for TestCylinder
12
Circle.java
Circle.java
public class Circle
{
private double radius;
public Circle()
{
radius = 1.0;
}
public Circle (double r)
{
radius = r;
}
public double getRadius()
{
return radius;
}
public void setRadius (double r)
{
radius = r;
}
public double Area()
{
return radius*radius*Math.PI;
}

}
Circle
- radius : double
+ Circle()
+ Circle(r : double)
+ getRadius() : double
+ setRadius(r : double) : void
+ Area() : double
13
Cylinder.java
Cylinder.java
public class Cylinder extends Circle
{
private double length;
public Cylinder()
{
radius = 1.0; // ??
length = 1.0;
}
public Cylinder (double r, double l)
{
radius = r;
length = l;
}
public double getLength()
{
return length;
}
public void setLength (double l)
{

length = l;
}
public double Volume()
{
return Area() * length;
}
}

Cylinder
- length : double
+ Cylinder()
+ Cylinder(r : double, l : double)
+ getLength() : double
+ setLength(l : double) : void
+ Volume() : double
14
TestCylinder.java
TestCylinder.java
public class TestCylinder
{
public static void main (String[] args)
{
Cylinder c = new Cylinder (5.0, 5.2);
System.out.println(“The radius is “ + c.getRadius());
System.out.println(“The length is “ + c.getLength());
System.out.println(“The area of the circle is “ + c.Area());
System.out.println(“The volume of the cylinder is “ + c.Volume());
}
}
15

The 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

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

Therefore, the Object class is the ultimate root of all
class hierarchies
16

The Object class contains a few useful methods, which
are inherited by all classes

For example, the toString method, the equals method is
defined in the Object class

The toString method in the Object class returns a
string that contains the name of the object’s class along
with some other information

We can override toString method in any class to get a more
meaningful string

The equals method in the Object class returns true if

two references are aliases

We can override equals method in any class to define equality in
some more appropriate way
The Object class
The Object class
28
Outline
• Creating subclasses
• Class hierarchies
• The protected modifier
• Overriding methods
• Polymorphism via inheritance
29
The protected modifier
The protected modifier

Variables and methods declared with private visibility
cannot be referenced in a child class

They can be referenced in the child class if they are
declared with public visibility - but public variables violate
the principle of encapsulation

There is a third visibility modifier that helps in inheritance
situations: protected
30
The protected modifier
The protected modifier


The protected modifier allows a child class to
reference a variable or method directly in the child class

It provides more encapsulation than public visibility, but
is not as tightly encapsulated as private visibility

A protected variable is visible to any class in the
same package as the parent class

Protected variables and methods can be shown
with a # symbol preceding them in UML diagrams
31
Example 1
Example 1
Circle
# radius : double
+ Circle()
+ Circle(r : double)
+ getRadius() : double
+ setRadius(r : double) : void
+ Area() : double
Cylinder
- length : double
+ Cylinder()
+ Cylinder(r : double, l : double)
+ getLength() : double
+ setLength(l : double) : void
+ Volume() : double
TestCylinder
+ main (args : String[]) : void

Class diagram for TestCylinder
32
Circle.java
Circle.java
public class Circle
{
protected double radius;
public Circle()
{
radius = 1.0;
}
public Circle (double r)
{
radius = r;
}
public double getRadius()
{
return radius;
}
public void setRadius (double r)
{
radius = r;
}
public double Area()
{
return radius*radius*Math.PI;
}
}
Circle
# radius : double

+ Circle()
+ Circle(r : double)
+ getRadius() : double
+ setRadius(r : double) : void
+ Area() : double
33
Cylinder.java
Cylinder.java
public class Cylinder extends Circle
{
private double length;
public Cylinder()
{
radius = 1.0;
length = 1.0;
}
public Cylinder (double r, double l)
{
radius = r;
length = l;
}
public double getLength()
{
return length;
}
public void setLength (double l)
{
length = l;
}
public double Volume()

{
return findArea() * length;
}
}

Cylinder
- length : double
+ Cylinder()
+ Cylinder(r : double, l : double)
+ getLength() : double
+ setLength(l : double) : void
+ Volume() : double
34
Example 2
Example 2
Book
# pages : int
+ Book (numPages : int)
+ setPages (numPages : int) : void
+ getPage() : int
Dictionary
- definitions : int
+ Dictionary (numPages : int, numDefinitions :
int)
+ computeRatio() : double
+ setDefinitions (numDefinitions : int) : void
+ getDefinitions() : int
Words
+ main (args : String[]) : void
Class diagram for Words

Class diagram for Words
35
Book.java
Book.java
public class Book
{
protected int pages;
public Book (int numPages)
{
pages = numPages;
}
public void setPages (int numPages)
{
pages = numPages;
}
public int getPages ()
{
return pages;
}
}
Book
# pages : int
+ Book (numPages : int)
+ setPages (numPages: int) : void
+ getPage() : int
36
Dictionary.java
Dictionary.java
public class Dictionary extends Book
{

private int definitions;
public Dictionary (int numPages, int numDefinitions)
{
pages = numPages;
definitions = numDefinitions;
}
public double computeRatio ()
{
return definitions / pages;
}
public void setDefinitions (int numDefinitions)
{
definitions = numDefinitions;
}
public int getDefinitions ()
{
return definitions;
}
}
Dictionary
- definitions : int
+ Dictionary (numPages : int, numDefinitions : int)
+ computeRatio() : double
+ setDefinitions (numDefinitions : int) : void
+ getDefinitions() : int
37
Words.java
Words.java
public class Words
{

public static void main (String[] args)
{
Dictionary w = new Dictionary (1500, 52500);
System.out.println ("Number of pages: " + w.getPages());
System.out.println ("Number of definitions: " + w.getDefinitions());
System.out.println ("Definitions per page: " + w.computeRatio());
}
}

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×