Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 – Data Structures Lecture 11: More Inheritance & Generic Types.

Similar presentations


Presentation on theme: "CSC 212 – Data Structures Lecture 11: More Inheritance & Generic Types."— Presentation transcript:

1 CSC 212 – Data Structures Lecture 11: More Inheritance & Generic Types

2 Polygons All polygons share certain details  Defined by vertices/edges  Have area and circumference But how can this be implemented?  How area computed differs by polygon  Number of vertices or edges Also, cannot have a “polygon”  Only have triangles, squares, dodecagons…

3 Abstract Keyword Abstract classes is a class  Declared via keyword abstract  Can declare fields & methods  Can also declare abstract methods Abstract methods promise functionality  These methods only contain declaration  Method will need to be defined in subclasses Subclasses that do not define all abstract methods are also abstract

4 Using Abstract Classes Abstract classes cannot be instantiated  Only contains common data & functionality Can have variables of that type however  Can refer to any subclass instances  Since instances exist, abstract methods define  Thus, can call abstract methods with variables

5 Using An Abstract Class public abstract class Polygon { protected Vertex[] verts; public abstract double area(); public double circumference() { double retVal = 0; for (int i = 1; i < verts.length; i++) { retVal+=verts[i].distance(verts[i-1]); } retVal+=verts[0].distance(verts[verts.length- 1]); return retVal; } } public class Square extends Polygon { /* Constructor skipped due to lack of space */ public double area() { return Math.pow(verts[1].distance(verts[0]), 2); } }

6 Using An Abstract Class (2) Polygon[] polys = new Polygon[5]; polys[0] = new Square(...); polys[1] = new Enneagon(...); polys[2] = new Tetrakaidecagon(...); polys[3] = new Chiliagon(...); polys[4] = new Myriagon(...); double totalArea=0.0; double totalCircum=0.0; for (int i=0; i<polys.length; i++) { totalCircum += polys[i].circumference(); totalArea += polys[i].area(); }

7 Abstract Class Summary Specify common data & methods  Can contain fields, methods, & abstract methods  Any class with abstract method(s) is abstract  Cannot instantiate class if declared abstract Subclasses can be instantiated  But must define abstract methods

8 Goal of Interfaces We want Polygons to be drawable  But other classes should be drawable, too Could define abstract class Drawable  But cannot extend Polygon & Drawable Interfaces guarantee functionality  Can cut across class definitions  Provides second form of inheritance

9 Interfaces Declare public abstract methods  Cannot define any other methods Declare necessary constants  Fields must be public static final Classes implement interfaces  Can implement more than 1 interface  Must define all methods from interface

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

11 Using Interfaces public class DrawableSquare extends Square implements Drawable{ private Color c; private double x; private double y; /* Constructor skipped due to lack of space */ 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); } }

12 Using Interfaces Cannot instantiate an interface… …but can have variables of interface type  Refer to instances of classes implementing interface  Classes required to implement interface’s methods public void drawRed(Drawable d, Graphics g) { d.setColor(red); d.draw(g); }

13 Last Word on Interfaces Interfaces have sub-interfaces  Sub-interface extends super-interface Inherits methods Adds to methods that must be implemented Classes implementing sub-interface implement super-interface  Must implement all abstract methods

14 Typecasting Polygon poly = new Square(); Square s = poly; Square s = ((Square)poly); Directs Java to compile illegal code Errors now occur at runtime  This code will compile, but cannot actually run int i = 9; Square s = ((Square)i);

15 Narrowing Conversions Object obj = new String(“bye”); String t = obj; // NO! NO! NO! Java cannot compile narrowing coversions  Assign superclass to subclass variable  Could be illegal; compiler will not allow it  These assignments must be typecast

16 Handling Variety of Objects Suppose field could refer to any object  Note: This is true for the rest of the class Define thousands of fields and use one  Good luck writing that one! Could make field be of type Object  Every class is a subclass of Object  Would require lots of typecasting  Talk to prior CSC212 students about this

17 Generic Types Recent release of Java added generics Class includes/uses a type parameter Actual type filled in at instantiation  Code runs as if were written using that type  Different instances can use different types  Actual type becomes part of variable type

18 Class With Generics public class ONode { private Object data; public ONode(Object d) { data = d; } public Object getData() { return data; } public void setData(Object dat) { data = dat; } } public class Node { private T data; public Node(T d) { data = d; } public T getData() { return data; } public void setData(T dat) { data = dat; } }

19 Using Generics Integer i; Scanner s;... ONode n = new ONode(5); i = ((Integer)n.getData()); s = ((Scanner)n.getData()); // Compiles fine, error when run n.setData(s); i = ((Integer)n.getData()); // Compiles fine, error when run s = ((Scanner)n.getData()); Integer i; Scanner s;... Node n = new Node (5); i = n.getData(); s = n.getData(); // Will not compile n.setData(s); // Also will not compile Node m = new Node (s); i = m.getData(); // Now this does not compile s = m.getData();

20 Before Next Lecture… Complete week #4 assignment Continue programming assignment #1  It is due 1 week from today Start reviewing for Midterm #1  It will be 1 week from Monday Monday’s lecture completes this lecture and talks about arrays  Read through Section 3.1 of the book


Download ppt "CSC 212 – Data Structures Lecture 11: More Inheritance & Generic Types."

Similar presentations


Ads by Google