lecture h - inheritance
DESCRIPTION
Lecture H - Inheritance. Unit H1 - Introduction to Inheritance. 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 . - PowerPoint PPT PresentationTRANSCRIPT
Slide 1 of 66.Lecture H
Lecture H - InheritanceUnit H1 - Introduction to Inheritance
Slide 2 of 66.Lecture H
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.
Slide 3 of 66.Lecture H
Example
Clock
AlarmClock AnalogClock
setTime()getSeconds()getMinutes()getHours()secondElapsed()...
getSecondsPointerAngle()getMinutesPointerAngle()getHoursPointerAngle()
setAlarm()setAlarmState()
Slide 4 of 66.Lecture H
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.
Slide 5 of 66.Lecture H
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
Slide 6 of 66.Lecture H
Example
Turtle
IntelligentTurtle
moveForwards(float size)turnLeft(float teta)turnRight(float teta)tailUp()...
drawPolygon(int n, float size)drawSquare(float size)
Slide 7 of 66.Lecture H
IntelligentTurtle/** * A logo turtle that knows how to draw composite * figures such as polygons. */public class IntelligentTurtle extends Turtle {
/** * Draws a perfect polygon. * @param n The number of edges * @param edgeSize The size of each edge */ public void drawPolygon(int n, double edgeSize) { double teta = 360.0 / n; for (int i=0; i<n; i++) { moveForwards(edgeSize); turnLeft(teta); } }}
Slide 8 of 66.Lecture H
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); }}
Slide 9 of 66.Lecture H
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
• Don’t use inheritance when black-box use is sufficient• Thumb Rule: Subclass only when it is reasonable from
the point of view of the abstractions!
Slide 10 of 66.Lecture H
Examples
Point
Pixel
Employee
Manager
Frame
Dialog
FileDialog
TextField
PasswordField
Slide 11 of 66.Lecture H
Lecture H - InheritanceUnit H2 - What is inherited?
Slide 12 of 66.Lecture H
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
Slide 13 of 66.Lecture H
Switch
isOn
Switch(boolean) boolean isOn() setOn(boolean)
Switch Example
Slide 14 of 66.Lecture H
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; }}
Slide 15 of 66.Lecture H
Switch vs. Adjustable Switch
on off
pressing the adjustable switchturns it on/off
an adjustable switch has a “level of current” dial
Slide 16 of 66.Lecture H
Inheriting AdjustableSwitch from Switch
Switch
AdjustableSwitch
Switch(boolean) isOn() setOn(boolean)
AdjustableSwitch(float) setLevel(float) getLevel()
Slide 17 of 66.Lecture H
AdjustableSwitchpublic 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; }}
Slide 18 of 66.Lecture H
AdjustableSwitch
level
setOn(boolean) isOn() AdjustableSwitch(float) setLevel(float) getLevel()
AdjustableSwitch Example
isOn
Slide 19 of 66.Lecture H
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
Slide 20 of 66.Lecture H
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
Slide 21 of 66.Lecture H
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!
Slide 22 of 66.Lecture H
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.
Slide 23 of 66.Lecture H
Automatic Default Construction// Assume we didn’t include any constructors previously…
// ...in class Switch (automatically added)
public Switch() {
}
//and in class AdjustableSwitch(automatically added)
public AdjustableSwitch() { super();}
Slide 24 of 66.Lecture H
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.
Slide 25 of 66.Lecture H
Lecture H - InheritanceUnit H3 - Overriding Methods
Slide 26 of 66.Lecture H
File
RestrictedFile
File(String name) isOpen() open() close() getName()
RestrictedFile(String name, long key) isLocked() lock() unlock(long key)
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
Slide 27 of 66.Lecture H
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...}
Slide 28 of 66.Lecture H
RestrictedFilepublic 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()!!!}
Slide 29 of 66.Lecture H
RestrictedFile – redefining open()
/** * Open the file. The file will be opened only if it * is unlocked. */public void open() { if (!isLocked) { super.open(); }}
Slide 30 of 66.Lecture H
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.
Slide 31 of 66.Lecture H
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.
Slide 32 of 66.Lecture H
Lecture H - InheritanceUnit H4 - The Object class
Slide 33 of 66.Lecture H
The Object Class
• Java defines the class java.lang.Object that is defined as a superclass for all classes.
• If a class doesn’t 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.
Slide 34 of 66.Lecture H
File
RestrictedFile
Object
Hierarchy Diagram
Slide 35 of 66.Lecture H
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
Slide 36 of 66.Lecture H
Point /** * Represents a point on a grid. */public class Point { // The coordinates of the point private int x,y;
/** * Constructs a new point. * @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 + ”)”; }}
Slide 37 of 66.Lecture H
Point: Example of Overriding toString()
The output of the program will be:
// 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()); }}
(2,3)
Slide 38 of 66.Lecture H
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
Slide 39 of 66.Lecture H
Lecture H - InheritanceUnit H5 - Widening and Narrowing
Slide 40 of 66.Lecture H
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.
Slide 41 of 66.Lecture H
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(“visa.dat”);
File f = new RestrictedFile(“story.txt”);
Slide 42 of 66.Lecture H
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(“visa.dat”, 12345);f.close();f.lock();f.unlock(12345);String s = f.toString();
Slide 43 of 66.Lecture H
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);String s = f.toString();
Slide 44 of 66.Lecture H
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);String s = f.toString();
Slide 45 of 66.Lecture H
Points of View
RestrictedFile
... isOpen isLocked key
toString() ... isOpen() open() close() lock() unlock(key) isLocked()
Slide 46 of 66.Lecture H
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”);
Slide 47 of 66.Lecture H
RestrictedFile referenceRestrictedFile point of view
File referenceFile point of view
Widening
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);
Slide 48 of 66.Lecture H
Point -- distanceFrom/** * A point on a grid. */public class Point { // ... The same implementation as before
/** * Computes the distance from another point * @param p The given point. */ public double distanceFrom(Point p) { int dx = x-p.x; int dy = y-p.y; return Math.sqrt(dx*dx+dy*dy); }
// ... more methods}
Slide 49 of 66.Lecture H
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. * @param x,y The coordinates of the pixel. * @param color The color of the pixel. */ public Pixel(int x, int y, Color color) { super(x,y); this.color = color; } // ... more methods}
Slide 50 of 66.Lecture H
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);
Slide 51 of 66.Lecture H
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.
Slide 52 of 66.Lecture H
Type of Method is Determined at Runtime
Will the file be opened if the number tossed is less than 0.5 ?? - NO!
File file;
if (Math.random() >= 0.5) file = new File(“visa.dat”);else file = new RestrictedFile(“visa.dat”, 76543); file.open();
Slide 53 of 66.Lecture H
Virtual methodspublic 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.getVal()); // Will print 2!! }}
Slide 54 of 66.Lecture H
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 ; // wideningp2 = (Pixel) p1 ; // narrowing
Slide 55 of 66.Lecture H
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() >= 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();
Slide 56 of 66.Lecture H
Lecture H - InheritanceUnit H6 - Visibility rules
Slide 57 of 66.Lecture H
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
Slide 58 of 66.Lecture H
Example: paint-brush program organization
figuresFigure.classRectangle.classEllipse.classPolygon.class
paintbrushPaintBrushCanvas.classPaintBrushPicture.classPaintBrush.class
my_classes
(c:)
Slide 59 of 66.Lecture H
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()
Slide 60 of 66.Lecture H
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 organization’s URL is www.omg
.org)• The directory structure should mirror the name hierarchy
<Classpath>/org/omg/CORBA/portable/<ClassName.class>
Slide 61 of 66.Lecture H
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
Slide 62 of 66.Lecture H
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
Slide 63 of 66.Lecture H
Point/** * Represents a point on a grid. */public class Point { /** * The coordinates of the point. */ protected int x,y;
/** * Constructs a new point. * @param x,y The coordinates of the point. */ public Point(int x, int y) { this.x = x; this.y = y; } // ...}
Slide 64 of 66.Lecture H
Pixelpublic 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 }}
Slide 65 of 66.Lecture H
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.
Slide 66 of 66.Lecture H
Lecture H - Inheritance