COMP200 INTERFACES OOP using Java, from slides by Shayan Javed 2 - - PowerPoint PPT Presentation

comp200
SMART_READER_LITE
LIVE PREVIEW

COMP200 INTERFACES OOP using Java, from slides by Shayan Javed 2 - - PowerPoint PPT Presentation

1 1 COMP200 INTERFACES OOP using Java, from slides by Shayan Javed 2 Interfaces 3 ANIMAL picture food sleep() roam() makeNoise() eat() 4 ANIMAL picture food sleep() roam() makeNoise() eat() CANINE FELINE roam() roam() 5


slide-1
SLIDE 1

1

COMP200


INTERFACES

OOP using Java, from slides by Shayan Javed

1

slide-2
SLIDE 2

2

Interfaces

slide-3
SLIDE 3

3 ANIMAL

picture food sleep() roam() makeNoise() eat()

slide-4
SLIDE 4

4 ANIMAL

picture food sleep() roam() makeNoise() eat()

FELINE roam() CANINE roam()

slide-5
SLIDE 5

5 ANIMAL

picture food sleep() roam() makeNoise() eat()

FELINE roam() CANINE roam() LION makeNoise() eat() CAT makeNoise() eat() WOLF makeNoise() eat() DOG makeNoise() eat()

slide-6
SLIDE 6

6

Problem

Need to add Pet behaviors to the pet animal

classes (Cat and Dog):

  • play()
slide-7
SLIDE 7

7

Option 1

Add concrete methods:

play() to Animal class

Animal Feline Canine Lion Cat Wolf Dog

slide-8
SLIDE 8

8

Option 1

Add concrete methods:

play() to Animal class

What’s the problem?

Animal Feline Canine Lion Cat Wolf Dog

slide-9
SLIDE 9

9

Option 1

Add concrete methods:

play() to Animal class

What’s the problem? Also exist for Lion/Wolf

Animal Feline Canine Lion Cat Wolf Dog

slide-10
SLIDE 10

10

Option 2

Add abstract methods: play() to Animal class

Animal Feline Canine Lion Cat Wolf Dog

slide-11
SLIDE 11

11

Option 2

Add abstract methods: play() to Animal class Provide empty body for it in Lion and Wolf classes

Animal Feline Canine Lion Cat Wolf Dog

slide-12
SLIDE 12

12

Option 2

Add abstract methods: play() to Animal class Provide empty body for it in Lion and Wolf classes Provide non-empty body for it in Cat and Dog classes

Animal Feline Canine Lion Cat Wolf Dog

slide-13
SLIDE 13

13

Option 3

Add concrete methods:

play() to Cat and Dog classes

Animal Feline Canine Lion Cat Wolf Dog

slide-14
SLIDE 14

14

What we need

Pet behaviors just in Pet classes (like Dog and

Cat)

slide-15
SLIDE 15

15

What we need

Pet behaviors just in Pet classes (like Dog and

Cat)

Consistency - Guarantee that all Pet classes

use the same signature for Pet methods

slide-16
SLIDE 16

16

What we need

Pet behaviors just in Pet classes (like Dog and

Cat)

Consistency - Guarantee that all Pet classes

use the same signature for Pet methods

Take advantage of Polymorphism

slide-17
SLIDE 17

17

Multiple Inheritance?

Animal Feline Canine Lion Cat Wolf Dog Pet

slide-18
SLIDE 18

18

Multiple Inheritance?

Animal Feline Canine Lion Cat Wolf Dog Pet But Java does not allow multiple inheritance! Why?

slide-19
SLIDE 19

19

Multiple Inheritance?

Not allowed Mainly to avoid complexity/confusion. Want to keep it simple.

slide-20
SLIDE 20

20

The Diamond Problem

SuperClass method() A method() B method() C

slide-21
SLIDE 21

21

The Diamond Problem

SuperClass method() A method() B method() C

What happens when method() is called in C?

slide-22
SLIDE 22

22

Solution: Interfaces

Animal Feline Canine Lion Cat Wolf Dog Pet abstract play(); Make Pet an interface and put all pet behaviors in it as abstract methods

