Cop3502 introduction to cis i
This presentation is the property of its rightful owner.
Sponsored Links
1 / 46

Lecture 16 PowerPoint PPT Presentation


  • 106 Views
  • Uploaded on
  • Presentation posted in: General

COP3502: Introduction to CIS I. Lecture 16. Today Interfaces and Polymorphism Wednesday, Friday, Monday GUIs, Animation, Events. In-class review Wed Feb 26 In-class Exam Fri Feb 28 Will cover everything up through next Monday’s class. Super Session Thursday Feb 27 8pm-10pm.

Download Presentation

Lecture 16

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cop3502 introduction to cis i

COP3502: Introduction to CIS I

Lecture 16


Lecture 16

Today

Interfaces and Polymorphism

Wednesday, Friday, Monday

GUIs, Animation, Events


Lecture 16

In-class review

Wed Feb 26

In-class Exam

Fri Feb 28

Will cover everything up through next Monday’s class


Lecture 16

Super Session

Thursday Feb 278pm-10pm


Lecture 16

Tuesday Feb 18

Lab 3 (Objects)

Tuesday Feb 25

Lab 4 (GUIs)


Lecture 16

Problem Set 3


Lecture 16

Reading

Thinking in Java (Eckel)

Ch. 1

Intro to Java Programming (Eck)

Ch. 5


Lecture 16

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


Lecture 16

classes can only inherit from one superclass


Lecture 16

uses of inheritance

  • Organizing information

  • Grouping similar classes

  • Modeling similarity among classes

  • Creating a taxonomy among objects


Lecture 16

What does the subclass inherit?

public methods

protected methods

protected instance variables


Lecture 16

What does the subclass inherit?

public methods

protected methods

protected instance variables

NOT PRIVATE ANYTHING


Lecture 16

subclass specializes superclass


Lecture 16

subclass specializes superclass

adding new methods


Lecture 16

subclass specializes superclass

adding new methods

overriding existing methods


Lecture 16

subclass specializes superclass

adding new methods

overriding existing methods

Implementing abstract methods


Lecture 16

subclass specializes superclass

adding new methods

overriding existing methods

Implementing abstract methods

Superclass “factors out” common capabilities among subclasses


Lecture 16

abstract methods

empty methods in the superclass

need to be implemented by inheriting subclass

uses abstract keyword


Lecture 16

abstract methods

empty methods in the superclass

need to be implemented by inheriting subclass

uses abstract keyword

abstract void draw();


Lecture 16

any class with an abstract method should itself be declared abstract

abstract class Shape {

abstract void draw();

}


Lecture 16

overriding methods

public class Square extends Shape {

@Override

public void draw() {

}

}


Lecture 16

method resolution

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


Lecture 16

super keyword

public class Square extends Shape {

public Square(double x, double y, Color c, double len) {

super(x, y, c);

length = len;

}

}


Lecture 16

super keyword

public class Shape {

public void rotate(double radians) {

//perform rotation

}

}

Pretend this method exists in

our Shape implementation!


Lecture 16

super keyword

public class Square extends Shape {

@Override

public void rotate(double degrees) {

double radians = Math.PI*degrees/180;

super.rotate(radians);

}

}

Same as Shape.rotate()


Lecture 16

super keyword

public class Square extends Shape {

@Override

public void rotate(double degrees) {

double radians = Math.PI*degrees/180;

super.rotate(radians);

}

}

partial overriding

Same as Shape.rotate()


Lecture 16

classes can only inherit from one superclass


Lecture 16

classes can only inherit from one superclass

But they can inherit from multiple interfaces

Superclass

Interface

Interface

Subclass


Lecture 16

interface

a set of instance methods without any implementation

ie. a collection of abstract methods


Lecture 16

interface

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


Lecture 16

both have the ability to be “colored”

might implement a Colorable interface


Lecture 16

interfaces declare methods, but don’t include any implementation


Lecture 16

interfaces

No constructor

usually no instance variables

just a list of responsibilities

even more abstract than abstract classes


Lecture 16

uses of interfaces

give specific properties to an object

Usually an adjective that ends in –ive or –able

Colorable

Rotatable

Bounceable


Lecture 16

uses of interfaces

give role to an object

Usually ends in –er

Container

Mover

Teacher


Lecture 16

public interface Colorable {

// set the current color

public void setColor(Color color);

// get the current color

public Color getColor();

}


Lecture 16

public interface Decorable {

// models something which can be decorated

public void decorate(Decoration dcr);

}


Lecture 16

interfaces can extend any number of other interfaces

subclass inherits all superclass methods AND all interfaces


Lecture 16

public interface Artistic extends Colorable, Decorable{

// probably uses decorate and change color methods

public void putOnDisplay();

}


Lecture 16

methods for factoring out common code

loops and functions


Lecture 16

methods for factoring out common code

loops and functions

classes and instances


Lecture 16

methods for factoring out common code

loops and functions

classes and instances

superclasses and subclasses


Lecture 16

methods for factoring out common code

loops and functions

classes and instances

superclasses and subclasses

interfaces and implementations


  • Login