Você está na página 1de 25

Inheritance

CS 102 - Algorithms & Programming II

September 29, 2010

c
2010
Aybar C. Acar
Some examples from Savitch, Absolute Java 3Ed, 2008
Inheritance

A Puzzle...
All Kobbles are also Popples. Some Popples are Snapples.
Zibbles are always Snapples. Kobbles are never Snapples.
Dabbles are Snapples that are not Zibbles.
Some Kobbles are Fizzles.
Dabbles are either Gorks, Zorks or Jorks.
Kibbles are Kobbles but not Fizzles.
Popple

Kobble

Kibble

Snapple

Fizzle

Gork

Dabble

Zork

Inheritance

Zibble

Jork

Same Puzzle...
All HourlyEmployees are also Employees.
Some Employees are Salaried.
Executives are always Salaried.
HourlyEmployees are never Salaried.
Technical staff are Salaried Emploees that are not Executives.
Some Hourly employees are Part-Time.
Technical staff are either Engineers, Technicians or Clerks.
FullTime hourly employees are Hourly Employees but not
Part-Time.
Employee

HourlyEmployee

FullTime

SalariedEmployee

PartTime

Engineer

Technical

Technician

Executive

Clerical

Inheritance

Terminology of Objects and Classes


Classes are types of objects. A class is an hypothetical thing
until an object of that class is created.
Objects are real constructs in memory. All objects must
belong to a type (e.g. a class)
References are names for objects of a given type.
So,
Objects are created (or constructed, or instantiated).
Classes are defined.
References are declared, followed and assigned.
Therefore:
You never create a class, you create/instantiate/construct an
object of that class.
Creation always involves a new somewhere...

A reference can only point to an object of its declared type


(class) or to nothing at all (null).
You never have a reference to a class.
Inheritance

Inheritance

Class Inheritance allows us to:


Create simple parent objects and to make them more
specialized.
Have specializations share the variables & methods of their
common ancestors.
Write the code once, and re-use in many children! (Laziness)
Inheritance

Deriving Classes in Java


Derived class, subclass, child class all mean the same thing.
Base class, superclass, parent class all mean the same thing.
Base Class:
1
2
3
4
5

public class Employee


{
private String name ;
private long idNumber ;
}
Child (derived class):

1
2
3
4
5

public class HourlyEmployee extends Employee


{
private float hourlyRate ;
private int hoursWorked ;
}
The HourlyEmployee objects have name and idNumber inherited from
Employee, but they also include new variables: hourlyRate and
hoursWorked.
Inheritance

Structure of a Derived Class


Every child class is
its own type and also
of its parents
type(s).
HourlyEmployee
this

float hourlyRate
55.45
int hoursWorked
765

super

Employee

int idNumber
3482942932
String name
"Mehmet Sarcizmeli"

Creation of a derived
class involves the
creation of its parent
as well.
The derived class
holds its parent
within itself.
e.g. you have your
parents DNA.

Each derived object


refers to its parent
version using the
reference super
Inheritance

Accessibility in Inheritance
There are three main classes of accessibility, in order of
permissiveness:
public : Public variables and methods are accessible by
everyone.
protected : Protected variables and methods are only accesible
by the class and its derived classes.
private : Private variables and methods are accessible only
by the class itself.
super and this are always private.
super is even more restrictive than private as it
cannot be returned in a method or assigned to
another reference. It can only be used directly.

Inheritance

Sandbox:
Assume the following case:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

class A {
p r i v a t e i n t vA1 ;
p r o t e c t e d f l o a t vA2 ;
p u b l i c S t r i n g vA3 ;
p u b l i c v o i d fA1 ( ) { System . o u t . p r i n t l n ( vA1 ) } ;
p r o t e c t e d i n t fA2 ( i n t m) { r e t u r n m ( i n t ) vA2 } ;
p r i v a t e b o o l e a n fA3 ( ) { r e t u r n ( vA1 < vA2 ) } ;
}
class B extends A {
p r i v a t e i n t vB1 = 3 ;
p u b l i c f l o a t vB2 ;
}
class C {
p r i v a t e A vC1 ;
p r i v a t e B vC2 ;
}

Inheritance

Class Diagrams
Another way of viewing the previous example is with a class
diagram:
A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

C
- vC1 : A
- vC2 : B

B
- vB1 : int
+ vB2 : float

Inheritance

Is-A and Has-A

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

