Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS2200 Software Development Polymorphism II A. O’Riordan, 2008 K. Brown, 2004-2007.

Similar presentations


Presentation on theme: "1 CS2200 Software Development Polymorphism II A. O’Riordan, 2008 K. Brown, 2004-2007."— Presentation transcript:

1 1 CS2200 Software Development Polymorphism II A. O’Riordan, 2008 K. Brown, 2004-2007

2 2 Example: Camera Store Write a program to manage the inventory of a camera shop ●all inventory elements contain a description of the item, a catalogue number, the quantity in stock, and the price ●lenses have a focal length, and they may zoom ●films have a recommended storage temperature, a speed, and a number of exposures ●cameras have a maximum shutter speed, a body colour, and may come complete with a lens

3 Class Design: version 1 ●Represent each type of inventory element as a class ●each inventory element should be able to display details, and should allow the user to change the stock levels, change the price, etc -description: String -catalogueNumber: int -stockLevel: int -price: double -focalLength: double -zoom: boolean +Lens(...) +display() +changePrice() +changeStock() -description: String -catalogueNumber: int -stockLevel: int -price: double -temp: int -exposures: int +Film(...) +display() +changePrice() +changeStock() -description: String -catalogueNumber: int -stockLevel: int -price: double -hasLens: boolean -maxShutterSpeed: int -bodyColour: String +Camera(...) +display() +changePrice() +changeStock() Lens Film Camera

4 4 Class Design: version 2 ●Each inventory element has some details in common, but also some different properties... ●define a superclass for all elements ●define subclasses for each new type ●inherit common data and methods ●define new data and methods in subclasses ●Issue: Should we have a “type” variable in the superclass? ●Represent the inventory as an array of the superclass objects ●Why not three separate arrays? ●What happens if we want to add a new type of stock?

5 5 UML Class diagram -focalLength: double -zoom: boolean +Lens(...) +display() -temp: int -exposures: int +Film(...) +display() -hasLens: boolean -maxShutterSpeed: int -bodyColour: String +Camera(...) +display() Lens Film Camera #description: String #catalogueNumber: int #stockLevel: int #price: double +InventoryItem(...) +changePrice() +changeStock() InventoryItem +: public #: protected -: private +: public #: protected -: private Accessible? public: anywhere protected: in same class or subclass private: in same class Accessible? public: anywhere protected: in same class or subclass private: in same class

