Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing.

Similar presentations


Presentation on theme: "Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing."— Presentation transcript:

1 Object-Oriented Design CSC 212

2 Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing you (and my chance to try and explain it differently and help solve your confusion) Get additional Java review now  Homework #1 on web; due in one week

3 Object Inheritance Review Subclass represents es can extend or specialize the superclass Use extends to declare a subclass public class subclass extends superclass {...} public class Robin extends Bird {...} public class Van extends Automobile {...} public class ProfHertz extends Genius {...}  Subclass inherits non-private fields and methods  As if fields & methods were pasted into subclass code

4 Object Inheritance Review Inheritance represents “is a” relationship  E.g., Robin “is a” Bird; Van “is a” Automobile; Prof. Hertz “is a” Genius  Sorry, I couldn’t resist the last example Subclass can specialize superclass  Subclass can add functionality  Subclass can define additional data  Subclass can specialize how instances operate

5 Reusing Fields in Subclasses Subclasses can hide superclass’s fields  Subclass defines field with identical name  Field from superclass also exists in subclass super. accesses superclass’s field Which field Java will access? What does this depend on?

6 Field Hiding Example public class SuperClass { public SuperClass() { } protected String myString = “SUPERSTRING”; public String getMyString() { return myString; } } public class SubClass extends SuperClass { public SubClass() { } protected String myString = “substring”; public String getMyString() { return myString; } public String getOldString() { return super.myString; }

7 Field Hiding Example public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass supr = sub; System.out.println(sub.getMyString()); System.out.println(supr.getMyString()); System.out.println(sub.myString); System.out.println(supr.myString); System.out.println(sub.getOldString()); }

8 Reusing Names Review Two ways of reusing method names:  Overloading – same name, different signature  Overriding – same name, same signature  Java calls the method appropriate to the actual object instance, NOT the variable type Reusing field name hides inherited field  Both fields exist within the class  Java uses field appropriate to variable type

9 Daily Quiz #1 Do problem R-2.10 (p. 96) from the book

10 Polygons Consider polygon classes:  Rectangle, Square, Triangle, … Want generic Polygon class  Superclass for all these classes  Make arrays of Polygons possible  Enables Polygon as method parameter

11 Polygons Polygons have at least two common methods:  float area() and float circumference()  But their implementation is class specific How can we do this?

12 Polygon Class public abstract class Polygon { public abstract double area(); public abstract double circumference(); public boolean isPlanar() { return true; } } Abstract methods have no body  Serve as placeholder for subclasses to define  Guarantee minimal class functionality

13 Abstract Class Abstract classes cannot be instantiated  Any class could be declared abstract Subclass need not override all abstract methods  But subclass will also be abstract Can instantiate subclasses of abstract class only if all inherited abstract methods overriden

14 Square Class public class Square extends Polygon { protected double side; public Square() { this(1.0); } public Square(double length) { side=length; } public double area() { return side * side; } public double circumference() { return side * 4; } public double getLength() { return side; } public double getWidth() { return side; } } Can we instantiate Square ?

15 Benefits of Abstract Classes Polygon[] polys = new Polygon[3]; polys[0] = new Square(2.0); polys[1] = new Triangle(...); polys[2] = new Square(5.6); double total_area=0.0; for (int i=0; i<shapes.length; i++) total_area += shapes[i].area();

16 Abstract Class Summary Abstract classes can contain fields, abstract methods, and normal methods  Superclass can specify common functionality  Subclasses required to implement methods  Any class with abstract method(s) is abstract

17 Abstract Class Summary Abstract class cannot be instantiated  But class can be extended  Subclasses could then be instantiated Declaring methods abstract forces subclasses to implement them  Can be good  Can be bad

18 Public Methods (Explicitly Stated) (Important!) Interfaces Interface is Java’s way to define an ADT  Design document for external class features Class Details (HIDDEN!) (What do I care?)

19 Interfaces Interfaces declare public abstract methods  Cannot define any other methods Interfaces can declare fields  Fields have constant value (public static final) Classes implement interfaces  Class can implement more than 1 interface  Must implement all the interfaces’ methods

20 Methods in an Interface All methods are abstract  Adding abstract qualifier is optional Methods cannot have any implementation  Implementation left strictly to Classes Methods public by default  Methods cannot be native, static, synchronized, or final

21 Goal of Interface We want Polygons to be drawable  But want to make other classes drawable, too Could make an abstract Drawable class  Classes can’t extend Polygon AND Drawable Solution: Make Drawable an interface!  Provides way of multiple inheritence

22 Declaring Interfaces public interface Drawable { public void setColor(Color c); public void setPosition(double x, double y); public void draw(Graphics g); } public interface TransparentDrawable extends Drawable { public void setTransparency(int tLevel); }

23 Using Interfaces public class DrawableSquare extends Square implements Drawable { private Color c; private double x,y; public DrawableSquare(double length) { super(length); } public void setColor(Color col) { c = col; } public void setPosition (double x_pos, double y_pos) { x=x_pos; y=y_pos; } public void draw(Graphics g) { g.drawRect(x, y, side, side, c); } }

24 Benefits of Interfaces public void drawRed(Drawable d, Graphics g) { d.setColor(red); d.draw(g); } Which of these calls are legal? drawRed(new Square(4), g); drawRed(new SquareDrawable(4), g); drawRed(new TriangleDrawable(…), g); drawRed(new DrawableIcon(…), g);

25 Last Word on Interfaces Interfaces can have sub-interfaces  Sub-interface extends its super-interface Inherits all abstract methods and static final fields Can then further declare more to list  Classes implementing sub-interface automatically implement super-interface Will need to implement all the abstract methods

26 Typecasting Polygon square = new Sqare(); double r = square.getLength(); Won’t compile!! Variable square is declared to be Polygon  Can only use methods defined on Polygon  Declaration specifies which methods can run  Actual type determines which methods do run

27 How To Get a Square from a Polygon Polygon c = new Square(); double l = ((Square)c).getLength(); Typecasting forces c to act like square This now compiles (and works) But be careful with this power  This also compiles, but will not work: int i = 9; double l = ((Square)i).getLength();

28 Widening Conversions String s = new String(“hello”); Object obj; obj = s; This is a widening conversion  Assigns a String to variable of superclass type Widening conversions are always legal  By inheritance, a String “is a” Object  Do not need to perform any typecasting

29 Narrowing Conversions String s = new String(“bye”); Object obj = s; String t = obj; // NO! NO! NO! Java cannot perform narrowing coversions  Assigning superclass variable to subclass variable  Cannot always tell if this is legal  Must be done using typecasting: String t = ((String)obj);


Download ppt "Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing."

Similar presentations


Ads by Google