A class is said to have an


Is-A relationship with
another class if it is derived
from it.
e.g. Every B is an A

A class is said to have a


Has-A relationship with
another class if objects of
that class hold a references
to objects of the other class.
e.g. Every C has an A
or, every C has a B.

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class C?
1 p u b l i c A fC1 ( )
2 {
3
r e t u r n vC1 ;
4 }
Yes. The variable vC1 is a
reference to an object of type
(class) A. An object of class C
has access to its private variables
so it can read them (and return
them in methods).

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class C?
1 p u b l i c S t r i n g fC2 ( )
2 {
3
r e t u r n vC1 . vA3 ;
4 }
Yes. The variable vA1 is a public
variable of class A. An object of
class C has access to the public
variables of objects of class A.

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class C?
1 p u b l i c i n t fC3 ( )
2 {
3
r e t u r n vC1 . vA1 ;
4 }
NO. Class C does not have
access to the private variable vA1
of class A.

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class C?
1 p u b l i c f l o a t fC4 ( )
2 {
3
r e t u r n vC1 . vA2 ;
4 }
NO. Class C is not a child of
class A. Therefore it cannot
access protected variables.

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class B?
1 p u b l i c S t r i n g fB1 ( )
2 {
3
r e t u r n vA3 ;
4 }
YES. vA3 is an instance variable
of B inherited from parent class
A. It is public so B can access it.

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class B?
1 p u b l i c f l o a t fB2 ( )
2 {
3
r e t u r n vA2 ;
4 }
YES. vA2 is an instance variable
of B inherited from parent class
A. It is protected and B is a child
of A, so B can access it.

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class B?
1 p u b l i c i n t fB3 ( )
2 {
3
r e t u r n vA1 ;
4 }
NO. vA1 is an instance variable
of B inherited from parent class
A. But, it is private and even if
B is a child of A, B can not
access it.

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class C?
1 p u b l i c v o i d fC4 ( )
2 {
3
vC2 . fA1 ( ) ;
4 }
YES. fA1 is a method of B
inherited from parent class A. It
is public so it can be accessed by
objects of class C.

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class C?
1 p u b l i c i n t fC5 ( i n t a )
2 {
3
r e t u r n vC2 . fA2 ( a ) ;
4 }
NO. fA2 is a protected method
of B inherited from parent class
A. Since C is not a child of A, it
cannot access fA2.

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class B?
1 p u b l i c i n t fB4 ( i n t a )
2 {
3
r e t u r n fA2 ( a ) ;
4 }
YES. fA2 is a protected method
of B inherited from parent class
A. Since B is a child of A, it can
access fA2.

Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class B?
1 p u b l i c boolean fB5 ( )
2 {
3
r e t u r n fA3 ( ) ;
4 }
NO. fA3 is a private method of
A. Although B is a child of A, it
can not access fA3.

Inheritance

Exercise
Example
Can we add the following method
to the definition of class B?
A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

1 p u b l i c B fB6 ( )
2 {
3
return t h i s ;
4 }
YES. this is a private variable
of B. B can access and return its
own address.
However, this is sort of useless...
If you can call fB6() on some
object, it means you already have
its reference!
Inheritance

Exercise

A
- vA1 : int
# vA2 : float
+ vA3 : String
+ fA1() : void
# fA2(int) : int
- fA3() : bool

B
- vB1 : int
+ vB2 : float

C
- vC1 : A
- vC2 : B

Example
Can we add the following method
to the definition of class B?
1 p u b l i c A fB6 ( )
2 {
3
return super ;
4 }
NO. super is a private variable
of B and normally this would be
fine. But super is an exceptional
case in that B can use super but
can never give it away.

Inheritance

Overriding Methods
A method is uniquely defined by its name and parameter list.
This is called the methods signature (or fingerprint)

When a class defines two methods with the same name but
different parameters, this is called overloading
e.g.
1
2

int foo ( String s )


int foo ( float f )

Java will not allow two different methods with the same
signature in the same class.
How ever a child class can define a new method with the
same signature as that of its parent class.
Assume class A has:
1

p u b l i c v o i d fA1 ( ) { System . o u t . p r i n t l n ( vA1 ) ; }


Child class B can define:

p u b l i c v o i d fA1 ( ) { System . o u t . p r i n t l n ( vA2 2 ) ; }

This is called overriding.


Inheritance

Você também pode gostar