CS102 – OOP_2
description
Transcript of CS102 – OOP_2
![Page 1: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/1.jpg)
CS102 – OOP_2
Inheritance, Polymorphism, Interfaces & Abstract classes.
David Davenport
![Page 2: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/2.jpg)
Abstract Classes & Methods
Motivation Do not want to include instances of
common (base) class in polymorphic collection
Want guarantee all objects in a polymorphic collection include certain methods
![Page 3: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/3.jpg)
Get into Shapes… A collection of Rectangles & Circles?
CircleRectangle
TwoDShapeEnsure instances of TwoDShape cannot be added to collection
{Rectangle} {Circle} {Rectangle}
picture
{ ?? } { TwoDShape[] }
![Page 4: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/4.jpg)
University People… Abstract vs. Concrete classes
Person
Student
GradUnderGrad
Staff
Academic Non-Academic
Secretarial Security Cleaning
![Page 5: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/5.jpg)
Get into Shapes… Compute area of collection of shapes
Circle+getArea()
Rectangle +getArea()
Triangle
RightAngled Isosceles Equilateral
Polygon
TwoDShapeNeed guarantee that all future classes implement getArea()
Ensure instances of TwoDShape cannot be added to collection
+getArea()
![Page 6: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/6.jpg)
Abstract Classes (& methods)
Abstract classes provide common parent which
cannot be instantiated a guarantee sub-classes already have
or must implement certain methods Create with keyword “abstract”
e.g. public abstract class Media {…}(sub-class extends abstract class as normal)
Can include Properties (& constructors!) Implemented methods Abstract methods
(create with keyword “abstract” & no body)e.g. public abstract double getDuration();
![Page 7: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/7.jpg)
The MusicCD Collection
Object
MusicCD
DiscountCD
CDCollection
toString(), clone(), …
title, artist, price, tracks getTitle(),getArtist(),getPrice(),getDuration()
DiscountsetDiscount(-)getPrice()
Set of MusicCDgetValue()getTotalDuration()
![Page 8: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/8.jpg)
The Media Hierarchy
Object
MusicCD
DiscountCD
Video
Media Library
DVD
price, title, getPrice(), getTitle(){abst} getDuration()
toString(), clone(), …
artist, tracksgetDuration()
discountsetDiscount(-)getPrice()
lengthdirectorgetDuration()
Set of MediagetValue()getTotalDuration()
![Page 9: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/9.jpg)
The Media Classpublic abstract class Media {
String title; double price; public Media( String title, double price) { this.title = title; this.price = price; }
public String getTitle() { return title; }
public double getPrice() {return price; }
public void setPrice( double newPrice) { price = newPrice; }
public abstract int getDuration(); public String toString() { return title + "\t" + getPrice() + "\n"; }}
Declare as abstract- cannot be instantiated
Can include constants, properties constructors!, implemented & abstract methods
abstract method – look, no body!
Class must be abstract if it includes abstract methods
![Page 10: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/10.jpg)
The Video Class// Video// David, 23/3/02
public class Video extends Media {
int length;String director;
public Video( String title, String director, int length, int price) {super( title, price);this.length = length;this.director = director;
}
public int getDuration() { return length; }
public String getDirector() { return director; }
public String toString() {return "VIDEO " + super.toString();
}}
Calls parent constructor
Sub-class simply extends “abstract” parent as normal
Implements required
“abstract” method
Overrides & reuses parent method
![Page 11: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/11.jpg)
Interfaces
Motivation Provides a form of multiple-inheritance Guarantee specified methods exist Powerful design approach!
![Page 12: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/12.jpg)
Interfaces An interface
is the boundary between two systemsthrough which they connect/communicate
Often standardised
SystemA
SystemB
Advantage:can change either side without affecting the other!
interface
![Page 13: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/13.jpg)
Java Interfaces Declare with public interface X {…}
restricted to constants & abstract methods only cannot be instantiated guarantees any implementing class
has specified methods (else cannot be instantiated)
Classes extend one class & implement interfacese.g. public class U extends V implements X,Y,Z {…}
Can view as a special form of classso class U is_a V, is_a X, is_a Y, is_a Zi.e. a form of multiple inheritance
![Page 14: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/14.jpg)
Simple example…
public interface Pointable {public boolean contains( int x, int y);
}
public class Circle extends TwoDShape implements Pointable {
int radius;
public Circle( int radius) {super();this.radius = radius;
}
public int getRadius() {return radius;
}
public boolean contains( int x, int y) {// set result true iff x,y inside circle...return result;
}}
Define interface with abstract method
Define classthat implements interface
Required method...won’t compile if omitted
![Page 15: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/15.jpg)
Another example… Multiple inheritance?
Notebook is_a Computeris_a ElectricalDeviceis_a NetworkedDevice
ElectricCookeris_a Cookeris_a ElectricalDevice
Microwaveis_a Cookeris_a ElectricalDevice
SmartPhoneis_a Phoneis_a Computeris_a NetworkedDevice
Brain is_a Computeris_a BiologicalDevice
GasCookeris_a Cookeris_a GasDevice
![Page 16: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/16.jpg)
One soln with interfaces
NotebookBrain
Computer ElectricalDevice
Microwave ElectricCooker GasCooker
Cooker
![Page 17: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/17.jpg)
Another soln with interfaces
NotebookBrain
Computer ElectricalDevice
Microwave ElectricCooker GasCooker
Cooker
![Page 18: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/18.jpg)
{V}
a
{V}
Interfaces… as a form of multiple-inheritance
{U}
{V}
{Y} {Z}
{X}
b
{X}
V a;a = new V();a = new U();
X b;b = new X();b = new U();
b = a;b = (U) a;
Y c;c = new U();
c = b;
public class U extends V implements X, Y, Z
c
{Y}
{U}
![Page 19: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/19.jpg)
Categorised Library…
Object
MusicCD
DiscountCD
Video
Media Libraryprice, title, getPrice(){abst} getDuration()set & getCategory()
toString(), clone(), …
artist, tracksgetDuration()
discountsetDiscount(-)getPrice()
lengthdirectorgetDuration()
Set of MediagetValue()getTotalDuration()listInCategory(cat)
Simple: just add set/get category to base class…
![Page 20: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/20.jpg)
The Categories Interface// Categories// David, 23/3/02
public interface Categories {
public final String COMEDY = "COMEDY";public final String ADVENTURE = "ADVENTURE";public final String CRIME = "CRIME";public final String NONFICTION = "NONFICTION";public final String ROCK = "ROCK";public final String POP = "POP";public final String CLASSICAL = "CLASSICAL";
public String getCategory();
public void setCategory( String c);}
Declare interface (instead of class)
Can only include
constants & abstract methods
Methods abstract by
default!
![Page 21: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/21.jpg)
Media with Interface (1)
Object
MusicCD
DiscountCD
Video
Media Libraryprice, title, getPrice(){abst} getDuration()set & getCategory()
toString(), clone(), …
artist, tracksgetDuration()
discountsetDiscount(-)getPrice()
lengthdirectorgetDuration()
Set of MediagetValue()getTotalDuration()listInCategory(cat)
Categories
![Page 22: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/22.jpg)
The Media Classpublic abstract class Media implements Categories {
// include all // properties, constructors & methods // as before..
// *********************************// & add properties & methods // to implement Categories interface
String category;
public String getCategory() {return category;
}
public void setCategory( String c) {category = c;
}// *********************************
}
Class implements one or more interfaces
by adding any necessary code
![Page 23: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/23.jpg)
Media with Interface (2)
Object
MusicCD
DiscountCD
Video
Media Libraryprice, title, getPrice(){abst} getDuration()
toString(), clone(), …
artist, tracksgetDuration()setCategory() getCategory()
discountsetDiscount(-)getPrice()
lengthdirectorgetDuration()
Set of ?Categories?getValue()getTotalDuration()listInCategory(cat)
Categories
Categories
CatVideo setCategory() getCategory()
![Page 24: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/24.jpg)
Media with Interface (3) Moral: Design interfaces first!
IMedia Media
ICategories
Library- initially: set of IMedia- later: set of ICategories
(a) (b)
![Page 25: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/25.jpg)
Extending Interfaces (a)public interface IMedia {
public int getPrice();public int getDuration();
}
public interface ICategories extends IMedia {public String getCategory();public void setCategory( String c);
}
public class Library {ArrayList<IMedia>() items;public int getValue () {…}public int getTotalDuration() {…}
}
public class Library {ArrayList<ICategories>() items;public int getValue () {…}public int getTotalDuration() {…}public void listInCategory( cat) {…}
}
public abstract class Media implements IMedia {
public int getPrice() {…}}
public class Video extends Media {public int getDuration() {…}
}
public class CatVideo extends Videoimplements ICategories {
public String getCategory() {…}public void setCategory(
String c) {…}
}
![Page 26: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/26.jpg)
Media with Interface (3) Moral: Design interfaces first!
ICategories
IMedia Media
Library- initially: set of IMedia- later: set of ICategories
IMedia ICategories
ICatMedia
Library- initially: set of IMedia- later: set of ICatMedia
(a) (b)
![Page 27: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/27.jpg)
Extending Interfaces (b)public interface IMedia {
public int getPrice();public int getDuration();
}
public interface ICategories {public String getCategory();public void setCategory( String c);
}
public class Library {ArrayList<IMedia>() items;public int getValue () {…}public int getTotalDuration() {…}
}
public class Library {ArrayList<ICatMedia>() items;public int getValue () {…}public int getTotalDuration() {…}public void listInCategory( cat) {…}
}
public abstract class Media implements IMedia {
public int getPrice() {…}}
public class Video extends Media {public int getDuration() {…}
}
public class CatVideo extends Videoimplements ICatMedia {
public String getCategory() {…}public void setCategory(
String c) {…}
}
public interface ICatMedia extends IMedia, ICategories {
}
![Page 28: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/28.jpg)
Interface notes Can’t create objects of interface type, only a
type that implements it Interfaces can extend interfaces
(but not classes) to form a hierarchy separate from class one
Start design with interfaces!
Java provides “instanceof” operator to test object type (but use very sparingly)
See also “getClass()”
![Page 29: CS102 – OOP_2](https://reader036.fdocuments.in/reader036/viewer/2022081603/56815010550346895dbdf416/html5/thumbnails/29.jpg)
Other notes… Method access modifiers
protected vs. default (nothing specified)
“default” is package access only
“protected” is package, plus can be sub-classed from another package!