301lec06mj
301lec06mj
1
Comparison Interfaces
• public class Location {
private int x;
• An interface can specify a contract between
private int y; interacting or associated objects.
…
public boolean equals( Location other ) {
if (this == other)
• For example, a server object’s class could satisfy
return true; an interface (a list of provided methods or
else
return x == other.x && y == other.y; services).
}
… • A client object could use any object for the server
}
role whose class implements this interface.
5 6
Interfaces Interfaces
7 8
2
Interfaces Interfaces
• public interface Sequenced { • public class Counter implements Sequenced {
public void next(); …
public void reset(); public Counter() { … }
} public void next() { … }
// objects of classes that implement this interface public void reset() { … }
// can be “stepped” forward in sequence …
}
// constructor
public SimpleCounter( int initValue ) {
value = initValue;
}
// private method
private void update() {
if (message != null)
message.setText( String.valueOf( value ) );
11 } 12
3
Private Data and Methods Generalization
• // public methods
• Look for conceptual commonalities in the
public void next() {
value++;
abstractions:
update();
}
– common attri but es
– e.g., all vehicles have ?
public void reset() {
value = 0; – common acti ons
update();
} – e.g., all vehicles can ?
13 14
Example
Designing a Class Hierarchy
Designing a Class Hierarchy
15 16
4
Generalization Generalization
17 18
• Inheritance: • Programming:
– “is a” rel ationshi p bet ween abstractions or cl asses – fact or out repetiti ous, reusabl e code
– e.g., a bus “is a” land-based vehicle • Design:
– a deri ved cl ass can ext end t he base cl ass by addi ng – applicati on do mai n modeli ng
ne w dat a and met hods
• Software engineering:
– ma ke syst e ms flexi bl e and ext ensi bl e
19 20
5
Sharing Implementation Sharing Implementation
• public class Number { • public class Cycler {
private int value; private int value;
private TextBox textBox; private int base;
private TextBox textBox;
public Number( int initValue ) { … }
public Number() { … } public Cycler( int base, int initValue ) { … }
public void showIn( TextBox tbox ) { … } public Cycler( int base ) { … }
public void show() { … } public Cycler() { … }
public void next() { … } public void showIn( TextBox tbox ) { … }
public void reset() { … } public void show() { … }
public int value() { … } public void next() { … }
} public void reset() { … }
public int value() { … }
}
21 22
• Co mmonalities: • Di fferences:
– data – data
– value – base
– textBox
– methods – methods
– showIn( … ) – constructors
– show() – next()
– reset()
– value()
23 24
6
Sharing Implementation Inheriting Methods and Data
• public class DisplayableNumber { // version 1
• Cycler “is a” DisplayableNumber private TextBox textBox;
protected int value;
– a Cycl er obj ect can be used wherever a
Di s playabl e Nu mber obj ect can be used public DisplayableNumber( int initValue ) { … }
public void showIn( TextBox tbox ) { … }
– a Cycl er obj ect responds t o t he sa me calls ( messages) public void show() { … }
as a Displ ayabl e Nu mber obj ect public void reset() { … }
public int value() { … }
– DisplayableNumber number = new Cycler();
}
25 26
27 28
7
Inheritance Constructors
29 30
8
Shadowing Data Replacing Inherited Methods
• public class DisplayableNumber {
protected int value; // 2, 3 • A subclasscanredefineaninheritedmethodbyoverridingit.
…
}
overriding it.
9
Extending Inherited Methods Extending Inherited Methods
• public class Cycler extends DisplayableNumber {
• To extend an inherited method, override it, and private int base;
also call it (to do part of the work). …
public void reset() {
// call overridden method;
// does not need to be first statement
super.reset();
value = value % base;
}
}
37 38
• To prevent an inappropriate superclass method • Too much hiding probably means a violation of
from appearing in a subclass, hide it by: the “is a” relationship between the superclass and
– overri di ng t he met hod, or subclass.
– usi ng aggregation, or
– revisi ng t he i nheritance hi erarchy
39 40
10
Hiding Inherited Methods Hiding Inherited Methods
• Suppose: • Suppose:
– cl ass Dog – cl ass Windo w
has bar k(), fet ch() has show(), hi de(), move(), resi ze()
– cl ass Cat inherits from Dog, – cl ass Fi xedSi ze Wi ndo w
but hi des bar k(), fet ch(), inherits from Wi ndo w
and adds purr() but hi des resi ze()
• Cat “is a” Dog? • FixedSizeWindow “is a” Window?
41 42
11
Overriding the Method Using Aggregation
• public class Square extends Rectangle { • public class Square {
… private Rectangle rect;
public void setShape( Shape s ) {
public Square( int side ) {
// should not implement
rect = new Rectangle(
}
new Shape( side, side ) );
… }
}
public void setSide( int newSide ) {
rect.setShape(
new Shape( newSide, newSide ) );
}
…
// Square “has a” Rectangle
}
45 46
12
Dynamic Binding Dynamic Binding
• public class Base {
// default implementation • The object does the right thing, even if the calling
public void op() { … }
}
code does not know its type.
• public class Derived1 extends Base {
…
• Interrelated concepts:
}
// does not override op() inheritance, type casting, method overriding,
• public class Derived2 extends Base { abstract methods.
// override ...
public void op() { … } • Polymorphism.
}
• Base base;
base = new Derived1(); // implicit widening cast
base.op(); // calls op() in Base
base = new Derived2(); // implicit widening cast
base.op(); // calls op() in Derived2 49 50
13
Example:
Abstract Classes
Abstract class
• A class which is explicitly designed to be the superclass abstract class Shape {
53
abstract void draw(); } 54
14
Designing a Class Hierarchy Designing a Class Hierarchy
57 58
59 60
15
Designing a Class Hierarchy Designing a Class Hierarchy
61 62
End
63
16