Lecture%20H%20-%20Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture%20H%20-%20Inheritance

Description:

Inheritance allows us to derive a new class from an existing one ... Instances of the derived class inherit all the properties and functionality that ... – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 67
Provided by: cad84
Category:

less

Transcript and Presenter's Notes

Title: Lecture%20H%20-%20Inheritance


1
Lecture H - Inheritance
  • Unit H1 - Introduction to Inheritance

2
Inheritance
  • Inheritance allows us to derive a new class from
    an existing one
  • The existing class is called the superclass or
    base-class.
  • derived class is called the subclass or
    derived-class.
  • Instances of the derived class inherit all the
    properties and functionality that is defined in
    the base class.
  • Usually, the derived class adds more
    functionality and properties.

3
Example
setTime() getSeconds()getMinutes() getHours() sec
ondElapsed() ...
Clock
AlarmClock
AnalogClock
getSecondsPointerAngle()getMinutesPointerAngle()
getHoursPointerAngle()
setAlarm() setAlarmState()
4
The is a relationship
  • Inheritance creates an is-a relationship.
    AnalogClock is a Clock, AlarmClock is a Clock.
  • Everything that can be done with a Clock object
    can also be done with an AlarmClock object.
  • An AnalogClock is a special kind of Clock. It has
    all the functionality of a clock and some more.
  • The subclass instances are more specific than the
    instances of the superclass.

5
What is inherited and why
  • Properties of the super-class
  • Fields, methods
  • Advantage code reuse
  • white-box reuse has more power than black-box
    reuse
  • Obligations of the super-class
  • Its interface
  • Advantage polymorphism other code can treat
    objects from the subclass as though they belong
    to the super-class

6
Example
moveForwards(float size) turnLeft(float
teta)turnRight(float teta) tailUp() ...
Turtle
drawPolygon(int n, float size) drawSquare(float
size)
IntelligentTurtle
7
IntelligentTurtle
/ A logo turtle that knows how to draw
composite figures such as polygons. / public
class IntelligentTurtle extends Turtle /
Draws a perfect polygon. _at_param n The
number of edges _at_param edgeSize The size of
each edge / public void drawPolygon(int n,
double edgeSize) double teta 360.0 / n
for (int i0 iltn i)
moveForwards(edgeSize) turnLeft(teta)

8
Using IntelligentTurtle
class PerfectPolygon static final double
EDGE_SIZE 100.0 static final int NUM_EDGES
10 public static void main(String args)
IntelligentTurtle t new IntelligentTurtle()
t.tailDown() t.drawPolygon(n,
EDGE_SIZE)
9
When to derive a subclass?
  • Derived class should normally extend the
    functionality of the super-class
  • In certain cases, a derived class would change
    some of the functionality of the super-class
  • Dont use inheritance when black-box use is
    sufficient
  • Thumb Rule Subclass only when it is reasonable
    from the point of view of the abstractions!

10
Examples
Frame
Dialog
FileDialog
11
Lecture H - Inheritance
  • Unit H2 - What is inherited?

12
What is Inherited?
  • When you derive a class from a given base class
  • The subclass inherits all the fields of the base
    class
  • It inherits all the methods of the base class
  • You have to declare the constructors of the
    subclass from scratch
  • Public fields and methods of the super-class can
    be used just like the fields and methods of the
    subclass
  • Private fields and methods are inherited but
    cannot be accessed directly from the code of the
    subclass

13
Switch Example
Switch
  • Switch(boolean)
  • boolean isOn()
  • setOn(boolean)
  • isOn

14
Switch Code
/ An electronic switch that can be on/off.
/ public class Switch // Records the state
of the switch private boolean isOn public
Switch(boolean isOn) this.isOn isOn
public boolean isOn() return isOn
public void setOn(boolean state) isOn
state
15
Switch vs. Adjustable Switch
an adjustable switch has a level of current
dial
pressing the adjustable switch turns it on/off
16
Inheriting AdjustableSwitch from Switch
  • Switch(boolean)
  • isOn()
  • setOn(boolean)

Switch
  • AdjustableSwitch(float)
  • setLevel(float)
  • getLevel()

