cop 3503 fall 2012 shayan javed lecture 6

45
1 / 45 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 6 Programming Fundamentals using Java 1

Upload: rowena

Post on 22-Feb-2016

31 views

Category:

Documents


1 download

DESCRIPTION

COP 3503 FALL 2012 Shayan Javed Lecture 6. Programming Fundamentals using Java. Interfaces. Animal picture food sleep() roam() makeNoise () eat(). Animal picture food sleep() roam() makeNoise () eat(). Canine roam(). Feline roam(). Animal picture food sleep() roam() - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COP 3503  FALL 2012 Shayan Javed Lecture 6

1 / 45

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 6

Programming Fundamentals using Java

1

Page 2: COP 3503  FALL 2012 Shayan Javed Lecture 6

2 / 45

Interfaces

Page 3: COP 3503  FALL 2012 Shayan Javed Lecture 6

3 / 45

ANIMALpicturefoodsleep()roam()makeNoise()eat()

Page 4: COP 3503  FALL 2012 Shayan Javed Lecture 6

4 / 45

ANIMALpicturefoodsleep()roam()makeNoise()eat()

FELINE

roam()

CANINE

roam()

Page 5: COP 3503  FALL 2012 Shayan Javed Lecture 6

5 / 45

ANIMALpicturefoodsleep()roam()makeNoise()eat()

FELINE

roam()

CANINE

roam()

LIONmakeNoise()eat()

CATmakeNoise()eat()

WOLFmakeNoise()eat()

DOGmakeNoise()eat()

Page 6: COP 3503  FALL 2012 Shayan Javed Lecture 6

6 / 45

Problem

Need to add Pet behaviors to the pet animal classes (Cat and Dog):

• play()

Page 7: COP 3503  FALL 2012 Shayan Javed Lecture 6

7 / 45

Option 1

Add concrete methods: play() to Animal class

Animal

Feline Canine

Lion Cat Wolf Dog

Page 8: COP 3503  FALL 2012 Shayan Javed Lecture 6

8 / 45

Option 1

Add concrete methods: play() to Animal class

What’s the problem?

Animal

Feline Canine

Lion Cat Wolf Dog

Page 9: COP 3503  FALL 2012 Shayan Javed Lecture 6

9 / 45

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

Page 10: COP 3503  FALL 2012 Shayan Javed Lecture 6

10 / 45

Option 2

Add abstract methods: play() to Animal classAnimal

Feline Canine

Lion Cat Wolf Dog

Page 11: COP 3503  FALL 2012 Shayan Javed Lecture 6

11 / 45

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

Page 12: COP 3503  FALL 2012 Shayan Javed Lecture 6

12 / 45

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

Page 13: COP 3503  FALL 2012 Shayan Javed Lecture 6

13 / 45

Option 3

Add concrete methods: play() to Cat and Dog classes

Animal

Feline Canine

Lion Cat Wolf Dog

Page 14: COP 3503  FALL 2012 Shayan Javed Lecture 6

14 / 45

What we need

Pet behaviors just in Pet classes (like Dog and Cat)

Page 15: COP 3503  FALL 2012 Shayan Javed Lecture 6

15 / 45

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

Page 16: COP 3503  FALL 2012 Shayan Javed Lecture 6

16 / 45

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

Page 17: COP 3503  FALL 2012 Shayan Javed Lecture 6

17 / 45

Multiple Inheritance?

Animal

Feline Canine

Lion Cat Wolf Dog

Pet

Page 18: COP 3503  FALL 2012 Shayan Javed Lecture 6

18 / 45

Multiple Inheritance?

Animal

Feline Canine

Lion Cat Wolf Dog

Pet

But Java does not allow multiple inheritance! Why?

Page 19: COP 3503  FALL 2012 Shayan Javed Lecture 6

19 / 45

Multiple Inheritance?

Not allowed

Mainly to avoid complexity/confusion.

Want to keep it simple.

