inheritance examples by lesh
TRANSCRIPT
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 1/25
Inheritance
CS 102 - Algorithms & Programming II
September 29, 2010
c2010 Aybar C. Acar
Some examples from Savitch, “Absolute Java 3Ed”, 2008
Inheritance
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 2/25
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 Snapple
DabbleKibble ZibbleFizzle
Gork Zork Jork
Inheritance
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 3/25
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 notPart-Time.
Employee
HourlyEmployee SalariedEmployee
TechnicalFullTime ExecutivePartTime
Engineer Technician Clerical
Inheritance
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 4/25
Terminology of Objects and Classes
Classes are types of objects. A class is an hypothetical thinguntil an object of that class is created.
Objects are real constructs in memory. All objects mustbelong 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 anobject 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
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 5/25
Inheritance
Class Inheritance allows us to:
Create simple parent objects and to make them more
specialized.
Have specializations share the variables & methods of theircommon ancestors.
Write the code once, and re-use in many children! (Laziness)
Inheritance
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 6/25
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 p ub l ic c la ss E m pl o ye e
2 {
3 p ri va te S tr in g n am e ;
4 p ri va te l on g i dN um be r ;
5 }
Child (derived class):
1 p ub l ic c la ss H o u rl y Em p l oy e e e xt e nd s E m pl o ye e
2 {
3 p r iv at e f lo at h o ur l yR a te ;4 p r iv at e i nt h o ur s Wo r ke d ;
5 }
The HourlyEmployee objects have name and idNumber inherited from
Employee, but they also include new variables: hourlyRate and
hoursWorked.Inheritance
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 7/25
Structure of a Derived Class
this super
Employee
HourlyEmployee
55.45
765
float hourlyRate
int hoursWorked3482942932
"Mehmet Sarıcizmeli"
int idNumber
String name
Every child class isit’s own type and alsoof it’s parent’stype(s).
Creation of a derivedclass involves thecreation of its parent
as well.The derived classholds its parentwithin itself.
e.g. you have your
parents’ DNA.
Each derived objectrefers to its parentversion using the
reference superInheritance
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 8/25
Accessibility in Inheritance
There are three main classes of accessibility, in order of permissiveness:
public : Public variables and methods are accessible byeveryone.
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 itcannot be returned in a method or assigned toanother reference. It can only be used directly.
Inheritance
S
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 9/25
Sandbox:
Assume the following case:
1 c l a s s A {
2 p r i v a t e i n t vA1 ;3 p r o te c t e d f l o a t vA2 ;4 p u b l i c S t r i n g vA3 ;56 p u b l i c v o i d fA1 ( ) { S y st e m . o u t . p r i n t l n ( v A1 ) } ;7 p r ot e ct e d i n t f A 2 ( i n t m) { r e t u r n m ∗ ( i n t ) vA2 } ;8 p r i v a t e b oo le an fA3 ( ) { r e t u r n (vA1 < vA2) } ;
9 }1011 c l a s s B e x t e n d s A {12 p r i v a t e i n t v B 1 = 3 ;13 p u b l i c f l o a t vB2 ;14
15 }1617 c l a s s C {18 p r i v a t e A vC1 ;19 p r i v a t e B vC2 ;20 }
Inheritance
Cl Di
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 10/25
Class Diagrams
Another way of viewing the previous example is with a classdiagram:
+ fA1() : void
# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
Inheritance
I A d H A
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 11/25
Is-A and Has-A
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
A class is said to have an“Is-A” relationship withanother class if it is derivedfrom it.
e.g. Every B is an A
A class is said to have a“Has-A” relationship withanother class if objects of that class hold a referencesto objects of the other class.
e.g. Every C has an Aor, every C has a B.
Inheritance
E i
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 12/25
Exercise
+ fA1() : void# fA2(int) : int
- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
Example
Can we add the following methodto the definition of class C?
1 p u b l i c A f C1 ( )2 {
3 r e t u r n vC1 ;4 }
Yes. The variable vC1 is areference to an object of type
(class) A. An object of class Chas access to its private variablesso it can read them (and returnthem in methods).
Inheritance
E i
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 13/25
Exercise
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
ExampleCan we add the following methodto 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 publicvariable of class A. An object of class C has access to the publicvariables of objects of class A.
Inheritance
E is
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 14/25
Exercise
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
Example
Can we add the following methodto 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 vA1of class A.
Inheritance
Exercise
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 15/25
Exercise
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
Example
Can we add the following methodto 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 cannotaccess protected variables.
Inheritance
Exercise
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 16/25
Exercise
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
Example
Can we add the following methodto 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 classA. It is public so B can access it.
Inheritance
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 17/25
Exercise
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 18/25
Exercise
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
Example
Can we add the following methodto 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 variableof B inherited from parent class
A. But, it is private and even if B is a child of A, B can notaccess it.
Inheritance
Exercise
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 19/25
Exercise
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
ExampleCan we add the following methodto the definition of class C?
1 p u b l i c v oi d fC4 ( )
2 {3 vC2 . fA1 ( ) ;4 }
YES. fA1 is a method of Binherited from parent class A. Itis public so it can be accessed byobjects of class C.
Inheritance
Exercise
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 20/25
Exercise
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
ExampleCan we add the following methodto the definition of class C?
1 p u b l i c i n t f C 5 ( i n t a )
2 {3 r e t u r n vC2 . fA2 ( a ) ;4 }
NO. fA2 is a protected methodof B inherited from parent classA. Since C is not a child of A, itcannot access fA2.
Inheritance
Exercise
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 21/25
Exercise
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
ExampleCan we add the following methodto the definition of class B?
1 p u b l i c i n t f B 4 ( i n t a )
2 {3 r e t u r n f A2 ( a ) ;4 }
YES. fA2 is a protected methodof B inherited from parent classA. Since B is a child of A, it canaccess fA2.
Inheritance
Exercise
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 22/25
Exercise
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
Example
Can we add the following methodto the definition of class B?
1 p u b l i c b oo lea n fB5 ( )
2 {3 r e t u r n f A3 ( ) ;4 }
NO. fA3 is a private method of
A. Although B is a child of A, itcan not access fA3.
Inheritance
Exercise
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 23/25
Exercise
+ fA1() : void# fA2(int) : int
- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
Example
Can we add the following methodto the definition of class B?
1 p u b l i c B f B6 ( )2 {3 r et ur n t h i s ;4 }
YES. this is a private variableof B. B can access and return itsown address.
However, this is sort of useless...If you can call fB6() on someobject, it means you already haveits reference!
Inheritance
Exercise
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 24/25
+ fA1() : void# fA2(int) : int- fA3() : bool
- vA1 : int# vA2 : float+ vA3 : String
A
- vB1 : int+ vB2 : float
B
- vC1 : A- vC2 : B
C
Example
Can we add the following methodto the definition of class B?
1 p u b l i c A f B6 ( )2 {
3 r e t u r n s u p e r ;4 }
NO. super is a private variableof B and normally this would be
fine. But super is an exceptionalcase in that B can use super butcan never give it away.
Inheritance
Overriding Methods
8/18/2019 Inheritance Examples by lesh
http://slidepdf.com/reader/full/inheritance-examples-by-lesh 25/25
g
A method is uniquely defined by it’s name and parameter list.This is called the methods signature (or fingerprint )
When a class defines two methods with the same name butdifferent parameters, this is called overloading
e.g.
1 i n t f o o ( S t r i n g s )2 i n t fo o ( f l o a t f )
Java will not allow two different methods with the samesignature in the same class.How ever a child class can define a new method with thesame signature as that of its parent class.
Assume class A has:
1 p u b l ic v oi d fA1 ( ) { S y st e m . o u t . p r i n t l n ( v A1 ) ; }
Child class B can define:
1 p u b l ic v oi d fA1 ( ) { S y st e m . o u t . p r i n t l n ( v A2 ∗2 ) ; }
This is called overriding.
Inheritance