AdjustableSwitch
17
AdjustableSwitch
public class AdjustableSwitch extends Switch
private float level public
AdjustableSwitch(float level)
super(level ! 0) this.level level
public void setLevel(float level)
this.level level public float
getLevel() return isOn() ? level 0

18
AdjustableSwitch Example
AdjustableSwitch
  • setOn(boolean)
  • isOn()
  • AdjustableSwitch(float)
  • setLevel(float)
  • getLevel()
  • isOn
  • level

19
Private Fields - Inherited but not Accessible
  • Notice that an AdjustableSwitch object has a
    state variable isOn
  • This variable is inherited from Switch
  • However, it cannot be accessed directly from the
    code of AdjustableSwitch because it is defined as
    private in Switch - i.e., it is encapsulated

20
Constructors Must Be Redefined
  • Constructors are not inherited
  • They must be redefined in the subclass
  • The constructor of the sub-class will normally
    need to use the constructor of the super-class
  • This is done using the super keyword

21
Calling super(...)
  • The constructor of a derived class MUST first
    initialize the state of the object from the point
    of view of its parent class.
  • Thus the first line in the constructor must be
    either
  • a call to a constructor of the super-class using
    super(...).
  • A call to another constructor of the subclass
    using this().
  • If you do not call super(...) or this(..) in the
    first line of the constructor, the compiler
    automatically places a call to the empty
    constructor of the super-class.
  • If there is no empty constructor in the
    super-classthe code will not compile!

22
Automatic Default Construction
  • If we do not declare any constructor in a class
    then the compiler automatically adds an empty
    (default) constructor to it.
  • In addition, the compiler puts in the first line
    of the empty constructor a call to the empty
    constructor of the super-class.

23
Automatic Default Construction
// Assume we didnt include any constructors
previously // ...in class Switch (automatically
added) public Switch() //and in class
AdjustableSwitch(automatically added) public
AdjustableSwitch() super()
24
Inheritance 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.

25
Lecture H - Inheritance
  • Unit H3 - Overriding Methods

26
Overriding
  • In certain case, when we derive a class, we want
    to change some of the functionality defined in
    the super-class.
  • Example We want clients to be able to open a
    protected file only if it is unlocked
  • File(String name)
  • isOpen()
  • open()
  • close()
  • getName()

File
  • RestrictedFile(String name, long key)
  • isLocked()
  • lock()
  • unlock(long key)

RestrictedFile
27
File
// Part of a File implementation public class
File private String name private
boolean isOpen public File(String name)
this.name name // do something
public String getName() return name
public boolean isOpen() return isOpen
public void open() isOpen true //
public void close() isOpen false //
// other methods/variables...
28
RestrictedFile
public class RestrictedFile extends File
private long key private boolean isLocked
public RestrictedFile(String name, long key)
super(name) this.key key
isLocked true public boolean isLocked()
return isLocked public void lock()
isLocked true public void unlock(long
key) if (this.key key) isLocked
false // redefine open()!!!
29
RestrictedFile redefining open()
/ Open the file. The file will be opened
only if it is unlocked. / public void open()
if (!isLocked) super.open()
30
Overriding in RestrictedFile
  • RestrictedFile inherits the interface of File,
    but changes the functionality of the method
    open().
  • We say that RestrictedFile overrides the method
    open().
  • Notice the call to super.open() - we invoke the
    method open() of the super-class on this object.

31
Rules of Overriding
  • When you derive a class B from a class A, the
    interface of class B will be a superset of that
    of class A (except for constructors)
  • You cannot remove a method from the interface by
    sub-classing
  • However, class B can override some of the methods
    that it inherits and thus change their
    functionality.
  • The over-ridden methods of the super-class are no
    longer accessible from a variable of the
    sub-class type.
  • They can be invoked from within the sub-class
    definition using the super.method() syntax.
  • The contract of a method states what is expected
    from an overriding implementation of the method.

32
Lecture H - Inheritance
  • Unit H4 - The Object class

33
The Object Class
  • Java defines the class java.lang.Object that is
    defined as a superclass for all classes.
  • If a class doesnt specify explicitly which class
    it is derived from, then it will be implicitly
    derived from class Object.
  • So, in the previous example, RestrictedFile was
    derived from File which in turn was derived from
    Object.
  • We can depict the relationship between this
    classes in the following diagram, that is called
    class hierarchy diagram.

