Top Banner
CISC 3120 C09: Interface and Abstract Class and Method Hui Chen Department of Computer & Information Science CUNY Brooklyn College 9/28/2017 1 CUNY | Brooklyn College
26

CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

May 21, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

CISC 3120

C09: Interface and Abstract Class and Method

Hui Chen

Department of Computer & Information Science

CUNY Brooklyn College

9/28/2017 1CUNY | Brooklyn College

Page 2: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Outline

• Recap

• Polymorphism

• In-class group exercise

• Abstract method

• Abstract class

• Interfaces

• The Object super class

• The instanceof operator

9/28/2017 CUNY | Brooklyn College 2

Page 3: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

In-Class Group Exercise

• Shape, Circle, and Rectangle

• Discuss your solution with your team members

9/28/2017 CUNY | Brooklyn College 3

Page 4: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

The Shape Class

• Do you like the area() method here?

public class Shape {

public double area() {

System.out.println("This method is not supposed to be called.");

return 0;

}

}

• Are we ever going to instantiate a Shape object?

9/28/2017 CUNY | Brooklyn College 4

Page 5: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Abstract Class

• A class that is declared abstract

• Example

abstract class Animal {

}

• Abstract classes cannot be instantiated, but they can be subclassed.

9/28/2017 CUNY | Brooklyn College 5

Page 6: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Subclass & Instantiation

• Abstract classes cannot be instantiated, but they can be subclassed.

abstract class Animal {

}

• How about these examples?

9/28/2017 CUNY | Brooklyn College 6

Animal animal = new Animal(); class Dog extends Animal {…}

Page 7: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Subclass & Instantiation

• Abstract classes cannot be instantiated, but they can be subclassed.

abstract class Animal {

}

9/28/2017 CUNY | Brooklyn College 7

Animal animal = new Animal(); class Dog extends Animal {…}

Page 8: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Abstract Method

• A method that is declared without an implementation

abstract void makeNoise();

• A class that has an abstract method must be declared abstract

• How about these examples?

9/28/2017 CUNY | Brooklyn College 8

class Animal {

abstract void makeNoise();

}

abstract class Animal {

abstract void makeNoise();

}

Page 9: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Class with Abstract Method

• A class that has an abstract method must be declared abstract

9/28/2017 CUNY | Brooklyn College 9

class Animal {

abstract void makeNoise();

}

abstract class Animal {

abstract void makeNoise();

}

Page 10: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Subclass an Abstract Class

• Concrete subclass

• A subclass may provide implementations for all of the abstract methods in its parent class.

• Abstract subclass

• The subclass must also be declared abstract if it does not provide implementation of all of the abstract methods in its parent class.

9/28/2017 CUNY | Brooklyn College 10

Page 11: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Example: Animal Game

9/28/2017 CUNY | Brooklyn College 11

Animal

Feline Dove Whale

Cat Panther

Page 12: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Interfaces

• Not the “interface” in “Graphical User Interface”

• Java has a reference type, called interface

• Typically contain abstract methods only.

• Java 8 introduces the concept of default methods and permits static methods

• Interfaces cannot be instantiated

• can only be implemented by classes or extended by other interfaces

9/28/2017 CUNY | Brooklyn College 12

Page 13: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Example: Animal Game

9/28/2017 CUNY | Brooklyn College 13

Animal

Feline Dove Whale

Cat Panther

InterfaceFelineMotion

InterfaceBirdMotion

InterfaceWhaleMotion

Page 14: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Example: Birds Fly, Whales Swim, and Cats ...public interface BirdMotion {

public void fly(Direction direction, double speed, double distance);

}

public interface WhaleMotion {

public void swim(Direction direction, double speed, double distance);

}

public interface FelineMotion {

public void walk(Direction direction, double speed, double distance);

public void pounce(Animal prey);

}

9/28/2017 CUNY | Brooklyn College 14

Page 15: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Example: Implementing Interfacesabstract class Feline implements FelineMotion {

public void walk(Direction direction, double speed, double distance) { … }

public void pounce(Animal prey) { … }

}

class Dove extends Animal implements BirdMotion { …

public void fly(Direction direction, double speed, double distance) { … }

}

9/28/2017 CUNY | Brooklyn College 15

Page 16: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Using an Interface as a Type

• Interfaces are data types

void flyAll(ArrayList<BirdMotion> flyingAnimals) {

}

9/28/2017 CUNY | Brooklyn College 16

Page 17: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Evolving Interfaces

• Interfaces can be extended (like classes)

interface CatMotion extends FelineMotion {

public void tap(Animal animal) ;

}

9/28/2017 CUNY | Brooklyn College 17

Page 18: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Example: Extending FelineMotion

9/28/2017 CUNY | Brooklyn College 18

Animal

Feline Dove Whale

Cat Panther

InterfaceFelineMotion

InterfaceBirdMotion

InterfaceWhaleMotion

InterfaceCatMotion

interface CatMotion extends FelineMotion {

public void tap(Animal animal) ;

}

Page 19: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Implementing Multiple Interfaces

• A class can implement multiple interfaces

• But a class cannot extend multiple classes

• Which one of the following are is allowed in Java?

9/28/2017 CUNY | Brooklyn College 19

class FlyingCat extends Cat, Dove {

}

class FlyingCat implements BirdMotion, CatMotion {

}

Page 20: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Implementing Multiple Interfaces

• A class can implement multiple interfaces

• But a class cannot extend multiple classes

9/28/2017 CUNY | Brooklyn College 20

class FlyingCat extends Cat, Dove {

}

class FlyingCat implements BirdMotion, CatMotion {

}

Page 21: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Example: Flying Cat in the Magic Kindom

9/28/2017 CUNY | Brooklyn College 21

Animal

Feline Dove Whale

Cat Panther

InterfaceFelineMotion

InterfaceBirdMotion

InterfaceWhaleMotion

InterfaceCatMotion

FlyingCat

Page 22: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

What an object can do?

• Java may know what method an object can invoke only at runtime.

• As a programmer how do we cope?

• Use appropriate data types

void flyAll(ArrayList<BirdMotion> flyingAnimals) {

}

• Check object type at runtime (using instanceof)

9/28/2017 CUNY | Brooklyn College 22

Page 23: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Operator instanceof

• Evaluates to true if the object is a given type; false otherwise

public static void move(Animal animal) {

if (animal instanceof Cat) {

}

}

9/28/2017 CUNY | Brooklyn College 23

Page 24: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

The Object Super Class

• Java has a class called Object, like

• All classes are subclass of Object in Java

9/28/2017 CUNY | Brooklyn College 24

Object

boolean equals()Class getClass()int hasCode()

String toString()…

Page 25: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Questions

• Abstract class

• Abstract method

• Interfaces

• Extending abstract classes

• Implementing interfaces

• The instanceof operator

• The Object superclass

9/28/2017 CUNY | Brooklyn College 25

Page 26: CISC 3120 C09: Interface and Abstract Class and MethodSubclass an Abstract Class •Concrete subclass •A subclass may provide implementations for all of the abstract methods in its

Assignment

• To be available via Blackboard

• Project 2

9/28/2017 CUNY | Brooklyn College 26