Title: Advance concepts in OOP
1Advance concepts in OOP
- We discuss fundamental object-oriented techniques
that supports re-use of software - Interface and polymorphhism
- Inheritance
- Method overriding
- Polymorphism, widening narrowing
- protected and final modifiers
syllabus
basic programming concepts
object oriented programming
topics in computer science
2Principles of object-oriented programming
- encapsulation data is protected from external
users - inheritance properties and methods defined for a
certain structures can be used by all structures
defined as direct offspring - polymorphism the same function call may invoke
different code, depending on context
3Interfaces
- A Java interface is a collection of abstract
methods and constants - An abstract method is a method header without a
method body
An
abstract method can be declared using the
modifier abstract, but because all methods in an
interface are abstract, it is usually left off - An interface is used to formally define a set of
methods that a class will implement
4Interface example
None of the methods in an interface are given a
definition (body)
public interface Doable public void
doThis() public int doThat() public
void doThis2 (float value, char ch) public
boolean doTheOther (int num)
5Interfaces
- An interface cannot be instantiated
- Methods in an interface are always public and
abstract - Constant in an interface are always public,
static and final - A class formally implements an interface by
- stating so in the class header
- providing implementations for each abstract
method in the interface - If a class declares that it implements an
interface, it must define all methods in the
interface or the compiler will produce errors
6Implementing interface example
public class CanDo implements Doable public
void doThis () // whatever
public void doThat () // whatever
// etc.
7Interfaces
- A class that implements an interface can
implement other methods as well - A class can implement multiple interfaces the
interfaces are listed in the implements clause,
separated by commas - The class must implement all methods in all
interfaces listed in the header
8Polymorphism via Interfaces
- An interface name can be used as the type of an
object reference variable - Doable obj
- The obj reference can be used to point to any
object of any class that implements the Doable
interface - The version of doThis that the following line
invokes depends on the type of object that obj is
referring to - obj.doThis()
9Example Animate Interface
- //
- // Animate.java
- //
- // Demonstrates the declaration of an interface
- //
- public interface Animate
-
- int HIND_LIMBS_NUM 2
- int FORE_LIMBS_NUM 2
- public void sound ()
- public void gait (String type)
-
10// Parrot.java class Parrot implements
Animate private String word
//---------------------------------- //
Establish Parrot's vocabulary
//---------------------------------- public
Parrot (String word) this.word word
//---------------------------------- //
Prints this parrot's sound.
//---------------------------------- public
void sound () System.out.println
("This parrot says " word)
//---------------------------------- //
Prints this parrot's gait //-------------------
--------------- public void gait (String
gaitType) System.out.println ("Parrots
" gaitType " on " HIND_LIMBS_NUM "
limbs")
11// Dog.java class Dog implements Animate
private String name private String owner
//---------------------------------- //
Establish Dog's id //--------------------------
-------- public Dog () name
"Lassi" owner "John"
//---------------------------------- //
Prints this dog's sound. //--------------------
-------------- public void sound ()
System.out.println ("woof")
//---------------------------------- //
Prints this dog's gait //----------------------
------------ public void gait (String
gaitType) System.out.println ("Dog "
name " " gaitType " on "
(HIND_LIMBS_NUMFORE_LIMBS_NUM) " limbs")
//---------------------------------- //
Prints unique contact information
//---------------------------------- public
void contact() System.out.println ("If
you find me, call " owner)
12// Pets.java class Pets //------------------
-----------------------------------------------
// Instantiates two objects using an interface
reference and invokes one of the common //
methods. Then casts interface reference into a
class reference to invoke its unique method
//------------------------------------------------
----------------- public static void main
(String args) Animate current
current new Parrot("hello")
current.sound() current.gait("walk")
System.out.println("\n")
current new Dog() current.sound()
current.gait("runs") ((Dog)
current).contact()
This parrot says hello
Parrots walk on 2 limbs
woof
Dog Lassi runs on 4 limbs
If you find me, call John
13Polymorphism via Interfaces
- Interface reference is polymorphic, which can be
defined as "having many forms" - The corresponding line of code may execute
different methods at different times if the
object that obj points to changes - Note that polymorphic references must be resolved
at run time this is called dynamic binding - Careful use of polymorphic references can lead to
elegant, robust software designs
14Review Interfaces and polymorphism
- A Java interface is a collection of abstract
methods and constants an interface is used to
formally define a set of methods that a class
will implement - Interface reference is polymorphic, which can be
defined as "having many forms" - The corresponding line of code may execute
different methods at different times if the
object that obj points to changes
15Standard Interfaces
- The Java standard class library contains many
interfaces that are helpful in certain situations - The Comparable interface contains an abstract
method called compareTo, which is used to compare
two objects - The String class implements Comparable which
gives us the ability to put strings in
alphabetical order - The Iterator interface contains methods that
allow the user to move through a collection of
objects easily
16Example Sorting Objects
- Integers have an inherent order, but the order of
a set of objects must be defined by the person
defining the class - We can use the Comparable interface to develop a
generic sort for a set of objects
17class Contact implements Comparable private
String firstName, lastName, phone // Uses
both last and first names to determine lexical
ordering. public int compareTo (Object other)
int result if (lastName.equals(((C
ontact)other).lastName)) result
firstName.compareTo(((Contact)other).firstName)
else result lastName.compareTo(((C
ontact)other).lastName) return result
public String toString() return
lastName ", " firstName
18public class Sorts public static void
insertionSort (Comparable objects) for
(int index 1 index lt objects.length index)
Comparable key objectsindex
int position index // shift
larger values to the right while
(position gt 0 objectsposition-1.compareTo(key
) gt 0) objectsposition
objectsposition-1 position--
objectsposition
key
19class SortPhoneList //-----------------------
------------------------------------------ //
Creates an array of Contact objects and sorts
them //----------------------------------------
------------------------- public static void
main (String args) Contact friends
new Contact4 friends0 new Contact
("John", "Smith", "610-555-7384")
friends1 new Contact ("Daphna", "Weinshall",
"02-555-7777") friends2 new Contact
("Moshe", "Cohen", "03-222-3333")
friends3 new Contact ("Marsha", "Grant",
"243-555-2837") Sorts.insertionSort(friend
s) for (int i0 iltfriends.length i)
System.out.println(friendsi)
20Unit 8
- Interface and polymorphhism
- Inheritance
- Method overriding
- Polymorphism, widening narrowing
- protected and final modifiers
21Inheritance
- Inheritance allows us to derive a new class from
an existing one - The existing class is called the superclass or
base-class - The derived class is called the subclass or
derived-class - Instances of the derived class inherit all the
properties and functionality (data and methods)
defined in the base class - Usually, the derived class adds more
functionality and properties
22Example
Employee (String name, ... hire(double
payRate) pay()layoff() promote() ...
Employee
Hourly() addHours(int moreHours)
Executive() giveBonus(int bonus)
23Properties of subclass
- Everything that can be done with an Employee
object can also be done with an Hourly object - An Hourly is a special kind of Employee it has
all the functionality of an employee and some
more - The subclass instances are more specific than the
instances of the superclass
24Example the Employee class
- public class Employee
- private String name
- private String address
- private double payRate
- public Employee (String name, String address)
-
- this.name name
- this.address address
- payRate 0
-
- public void hire (double payRate)
- this.payRate payRate
-
- public double pay ()
- return(payRate)
public void layoff () payRate 0
25Subclass the extends Keyword
- // define a special kind of employee - LEFI SHAOT
- public class Hourly extends Employee
- private int hoursWorked
- public void addHours (int moreHours)
-
- hoursWorked moreHours
-
- public double pay ()
- return payRatehoursWorked
-
-
-
26More about subclass
- Derived class should normally extend the
functionality of the superclass - In certain cases, a derived class would change
some of the functionality of the superclass
27The hourly subclass
- // define a special kind of employee - LEFI SHAOT
- public class Hourly extends Employee
- private int hoursWorked
-
- public void addHours (int moreHours)
-
- hoursWorked moreHours
-
-
- public void pay ()
- return payRatehoursWorked
-
-
-
extend
change
28Examples
Point
Employee
Object
Switch
29What is Inherited?
- The subclass inherits
- all the fields of the base class
- all the methods of the base class
- The constructors of the subclass should be
defined again - Private fields and methods are inherited but
cannot be accessed directly from the code of the
subclass they can only be accessed indirectly
30Switch Example
Switch
- Switch()
- boolean isOn()
- setOn(boolean)
31Switch Code
- // An electronic switch that can be on/off
- public class Switch
-
- // Records the state of the switch
- private boolean isOn
- // Checks if this switch is on
- public boolean isOn()
- return isOn
-
- // Sets the state of the switch on/off
- public void setOn(boolean state)
- isOn state
-
32Special type Adjustable Switch
33 Inheriting AdjustableSwitch from Switch
- Switch()
- isOn()
- setOn(boolean)
Switch
- AdjustableSwitch()
- setLevel(float)
- getLevel()
AdjustableSwitch
34AdjustableSwitch Example
AdjustableSwitch
35AdjustableSwitch Code
- public class AdjustableSwitch extends Switch
- // The level of current (0-100)
- private float level
-
- // Sets the level of current
- public void setLevel(float level)
- this.level level
-
- // Returns the level of current of the switch
- public float getLevel()
- return level
-
-
36Private Fields - Inherited but not Accessible
- An AdjustableSwitch object has a state variable
isOn inherited from Switch - However, it cannot be accessed directly from the
code of AdjustableSwitch because it is defined as
private in Switch (it is encapsulated)
37Inheritance a Basis for Code Reusability
- Fast implementation - we need not write the
implementation of AdjustableSwitch from scratch,
we just implement the additional functionality - Ease of use - if someone is already familiar with
the base class, then the derived class will be
easy to understand - Less debugging - debugging is restricted to the
additional functionality - Ease of maintenance - if we need to
correct/improve the implementation of Switch,
AdjustableSwitch is automatically corrected as
well - Compactness - our code is more compact and is
easier to understand
38Example Changing the Base Class
- Suppose that we want to add the property of
maximal power to our representation of a switch - This property is suitable in adjustable switches
as well (the inheritance supports our
abstraction!) - Inheritance allows us to add this property only
in the base class the changes will automatically
take place in the inherited class
39Changing the Switch Class
- public class Switch
- // Records the state of the switch
- private boolean isOn
- // The maximal power of this switch
- private float maxPower
- // constructor
- public Switch(float maxPower)
- if (maxPower gt 0.0f)
- this.maxPower maxPower
-
- // Returns the maximal power of this switch
- public float getMaxPower()
- return maxPower
-
40Constructors of subclasses must be redefined?
- The constructor of Switch receives a parameter -
maxPower - and initializes the private field with
the same name - We have to define a constructor for
AdjustableSwitch that receives the same parameter
and initializes this field but this field is
private, so there is no direct access to it - In general, when we invoke a constructor of a
subclass, we first have to construct the
superclass part of the subclass we do this by
using the super keyword
41AdjustableSwitch - Redefinition of Constructor
- // Constructs an adjustable switch
-
- public AdjustableSwitch(float power)
- super(power)
-
-
The first line of the constructor calls the
constructor of the superclass (Switch) that
receives a float parameter to initialize the
state variable defined in the superclass
42Calling super(...)
- The constructor of a derived class MUST
initialize the state of the object from the point
of view of its parent class - the first line in the constructor must be a call
to one of the constructors in the superclass
using super(...) - Otherwise, the compiler automatically places a
call to the empty constructor of the superclass - A later call to super(...) is not allowed because
a constructor can be invoked only once
43Automatic Default Construction
- In the previous implementation of Switch and
AdjustableSwitch we did not include any
constructors - The compiler automatically adds an empty
(default) constructor to each of the classes - In addition, the compiler puts in the first line
of the empty constructor of AdjustableSwitch a
call to the empty constructor of Switch
44Automatic Default Construction
- // ...in class Switch (automatically added)
- public Switch()
-
- // and in class AdjustableSwitch
- //(automatically added)
- public AdjustableSwitch()
- super()
-
-
45Default Construction Using Other Constructors
- Sometimes the programmer may wish to define a
default constructor (i.e., a constructor with no
arguments) by herself, which may call other
constructors with default arguments of her choice - If we do this in the Switch class, we need not
define any constructor in the AdjustableSwitch
class
46Default Construction - Delegation
- // Constructs a new switch.
- // _at_param power the maximal power of the
switch - public Switch(float maxPower)
- if (maxPower gt 0.0f)
- this.maxPower maxPower
-
private static final DEFAULT_POWER
60.0f // Constructs a switch with default
power public Switch()
this(DEFAULT_POWER)
47Unit 8
- Interface and polymorphhism
- Inheritance
- Method overriding
- Polymorphism, widening narrowing
- protected and final modifiers
48Overriding Methods
- Occassionally, when we derive a class, we want to
change some of the functionality defined in the
superclass - Mechanism a sub-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 - The type of the object executing the method
determines which version of the method is invoked
49Overriding example
we want clients to be able to open a restricted
file only if it is unlocked how do we do it?
50File Example
- public class File
-
- // The name of the file
- private String name
-
- // true if the file is opened
- private boolean isOpen
- // Construct a file with a given name
- public File(String name)
- this.name name
-
- // Returns the name of the file
- public String getName()
- return name
-
51File Example
- // Checks if the file is open
- public boolean isOpen()
- return isOpen
-
- // Opens the file
- public void open()
- // other operations
- isOpen true
-
-
- // Closes the file
- public void close()
- // other operations
- isOpen false
-
52RestrictedFile Example
- // Represents a restricted file, which can be
opened only - // if unlocked in order to unlock the file, key
is needed - public class RestrictedFile extends File
-
- // Password for unlocking the file
- private long key
- // The state of the file - locked/unlocked
- private boolean isLocked
// Constructs a new restricted file
public RestrictedFile(String name, long key)
super(name) this.key key
isLocked true
53RestrictedFile added methods
- // Checks if the file is locked
- public boolean isLocked()
- return isLocked
-
-
- // Locks the file
- public void lock()
- isLocked true
-
- // Unlock the file the file will be unlocked
only - // if the given key is valid
- public void unlock(long key)
- if (this.key key)
- isLocked false
-
-
So far the implementation is useless if we do not
change the implementation of open()
54Method overriding
- // Open the file - file will be opened
- // only if it is unlocked
- public void open()
- if (!isLocked())
- super.open()
-
-
55Overriding in RestrictedFile
- RestrictedFile inherits the interface of File,
but changes the functionality of the method
open() - We say that RestrictedFile overrides the method
open() - Note the call to super.open() - we invoke the
method open() of the superclass on this object
56Rules of Inheritance
- When class B is derived from a class A, the
interface of class B will be a superset of that
of class A (except for constructors) - It is not possible to remove a method from the
interface by subclassing - However, class B can override some of the methods
that it inherits and thus change their
functionality
57Rules of Overriding
- When overriding a method, a parent method can be
explicitly invoked using the super reference - If a method is declared with the final modifier,
it cannot be overridden - The concept of overriding can be applied to data
(called shadowing variables), but there is
generally no need for it
58Overloading vs. Overriding
- Overloading deals with multiple methods in the
same class with the same name but different
signatures - Overloading lets you define a similar operation
in different ways for different input data
(different parameters)
- Overriding deals with two methods, one in a
parent class and one in a child class, that have
the same signature - Overriding lets you define a similar operation in
different ways for different object types
59Unit 8
- Interface and polymorphhism
- Inheritance
- Method overriding
- Polymorphism, widening narrowing
- protected and final modifiers
60Class Hierarchies
- A child class of one parent can be the parent of
another child, forming class hierarchies
StaffMember
61Class Hierarchies
- Two children of the same parent are called
siblings - Good class design puts all common features as
high in the hierarchy as is reasonable - An inherited member is continually passed down
the line - Class hierarchies often have to be extended and
modified to keep up with changing needs
62The 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 - The Object class is therefore the ultimate root
of all class hierarchies
63The Object Class
- The Object class contains a few useful methods,
which are inherited by all classes - For example, the equals method of the Object
class determines if two references are aliases - When you define a new class, you can override
equals to define equality in some other way
64The Object Class
- The toString method is defined in the Object
class it returns a string that contains the name
of the objects class and a hash value - Thats 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 - When we define a new class, we can override the
toString() method in order to have a suitable
representation of the new type of objects as
Strings
65Example of Overriding toString()
- // Represents a point on a grid
- public class Point
-
- // The coordinates of the point
- private int x,y
- // Constructs a new point
- public Point(int x, int y)
- this.x x
- this.y y
-
- public String toString()
- return ( x , y )
-
66Example of Overriding toString()
- // Example overriding the toString() method
- class PrintingPointExample
-
- public static void main(String args)
- Point p new Point(2,3)
- System.out.println(p)
- //System.out.println(p.toString())
-
-
67Class Hierarchy example
a RestrictedFile is-a File which is-a Object
Object
68Reference and Inheritance
- We can view a RestrictedFile object from 3
different points of views - As a RestrictedFile this is the most narrow
point of view (the most specific) this point of
view sees the full functionality of the object - As a File this is a wider point of view (a less
specific one) this point of view is ignorant
of the special characteristics the object has as
a RestrictedFile (we can only open and close the
file) - As an plain Object
691 Object reference, many types
- We view an object by using an object reference
- A variable of type reference to File can only
refer to an object which is a File - File f new File(story.txt)
- But a RestrictedFile is also a File, so f can
also refer to a RestrictedFile object - File f new RestrictedFile(visa.dat)
- The type of the reference we use determines the
point of view we will have on the object
70Point of View 1
If we refer to a RestrictedFile object using a
RestrictedFile reference, we have the
RestrictedFile point of view - we see all the
methods that are defined in RestrictedFile and up
the hierarchy
- RestrictedFile f new RestrictedFile(visa.da
t, 12345) - f.close()
- f.lock()
- f.unlock(12345)
- String s f.toString()
71Point of View 2
- If we refer to a RestrictedFile object using a
File reference, we have the File point of view
we see only methods that are defined in class
File and up the hierarchy tree
- File f new RestrictedFile((visa.dat,
12345) - f.close()
- f.lock()
- f.unlock(12345)
- String s f.toString()
72Point of View 3
If we refer to a RestrictedFile object using an
Object reference, we have the Object point of
view - we see only methods that are defined in
class Object
- Object f new RestrictedFile((visa.dat,
12345) - f.close()
- f.lock()
- f.unlock(12345)
- String s f.toString()
73Visualization
RestrictedFile
- toString()
- ...
- isOpen()
- open()
- close()
- lock()
- unlock(key)
- isLocked()
object
file
restricted file
74References and Inheritance
- An object reference can refer to an object of its
class, or to an object of any descendant class
related to it by inheritance - Assigning a child object to an ancestral
reference is considered to be a widening
conversion, and can be performed by simple
assignment - Assigning an object with ancestral reference to a
child reference can also be done, but it is
considered to be a narrowing conversion and must
be done with an explicit cast
75Widening
- Changing our point of view of an object to a
wider one (a less specific one) is called
widening - File file
- file new RestrictedFile((visa, 1234)
RestrictedFile referenceRestrictedFile point of
view
File referenceFile point of view
Widening
76Point class example
- // A point on a grid
- public class Point
-
- // ... The same implementation as before
- // Computes the distance from another point
- // _at_param p the given point
- public double distanceFrom(Point p)
- int dx x-p.x
- int dy y-p.y
- return Math.sqrt(dxdxdydy)
-
- // ... more methods
77Pixel subclass example
- // Represents a pixel on a graphical area
- public class Pixel extends Point
-
- // The color of the pixel
- private Color color
- // Constructs a new pixel
- public Pixel(int x, int y, Color color)
- super(x,y)
- this.color color
-
- // ... more methods
78Widening
- In the following example, the method
distanceFrom() expects a reference to Point and
gets a reference to Pixel we are thus widening
our point of view of the Pixel object - Pixel p1, p2
- p1 new Pixel(2, 3, Color.yellow)
- p2 new Pixel(5, 6, Color.red)
- double d p1.distanceFrom(p2)
79Narrowing
- If we look at all objects from a wide point of
view, and would not be able to restore a more
narrow view for some of them, there is little
point in having multiple points of view - We can restore the narrow point of view for
objects by casting - We can use the instanceof operator to query about
the type of the object we are referencing
80Narrowing Example
- // Locks all the restricted files in the array
- // _at_param files the array of files to be locked
- public static void lockRestrictedFiles(File
files) - for (int i 0 i lt files.length i)
- if (filesi instanceof RestrictedFile)
- RestrictedFile file (RestrictedFile)files
i - file.lock()
-
-
-
81Narrowing - Equivalent Example
// Locks all the protected files in the array
// _at_param files The array of files to be
locked public static void lockRestrictedFiles(Fil
e files) for (int i 0 i lt files.length
i) if (filesi instanceof
RestrictedFile) ((RestrictedFile)filesi)
.lock()
82Rules of Narrowing
- Narrowing lets us restore a more specific point
of view of an object - You cannot refer to an object through a
RestrictedFile reference unless this object is
indeed a RestrictedFile object - If we try to cast a File reference to
RestrictedFile and the former reference refers to
a regular File, we will get the error message
ClassCastException - In the previous example we cast the reference
only after we verified the object is indeed a
RestrictedFile
83Polymorphism via Inheritance
- We saw how an interface can be used to create a
polymorphic reference - Recall that a polymorphic reference is one which
can refer to different types of objects at
different times - Inheritance can also be used as a basis of
polymorphism - An object reference can refer to one object at
one time, then it can be changed to refer to
another object (related by inheritance) at
another time
84Polymorphism via Inheritance example
- Suppose the Employee class has a method called
pay, and the Hourly class overrode it - Now consider the following invocation
- Daphna.pay()
- If Daphna refers to a Employee object, it
invokes the Employee version of pay if it
refers to a Hourly object, it invokes the Hourly
version
85Polymorphism via Inheritance
- It is the type of the object being referenced,
not the reference type, that determines which
method is invoked - Note that, if an invocation is in a loop, the
exact same line of code could execute different
methods at different times - Polymorphic references are therefore resolved at
run-time, not during compilation
86Type of Method is Determined at Runtime (1)
- File file
- if (Math.random() gt 0.5)
- file new File()
-
- else
- file new RestrictedFile(visa.dat, 76543)
- // Recall that a RestrictedFile is locked by
- // default
-
- file.open()
Will the file be opened if the number tossed is
less than 0.5 ??
87Type of Method is Determined at Runtime (2)
public void workaroundAttempt(RestrictedFile
file) File workaroundReference file
workaroundReference.open() //
Since the file is of type RestrictedFile, this
will invoke open() which is defined on restricted
files and not on regular files will the file be
opened?
88Heterogeneous Collections
- Polymorphism is especially useful when we want to
create a heterogeneous collection of objects - Example a class Staff that represents all
workers in a certain organization, some of whom
are employees and some are volunteers - Among the employees some are regular employees
paid monthly, some are temporary and are paid by
the hour, while some are managers (executives)
and get bonuses
89Example StaffMember Class Hierarchy
90StaffMember class
- // StaffMember.java
- class StaffMember
-
- private String name
- private String address
- private String phone
- // constructor
- public StaffMember (String name, String
address, String phone) - this.name name
- this.address address
- this.phone phone
-
- public double pay()
- return 0.0
-
91Volunteer subclass
- // Volunteer.java
- class Volunteer extends StaffMember
-
-
- // constructor
- public Volunteer (String name, String address,
String phone) -
- super (name, address, phone)
-
- // override method
- public double pay ()
-
- return 0.0
-
92Employee subclass
- // Employee.java
- class Employee extends StaffMember
-
- private double payRate
-
- // constructor
- public Employee (String name, String address,
String phone, double payRate) -
- super (name, address, phone)
- this.payRate payRate
-
-
- // override method
- public double pay ()
-
- return payRate
-
93Hourly subclass of Employee
- // Hourly.java
- class Hourly extends Employee
-
- private int hoursWorked
- // constructor
- public Hourly (String name, String address,
String phone, double payRate) -
- super (name, address, phone, payRate)
- hoursWorked 0
-
- // unique method
- public void addHours (int moreHours)
-
- hoursWorked moreHours
-
-
94Hourly subclass of Employee (cont)
- //---------------------------------------------
- - // override method pay of Employee Compute
- // and return the pay for this hourly employee
- //---------------------------------------------
- public double pay ()
-
- double payment payRate hoursWorked
- hoursWorked 0
- return payment
-
95Executive subclass of Employee
- // Executive.java
- class Executive extends Employee
-
- private double bonus
- // constructor
- public Executive (String name, String address,
String phone, double payRate) -
- super (name, address, phone, payRate)
- bonus 0 // bonus has yet to be awarded
-
- // unique method
- public void awardBonus (double execBonus)
-
- bonus execBonus
-
-
96Executive subclass of Employee (cont)
- //---------------------------------------------
-- - // override method pay of Employee Compute
and - // return the pay for an executive, which is
the - // regular employee payment plus a one-time
bonus - //---------------------------------------------
-- - public double pay ()
-
- double payment super.pay() bonus
- bonus 0
- return payment
-
97Heterogeneous collections
- Suppose we would like to store personnel of all
kinds in a single array, so we can easily write
code that takes care of all the workers - For example, suppose we want to implement a
method getTotalCost() in class Staff that will
compute how much money is needed to pay all
personnel at the end of the month
98// Staff.java class Staff StaffMember
staffList // constructor sets up the list
of staff members. public Staff ()
staffList new StaffMember4
staffList0 new Executive (Carla", "123 Main
Line", "555-0469", 1923.07) staffList1
new Employee ("Sam", "456 Off Line", "555-0101",
846.15) staffList2 new Hourly
("Diane", "678 Fifth Ave.", "555-0690", 8.55)
staffList3 new Volunteer ("Norm", "987
Suds Blvd.", "555-8374")
((Executive)staffList0).awardBonus (5000.00)
((Hourly)staffList2).addHours (40)
widening
narrowing
99// compute payday costs public void
getTotalCost () double amount 0.0
for (int count0 count lt staffList.length
count) amount
staffListcount.pay() // polymorphic
100Unit 8
- Interface and polymorphhism
- Inheritance
- Method overriding
- Polymorphism, widening narrowing
- protected and final modifiers
101The protected Modifier
- Any class member (variable, method or
constructor) can be declared with one of the 4
visibility modifiers - private - only code of the same class in which
the member has been defined can access this
member - (none) - default visibility only code of a class
of the same package as that of the class in which
the member has been defined can access it - protected - code in a class that is in the same
package or is a subclass of the class in which
the member was defined - public - code of any class that can access the
class of that member - We declare variables as protected if they will be
used often by someone deriving our class, but we
still want to hide them from a user of our class
102(No Transcript)
103Point Example of a protected Field
- public class Point
-
- // The coordinates of the point
- protected int x,y
- // constructor
- public Point(int x, int y)
- this.x x
- this.y y
-
- // ...
104Protected Fields Example
- public class Pixel extends Point
-
- // The color of the pixel
- private Color color
-
- // constructor
- public Pixel(int x, int y, Color color)
- super(x,y)
- this.color color
-
- // Draws this pixel
- public void draw()
- this method will need to refer to x,y
- and it will be cumbersome to do this
- through access methods
-
105The final modifier
The final modifier can be used for classes,
methods and variables in each case it has a
different meaning
- A final class can not have derived classes
- A final method cannot be overriden
- A final variable can be initialized only once
- If the variable is static you must specify its
value in the declaration and it becomes a
constant - If it is a state variable, you should either
specify a value in the declaration or in the
constructor, but only there and only once
106Heterogeneous Collections in the Java API
- library class java.util.Vector
- A vector is an oredered collection of objects
- You may add or remove objects from the vector at
any position - The size of a Vector grows and shrinks as needed
to accommodate adding and removing items after
the Vector has been created
107Heterogeneous Collections Vector
- class Cat
- public String toString()
- return new String(meaw)
-
-
- class Dog
- public String toString()
- return new String(bark)
-
-
- class Mouse
- public String toString()
- return new String(squeak)
-
108Heterogeneous Collections Vector
- class MouseTrap
- public static void main(String args)
- Vector v new Vector()
- v.addElement(new Cat())
- v.addElement(new Mouse())
- v.addElement(new Dog())
- v.addElement(new Mouse())
- v.addElement(
- new String(its raining cats and
dogs)) - for (int i 0 i lt v.size() i)
- System.out.println(v.elementAt(i))
- catchTheMice(v)
-
109Heterogeneous Collections Vector
- private static catchTheMice(Vector v)
- int i 0
- while (i lt v.size())
- if (v.elementAt(i) instanceof Mouse)
- v.removeElementAt(i)
-
- else
- i
-
-
110Multiple Inheritance Motivation
- Suppose we want to add a new kind of employee a
visitor
Employee
Visitor
Executive
But there can be visitors who are executives as
well, so what do we do in this case?
111Single vs. Multiple Inheritance
- We would like to define a VisitingExecutive class
that extends both Executive and Visitor - Multiple inheritance allows a class to be derived
from two or more classes, inheriting the members
of all parents collisions, such as the same
variable name in two parents, have to be resolved - Java supports single inheritance, meaning that a
derived class can have only one parent class (C
supports multiple inheritance) - In most cases, the use of interfaces gives us the
best aspects of multiple inheritance without the
overhead
112Interface Hierarchies
- Inheritance can be applied to interfaces as well
as classes - One interface can be used as the parent of
another - The child interface inherits all abstract methods
of the parent - A class implementing the child interface must
define all methods from both the parent and child
interfaces - Note that class hierarchies and interface
hierarchies are distinct (the do not overlap)