34
Hierarchy Diagram
Object
File
RestrictedFile
35
The Object Class
  • The Object class defines a set of methods that
    are inherited by all classes.
  • One of these is the toString() method that is
    used whenever we want to get a String
    representation of an object.
  • When you define a new class, you can override the
    toString() method in order to have a suitable
    representation of the new type of objects as
    Strings.
  • The contract of the toString() method says that
    you are expected to return a String that
    represents your object

36
Point
/ Represents a point on a grid. / public
class Point // The coordinates of the
point private int x,y / Constructs a
new point. _at_param x,y The coordinates of the
point. / public Point(int x, int y)
this.x x this.y y public String
toString() return ( x , y )

37
Point Example of Overriding toString()
// Example of 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())
  • The output of the program will be

(2,3)
38
Other methods defined in Object
  • boolean equals(Object other)
  • Tests whether this object is equal to the other
    object
  • Object clone()
  • Creates and returns a clone (copy) of this object
  • void finalize()
  • Called by the garbage collector before the object
    is collected
  • Various synchronization primitives
  • A few more

39
Lecture H - Inheritance
  • Unit H5 - Widening and Narrowing

40
Sub-classes as sub-types
  • Recall the is-a relationship induced by
    inheritance A RestrictedFile is a File which is
    an Object.
  • 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). We forget about the special
    characteristics the object has as a
    RestrictedFile (we can only open and close the
    file).
  • As a plain Object.

41
Variables can reference subclass values
  • We view an object by using an object reference.
  • A variable of type reference to File can only
    refer to any object which is a File.
  • But a RestrictedFile is also a File, so f can
    also refer to a RestrictedFile object.
  • The type of the reference we use determines the
    point of view we will have on the object.

File f new RestrictedFile(story.txt)
File f new RestrictedFile(visa.dat)
42
RestrictedFile point of view
  • 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 tree.

RestrictedFile f new RestrictedFile(vis
a.dat, 12345) f.close() f.lock() f.unlock(1234
5) String s f.toString()
43
File point of view
  • If we refer to a RestrictedFile object using a
    File reference we have the File point of view -
    which lets us use only methods that are defined
    in class File and up the hierarchy tree.

File f new RestricredFile(visa.dat,
12345) f.close() f.lock() f.unlock(12345) Stri
ng s f.toString()
44
Object point of view
  • If we refer to a RestrictedFile object using an
    Object reference we have the Object point of view
    - which let us see only methods that are defined
    in class Object.

Object f new RestrictedFile(visa.dat,
12345) f.close() f.lock() f.unlock(12345) Stri
ng s f.toString()
45
Points of View
RestrictedFile
  • toString()
  • ...
  • isOpen()
  • open()
  • close()
  • lock()
  • unlock(key)
  • isLocked()
  • ...
  • isOpen
  • isLocked
  • key

46
Compile time-type vs. run-time type
  • A variable of a reference type has a declared
    type that is known in compile time and never
    changes.
  • File f
  • A reference variable may hold values of any
    subclass of its declared type
  • The type of the values held may change during the
    running of the algorithm and is not known during
    compile time
  • The run-time type is always some subclass of the
    compile-time type.

f new RestrictedFile(visa.dat,12345)
f new File(visa.dat)
47
Widening
  • 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.dat, 1234)

RestrictedFile referenceRestrictedFile point of
view
File referenceFile point of view
Widening
48
Point -- distanceFrom
/ 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
49
Pixel
/ Represents a pixel on a graphical area.
/ public class Pixel extends Point //
The color of the pixel private Color color
/ Constructs a new pixel.
_at_param x,y The coordinates of the pixel.
_at_param color The color of the pixel. /
public Pixel(int x, int y, Color color)
super(x,y) this.color color
// ... more methods
50
Widening parameters
  • 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.

Point p1 Pixel p2 p1 new Point(2, 3) p2
new Pixel(5, 6, Color.red) double d
p1.distanceFrom(p2)
51
Compile-time vs. run-time method invokation
  • When we invoke a method on an object we always do
    it through a reference
  • The implementation of the method which is most
    specific will be chosen.
  • Java methods are virtual, i.e. the method which
    will be invoked is determined by the run-time
    type of object and not on the compile-time type
    of reference.
  • The identity of the invoked method is determined
    at runtime.
  • There are languages that use different mechanisms
    for method invocation.