slide-23
SLIDE 23

23

Interfaces

Classlike construct

  • contains only constants and abstract

methods.

slide-24
SLIDE 24

24

Interfaces

Classlike construct

  • contains only constants and abstract

methods.

  • cannot contain variables or concrete

methods.

slide-25
SLIDE 25

25

Interfaces

Classlike construct

  • contains only constants and abstract methods.
  • cannot contain variables or concrete methods.

Declaration:

public interface <InterfaceName> { // constant declarations; // method signatures; }

slide-26
SLIDE 26

26

Interface Pet

public interface Pet { public void play(); }

slide-27
SLIDE 27

27

Implementing Pet interface

class Dog extends Canine implements Pet{ public void play(){ System.out.println(“Catch ball”); } }

slide-28
SLIDE 28

28

Implementing Pet interface

class Dog extends Canine implements Pet{ public void play(){ System.out.println(“Catch ball”); } } class Cat extends Feline implements Pet { public void play(){ System.out.println(“Roll ball”); } }

slide-29
SLIDE 29

29

Interface Characteristics

Interfaces are defined in their own file

slide-30
SLIDE 30

30

Interface Characteristics

Interfaces are defined in their own file Methods are abstract. No implementations. May omit the abstract keyword.

slide-31
SLIDE 31

31

Interface Characteristics

Interfaces are defined in their own file Methods are abstract. No implementations. May omit the abstract keyword. Do not have instance fields.

slide-32
SLIDE 32

32

Interface Characteristics

Interfaces are defined in their own file Methods are abstract. No implementations. May omit the abstract keyword. Do not have instance fields. Can have constant fields. Automatically treated as

public, static, final; may omit any of these keywords

slide-33
SLIDE 33

33

Example

public interface Interface1 {

int aNumber = 0; public void aMethod();

}

slide-34
SLIDE 34

34

Example

public interface Interface1 {

int aNumber = 0; public void aMethod();

} Same as: public interface Interface1 {

public static final int aNumber = 0; public abstract void aMethod();

}

slide-35
SLIDE 35

35

Interface Characteristics

A class can implement any number of interfaces

public class aClass implements I1, I2, I3 { // methods from I1, I2, I3... }

slide-36
SLIDE 36

36

Interface Characteristics

A class can implement any number of interfaces

public class aClass implements I1, I2, I3 { // methods from I1, I2, I3... }

Interfaces can not be instantiated

slide-37
SLIDE 37

37

Interface Characteristics

A class can implement any number of interfaces

public class aClass implements I1, I2, I3 { // methods from I1, I2, I3... }

Interfaces can not be instantiated But can be used as a data type for a variable

slide-38
SLIDE 38

38

Using the interface

Pet[] pets = new Pet[2]; pet[0] = new Dog(); pet[1] = new Cat(); for(int i = 0; i < pets.length; i ++) pet[i].play();

slide-39
SLIDE 39

39

Using the interface

Pet[] pets = new Pet[2]; pet[0] = new Dog(); pet[1] = new Cat(); for(int i = 0; i < pets.length; i++) pet[i].play(); Similar to abstract classes – advantage of polymorphism

slide-40
SLIDE 40

40 Animal Feline Canine Lion Cat Wolf Dog Pet abstract play(); Robot RoboDog Roomba Classes from different inheritance hierarchy can implement same interface!

slide-41
SLIDE 41

41

Abstract Classes vs. Interfaces

Variables Constructors Methods

Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator. No restrictions. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods

slide-42
SLIDE 42

42

Abstract Classes vs. Interfaces

Variables Constructors Methods

Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator. No restrictions. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods

slide-43
SLIDE 43

43

Abstract Classes vs. Interfaces

Variables Constructors Methods

Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator. No restrictions. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods

slide-44
SLIDE 44

44

Abstract Classes vs. Interfaces

Cannot inherit from multiple classes but, Can can implement multiple interfaces Interfaces = model “can-do” type

relationships

Inheritance = model “is-a” type of

relationship

slide-45
SLIDE 45

45

Next Lecture

More on interfaces