6 Outline class definitions: InventoryItem variables will be inherited by subclasses public class InventoryItem { protected String description; protected int catalogueNumber; protected int stockLevel; protected double price; public InventoryItem(String desc, int cat, int stock, double pr) { description = desc; catalogueNumber = cat; if (stock > 0) stockLevel = stock; else stockLevel = 0; if (pr > 0) price = pr; else price = 0.0; } assume proper comments and other methods assume proper comments and other methods

7 public class Lens extends InventoryItem { private double focalLength; private boolean zoom; public Lens(String desc, int cat, int st, double p, double length, boolean hasZoom) { super(desc, cat, st, p); if (length > 0) focalLength = length; else focalLength = 0.0; zoom = hasZoom; } public String display() { return "# " + catalogueNumber + "\n" + description + "\nin stock: " + stockLevel + "\nfocal length = " + focalLength; } able to access these, since " protected " subclass constructor first statement must be call to superclass constructor

8 8 Try Displaying the Inventory InventoryItem[] inventory = new InventoryItem[3]; //filling the array manually inventory[0] = new Lens(...); inventory[1] = new Film(...); inventory[2] = new Lens(...); for (int i = 0; i < inventory.length; i++) System.out.println(inventory[i].display()); //error Does not compile! Why? Hint: Where is display method defined and what reference is used to call it.

9 9 Fixing Problem ●Also doesn't compile: InventoryItem item = new Lens(...); System.out.println(item.display()); ●Compiler sees that the variable item is of type InventoryItem. InventoryItem has no method display(). Therefore it complains, and does not compile. ●Object in question is actually of type Lens, and so it does have the appropriate method ●Solution: give the InventoryItem class a display() method (even though we know it will not be called)

10 10 New class diagram new method, which will be overridden public class InventoryItem { protected String description;... public void display() { } } description: String catalogueNumber: int stockLevel: int price: double //... +display() InventoryItem -focalLength: double -zoom: boolean +display() -temp: int -exposures: int +display() Lens Film...

11 11 Polymorphic display InventoryItem[] inventory = new InventoryItem[3]; //filling the array manually inventory[0] = new Lens(...); inventory[1] = new Film(...); inventory[2] = new Camera(...); for (int i = 0; i<inventory.length; i++) System.out.println(inventory[i].display()); Display method is now polymorphic - it has a different form in each subclass. JVM will find the appropriate method at run-time, i.e. it does the work for you

12 12 Abstract classes ●When writing the inventory example, we never intended to create an object directly of class InventoryItem - we would only create objects of the classes Lens, Film or Camera. ●Therefore InventoryItem should be an abstract class ●If we declare a class to be abstract, Java will not allow any program to create an object of that class directly - we will be forced to define a non-abstract subclass, and create objects in that. ●Non-abstract classes are called concrete classes. ●We can still create references to objects of an abstract class.

13 13 Example abstract class definition public abstract class InventoryItem { public String description; //... //display() - will be overriden public String display() { return "dummy"; } } Declares InventoryItem to be abstract - we are now forbidden from creating an object of this class directly.

14 14 Example: trying to instantiate an abstract class public class AbstractTest { public static void main(String args[]) { InventoryItem test = new InventoryItem("",1,1,1.0); } Trying to create an object from an abstract class -Called Compiler C:\PROGRA~1\Java\JDK15~1.0_0\bin\javac.exe- -Target File: \AbstractTest.java- \AbstractTest.java:18: InventoryItem is abstract; cannot be instantiated InventoryItem test = new InventoryItem("", 1, 1, 1.0); ^ 1 error -Finished-

15 15 Abstract methods ●We can also declare a method to be abstract. An abstract method has no method body. ●If a method is abstract, it means that any concrete subclass must provide a definition of the method. ●If we declare any method to be abstract, Java forces us to declare the class to be abstract. ●This ensures we can't forget to provide a method definition for a concrete class

16 16 Example: not defining an abstract method public class AbstractMethodTest extends InventoryItem { int dummy; } D:\MyData\Java\ADDRES~2\AbstractMethodTest.java:2: AbstractMethodTest should be declared abstract; it does not define display() in InventoryItem 1 error Output window: No definition for display() public abstract class InventoryItem { public abstract void display(); } Abstract method, no method body

17 17 Example: abstract method in concrete class public class InventoryItem { public String description; //display() - will be overriden public abstract void display(); } D:\MyData\Java\ADDRES~2\InventoryItem.java:10: InventoryItem should be declared abstract; it does not define display() in InventoryItem 1 error Output Window Abstract method, but not an abstract class

18 18 Why use Abstract classes? Declaring a class to be abstract is good programming practice: ●if we don't intend to create an object directly from a class, then declaring it to be abstract forces us to respect this ●if we want to create polymorphic methods at the lower levels, instead of providing a method body which we don't intend to use, declare the method to be abstract. This also forces us to provide a method for the subclasses - Java won't let us forget.

19 19 Final methods and classes If we declare a method to be final, then it cannot be overridden in any subclass. e.g. public final String displayName() {... We can still invoke calls to the method in a subclass, but it will not be polymorphic. If we declare a class to be final, then it cannot be extended - i.e. we cannot create subclasses of a final class. e.g. public final class Friend {...


Download ppt "1 CS2200 Software Development Polymorphism II A. O’Riordan, 2008 K. Brown, 2004-2007."

Similar presentations


Ads by Google