square draw() rotate() setlength() getlength() circle draw() getradius() setradius() shape erase()...

46
Lecture 16 COP3502: Introduction to CIS I

Upload: jeffrey-james

Post on 18-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Lecture 16COP3502: Introduction to CIS I

TodayInterfaces and Polymorphism

Wednesday, Friday, MondayGUIs, Animation, Events

In-class reviewWed Feb 26

In-class Exam Fri Feb 28

Will cover everything up through next Monday’s class

Super SessionThursday Feb 27 8pm-10pm

Tuesday Feb 18Lab 3 (Objects)

Tuesday Feb 25Lab 4 (GUIs)

Problem Set 3

Reading

Thinking in Java (Eckel)Ch. 1

Intro to Java Programming (Eck)Ch. 5

Square

draw()rotate()setLength()getLength()

Circle

draw()getRadius()setRadius()

Shape

Erase()Move()setX()setY()setColor()getPos()getColor()

Triangle

draw()rotate()setLength()getLength()

Hexagon

draw()rotate()setLength()getLength()

classes can be both a superclass and a subclass at the same time!

Reptile

move()eat()reproduce()

Animal

move()eat()reproduce()

Mammal

move()eat()reproduce()

Fish

move()eat()reproduce()

Lion Tiger Bear

classes can only inherit from one superclass

uses of inheritance

1. Organizing information2. Grouping similar classes

3. Modeling similarity among classes4. Creating a taxonomy among objects

What does the subclass inherit?

public methodsprotected methods

protected instance variables

What does the subclass inherit?

public methodsprotected methods

protected instance variables

NOT PRIVATE ANYTHING

subclass specializes superclass

subclass specializes superclass

adding new methods

subclass specializes superclass

adding new methodsoverriding existing methods

subclass specializes superclass

adding new methodsoverriding existing methods

Implementing abstract methods

subclass specializes superclass

adding new methodsoverriding existing methods

Implementing abstract methods

Superclass “factors out” common capabilities among subclasses

abstract methods

empty methods in the superclassneed to be implemented by inheriting subclass

uses abstract keyword

abstract methods

empty methods in the superclassneed to be implemented by inheriting subclass

uses abstract keyword

abstract void draw();

any class with an abstract method should itself be declared abstract

abstract class Shape {…abstract void draw();…

}

overriding methods

public class Square extends Shape {…@Overridepublic void draw() {

…}…

}

method resolution

compiler walks up the class inheritance hierarchy tree until it finds the appropriate method

super keyword

public class Square extends Shape {…public Square(double x, double y, Color c, double len) {

super(x, y, c);length = len;

}…

}

super keyword

public class Shape {…public void rotate(double radians) {

//perform rotation}…

}

Pretend this method exists in our Shape implementation!

super keyword

public class Square extends Shape {…@Overridepublic void rotate(double degrees) {

double radians = Math.PI*degrees/180;super.rotate(radians);

}…

}

Same as Shape.rotate()

super keyword

public class Square extends Shape {…@Overridepublic void rotate(double degrees) {

double radians = Math.PI*degrees/180;super.rotate(radians);

}…

}

Same as Shape.rotate()

partial overriding

classes can only inherit from one superclass

classes can only inherit from one superclass

But they can inherit from multiple interfaces

Subclass

Superclass Interface Interface

interface

a set of instance methods without any implementation

ie. a collection of abstract methods

interface

“factors out” commonality among very different classes that share behavior

both have the ability to be “colored”might implement a Colorable interface

interfaces declare methods, but don’t include any implementation

interfaces

No constructorusually no instance variablesjust a list of responsibilities

even more abstract than abstract classes

uses of interfaces

give specific properties to an objectUsually an adjective that ends in –ive or –able

ColorableRotatable

Bounceable

uses of interfaces

give role to an objectUsually ends in –er

ContainerMover

Teacher

public interface Colorable {

// set the current colorpublic void setColor(Color color);

// get the current colorpublic Color getColor();

}

public interface Decorable {

// models something which can be decorated public void decorate(Decoration dcr); }

interfaces can extend any number of other interfaces

subclass inherits all superclass methods AND all interfaces

public interface Artistic extends Colorable, Decorable {

// probably uses decorate and change color methods public void putOnDisplay(); }

Class Interface

Models an object with properties and behavior

Models a role and defines a set of responsibilities

Factors out common properties and behavior of similar objects

Factors out common behavior, but not properties, of usually dissimilar objects

Declares methods and may define some or all of them

Declares but does not define methods

Can only extend one superclass Can extend multiple interfaces

Can implement any number of interfaces

No implementation

methods for factoring out common code

loops and functions

methods for factoring out common code

loops and functionsclasses and instances

methods for factoring out common code

loops and functionsclasses and instances

superclasses and subclasses

methods for factoring out common code

loops and functionsclasses and instances

superclasses and subclassesinterfaces and implementations