13
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Embed Size (px)

Citation preview

Page 1: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

LECTURE 9:INTERFACES & ABSTRACT CLASSES

CSC 212 – Data Structures

Page 2: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Inheritance Issues

Multiple inheritance causes many problems What if field declared in multiple

superclasses? Which constructor will super() chain to? Java avoid by requiring classes extend

exactly 1 class Often mix multiple ideas within a classpublic class Student { … }

public class Employee { … }

public class StudentEmployee extends

Page 3: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

abstract

Can declare methods as abstract Methods cannot be defined; promise

functionality Methods can have parameters, throw

exceptions, etc.public abstract void foo(int i);public abstract Beer bar(String o);

Undefined abstract methods make class abstract Cannot instantiate any abstract class Can have fields & normal methods also

allowed Subclasses can override abstract

methods Subclass abstract if not overriding all

abstract methods

Page 4: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Interfaces in Real World

Page 5: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Interfaces

Declare important constant fields public static final must be used for

fields Declare public abstract methods

Methods callable anywhere by any class But method’s body cannot be defined in

interface Classes implement interfaces

Implementing classes define interface’s methods

Avoids problem of multiple inheritance No problem implementing more than 1

interface Unrelated to superclass choice for a class

Page 6: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Adding to an Interface

Interfaces have sub-interfaces Sub-interface extends super-interface Super-interface’s methods inherited by sub-

interface Inherits methods from super-super-

interface, too Sub-interface can extend multiple

interfaces Methods are not defined so this is legal Similar to how classes implement multiple

interfaces Must implement all interface’s methods

Includes methods inherited from super-interface

Also requires implementing interface’s methods

Page 7: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Declaring Interfaces

public interface Drawable { public void setColor(Color c); public Color getColor(); public void draw(Graphics g);}

public interface TranDraw extends Drawable{ public void setTransparency(int tLevel);}

public interface MoveDraw extends Drawable{ public void setPosition(int x, int y);

}

Page 8: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Implementing Interfaces

public class Square implements Drawable {private Color c;private int x, y, side;

public Square(Color col, int len) { c = col; side = len;}

public void setColor(Color col){ c=col; }public Color getColor() { return c; }public void draw(Graphics g) { g.drawRect(x, y, side, side, c);}

}

Page 9: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Using Interfaces

Cannot instantiate an interface… Can only ever create instances of a class

…but can have variables of interface type Instance of classes implementing interface

referenced Methods must be implemented by classes

public void drawRed(Drawable d, Graphics g) {d.setColor(Color.red);d.draw(g);

}

Page 10: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Interface vs. Abstract Class Concepts serve similar purposes

Cannot instantiate either of these types Both used for variable, field, & parameter

types Can extend classes only

Only classes have fields & method bodies Use abstract class when…

Classes need method or private field in common

… in all other cases use an interface

Page 11: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Typecasting

Shape poly = new Square();Square s = poly;Square s = ((Square)poly);

Directs Java to compile illegal code Errors occur at runtime instead

This code compiles, but still illegal

int i = 9;Square s = ((Square)i);

Page 12: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Narrowing Conversions

Java cannot compile narrowing conversions Assigns superclass/interface to lower

variable Compiler will not allow it, but could be

legal Typecasting required for these assignments

Object obj = new String(“bye”);

String sad = obj; // Does not work

String glad = (String)obj; // works!

Page 13: LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures

Before Next Lecture…

Finish work on week #3 homework Continue working on program

assignment #1 Readings for Wed. on Angel/web’s

schedule page I will be posting code, but there will not be

slides… Discuss how to write GUI programs Learning Java’s Swing classes