52
Type of Method is Determined at Runtime
File file if (Math.random() gt 0.5) file
new File(visa.dat) else file new
RestrictedFile(visa.dat, 76543)
file.open()
Will the file be opened if the number tossed is
less than 0.5 ?? - NO!
53
Virtual methods
public class X public int getVal() return
1 public class Y extends X public int
getVal() return 2 public class Test
public static void main(String args) X
x x new Y() System.out.println(x.getVa
l()) // Will print 2!!
54
Narrowing
  • Widening super-class variable ? subclass
    expression
  • Narrowing subclass-variable ? super-class
    expression
  • Narrowing only works if the run-time type of the
    super-class expression is indeed from the
    subclass
  • In Java, narrowing requires explicit casting

Point p1 Pixel p2 p2 new Pixel(2, 3,
Color.red) p1 p2 // widening p2
(Pixel) p1 // narrowing
55
InstanceOf
  • If the run-time type of the narrowed expression
    is not of the subclass that the value is assigned
    to, an exception is thrown.
  • It is possible to check what the run-time type is
    using the instanceOf operator

File file if (Math.random() gt 0.5) file
new File(visa.dat) else file new
RestrictedFile(visa.dat, 76543) if (file
instanceOf restricetdFile) r
(RestrictedFile) file r.unlock(76543) file.
open()
56
Lecture H - Inheritance
  • Unit H6 - Visibility rules

57
Packages
  • Java lets us group our classes into packages
  • When we define classes we often define them as
    part of a package of related classes
  • To group several classes into a package we must
  • Specify in each of the source files of the class
    that the class belongs to the package
  • Put all class files under a directory with the
    name of the package. The directory that contains
    this directory should be in our CLASSPATH.
  • Only classes that are declared public can be used
    from other packages
  • To use a class from a different package we must
    either
  • Import the other package
  • Use the full name of the class package.class

58
Example paint-brush program organization
(c)
my_classes
figures Figure.class Rectangle.class Ellipse.class
Polygon.class
paintbrush PaintBrushCanvas.class PaintBrushPictur
e.class PaintBrush.class
59
PaintBrushPicture.java
package paintbrush import java.awt. public
class PaintBrushPicture() //
PaintBrushCanvas x new PaintBrushCanvas()
figures.Rectangle r new figures.Rectangle()
Button b new Button() // java.awt.Button()
60
Package hierarchies
  • Packages may have hierarchical names
  • java.security.acl
  • Packages for public use should have names
    according to a standard naming scheme reverse of
    the URL of the company
  • org.omg.CORBA.portable (the organizations URL
    is www.omg.org)
  • The directory structure should mirror the name
    hierarchy
  • ltClasspathgt/org/omg/CORBA/portable/ltClassName.clas
    sgt

61
Visibility Modifiers for members
  • Any class member (variable, method or
    constructor) can be declared with one of the 4
    visibility modifiers
  • private accessible only in the same class in
    which the member is defined
  • (none) - default visibility accessible in any
    class in the same package as the class in which
    the member is defined
  • protected -accessible in any subclass of the
    class in which the member was defined or in the
    same package as the class in which the member is
    defined
  • public accessible in any class that can access
    the class of that member

62
The protected Modifier
  • 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
  • Avoid defining too many protected variables, we
    usually want our object to be as encapsulated as
    possible
  • A protected member is included in the API of our
    class. You should provide documentation comments
    to it
  • protected methods are usually useful for
    customizing the behavior of our class by
    sub-classing

63
Point
/ Represents a point on a grid. / public
class Point / The coordinates of the
point. / protected int x,y /
Constructs a new point. _at_param x,y The
coordinates of the point. / public
Point(int x, int y) this.x x this.y
y // ...
64
Pixel
public class Pixel extends Point // The color
of the pixel private Color color public
Pixel(int x, int y, Color color)
super(x,y) this.color color public
String toString() return color.toString ()
super.toString() public void
draw() //this method will need to refer to
x,y //and it will be cumbersome to do this
//through access methods
65
The 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.

66
Lecture H - Inheritance
Write a Comment
User Comments (0)
About PowerShow.com