Page 20: COP 3503  FALL 2012 Shayan Javed Lecture 6

20 / 45

The Diamond Problem

SuperClass

method()

A

method()B

method()

C

Page 21: COP 3503  FALL 2012 Shayan Javed Lecture 6

21 / 45

The Diamond Problem

SuperClass

method()

A

method()B

method()

C

What happens when method() is called in C?

Page 22: COP 3503  FALL 2012 Shayan Javed Lecture 6

22 / 45

Solution: Interfaces

Animal

Feline Canine

Lion Cat Wolf Dog

Petabstract play();

Make Pet an interface and put all pet behaviors in it as abstract methods

Page 23: COP 3503  FALL 2012 Shayan Javed Lecture 6

23 / 45

Interfaces

Classlike construct• contains only constants and abstract methods.

Page 24: COP 3503  FALL 2012 Shayan Javed Lecture 6

24 / 45

Interfaces

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

Page 25: COP 3503  FALL 2012 Shayan Javed Lecture 6

25 / 45

Interfaces

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

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

Page 26: COP 3503  FALL 2012 Shayan Javed Lecture 6

26 / 45

Interface Pet

public interface Pet {public void play();

}

Page 27: COP 3503  FALL 2012 Shayan Javed Lecture 6

27 / 45

Implementing Pet interface

class Dog extends Canine implements Pet{

public void play(){ System.out.println(“Catch ball”);

} }

Page 28: COP 3503  FALL 2012 Shayan Javed Lecture 6

28 / 45

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”);

} }

Page 29: COP 3503  FALL 2012 Shayan Javed Lecture 6

29 / 45

Interface Characteristics

Interfaces are defined in their own file

Page 30: COP 3503  FALL 2012 Shayan Javed Lecture 6

30 / 45

Interface Characteristics

Interfaces are defined in their own file

Methods are abstract. No implementations. May omit the abstract keyword.

Page 31: COP 3503  FALL 2012 Shayan Javed Lecture 6

31 / 45

Interface Characteristics

Interfaces are defined in their own file

Methods are abstract. No implementations. May omit the abstract keyword.

Do not have instance fields.

Page 32: COP 3503  FALL 2012 Shayan Javed Lecture 6

32 / 45

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

Page 33: COP 3503  FALL 2012 Shayan Javed Lecture 6

33 / 45

Example

public interface Interface1 {int aNumber = 0;

public void aMethod();}

Page 34: COP 3503  FALL 2012 Shayan Javed Lecture 6

34 / 45

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();}

Page 35: COP 3503  FALL 2012 Shayan Javed Lecture 6

35 / 45

Interface Characteristics

A class can implement any number of interfaces

public class aClass implements I1, I2, I3 {

// methods from I1, I2, I3...

}

Page 36: COP 3503  FALL 2012 Shayan Javed Lecture 6

36 / 45

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

Page 37: COP 3503  FALL 2012 Shayan Javed Lecture 6

37 / 45

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

Page 38: COP 3503  FALL 2012 Shayan Javed Lecture 6

38 / 45

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();

Page 39: COP 3503  FALL 2012 Shayan Javed Lecture 6

39 / 45

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

Page 40: COP 3503  FALL 2012 Shayan Javed Lecture 6

40 / 45

Animal

Feline Canine

Lion Cat Wolf Dog

Petabstract play();

Robot

RoboDog Roomba

Classes from different inheritance hierarchy can implement same interface!

Page 41: COP 3503  FALL 2012 Shayan Javed Lecture 6

41 / 45

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

Page 42: COP 3503  FALL 2012 Shayan Javed Lecture 6

42 / 45

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

Page 43: COP 3503  FALL 2012 Shayan Javed Lecture 6

43 / 45

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

Page 44: COP 3503  FALL 2012 Shayan Javed Lecture 6

44 / 45

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

Page 45: COP 3503  FALL 2012 Shayan Javed Lecture 6

45 / 45

Next Lecture

More on interfaces