Slide 1

advertisement
1 / 45
1
COP 3503 FALL 2012
SHAYAN JAVED
LECTURE 6
Programming Fundamentals using Java
2 / 45
Interfaces
3 / 45
4 / 45
5 / 45
6 / 45
Problem

Need to add Pet behaviors to the pet animal
classes (Cat and Dog):
•
play()
7 / 45
Option 1

Animal
Feline
Lion
Canine
Cat
Wolf
Dog
Add concrete methods:
play() to Animal class
8 / 45
Option 1
Animal
Feline
Lion

Add concrete methods:
play() to Animal class

What’s the problem?
Canine
Cat
Wolf
Dog
9 / 45
Option 1
Animal
Feline
Lion

Add concrete methods:
play() to Animal class

What’s the problem?

Also exist for Lion/Wolf
Canine
Cat
Wolf
Dog
10 / 45
Option 2
Add abstract methods:
play() to Animal class
Animal
Feline
Lion
Canine
Cat
Wolf
Dog
11 / 45
Option 2
Add abstract methods:
play() to Animal class
Animal
Feline
Lion
Provide empty body for it in
Lion and Wolf classes
Canine
Cat
Wolf
Dog
12 / 45
Option 2
Add abstract methods:
play() to Animal class
Animal
Feline
Lion
Provide empty body for it in
Lion and Wolf classes
Canine
Cat
Wolf
Dog
Provide non-empty body for
it in Cat and Dog classes
13 / 45
Option 3

Animal
Feline
Lion
Canine
Cat
Wolf
Dog
Add concrete methods:
play() to Cat and Dog
classes
14 / 45
What we need

Pet behaviors just in Pet classes (like Dog and Cat)
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
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
17 / 45
Multiple Inheritance?
Animal
Feline
Lion
Pet
Canine
Cat
Wolf
Dog
18 / 45
Multiple Inheritance?
Animal
Feline
Lion
Pet
Canine
Cat
Wolf
Dog
But Java does not allow multiple inheritance! Why?
19 / 45
Multiple Inheritance?

Not allowed

Mainly to avoid complexity/confusion.

Want to keep it simple.
20 / 45
The Diamond Problem
SuperClass
method()
A
B
method()
method()
C
21 / 45
The Diamond Problem
SuperClass
method()
A
B
method()
method()
C
What happens when method() is called in C?
22 / 45
Solution: Interfaces
Make Pet an interface and put all pet behaviors in it as abstract methods
Animal
Feline
Lion
Pet
abstract play();
Canine
Cat
Wolf
Dog
23 / 45
Interfaces

Classlike construct
• contains only constants and abstract methods.
24 / 45
Interfaces

Classlike construct
• contains only constants and abstract methods.
• cannot contain variables or concrete methods.
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;
}
26 / 45
Interface Pet
public interface Pet {
public void play();
}
27 / 45
Implementing Pet interface
class Dog extends Canine implements Pet{
public void play(){
System.out.println(“Catch ball”);
}
}
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”);
}
}
29 / 45
Interface Characteristics

Interfaces are defined in their own file
30 / 45
Interface Characteristics

Interfaces are defined in their own file

Methods are abstract. No implementations.

May omit the abstract keyword.
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.
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
33 / 45
Example
public interface Interface1 {
int aNumber = 0;
public void aMethod();
}
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();
}
35 / 45
Interface Characteristics

A class can implement any number of interfaces
public class aClass implements I1, I2, I3 {
// methods from I1, I2, I3...
}
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
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
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();
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
40 / 45
Classes from different inheritance hierarchy can implement same interface!
Robot
Animal
Feline
Pet
abstract play();
Canine
RoboDog
Lion
Cat
Wolf
Dog
Roomba
41 / 45
Abstract Classes vs. Interfaces
Abstract
class
Variables
Constructors
Methods
No restrictions
Constructors are invoked by subclasses
through constructor chaining.
No restrictions.
An abstract class cannot be instantiated
using the new operator.
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
42 / 45
Abstract Classes vs. Interfaces
Abstract
class
Variables
Constructors
Methods
No restrictions
Constructors are invoked by subclasses
through constructor chaining.
No restrictions.
An abstract class cannot be instantiated
using the new operator.
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
43 / 45
Abstract Classes vs. Interfaces
Abstract
class
Variables
Constructors
Methods
No restrictions
Constructors are invoked by subclasses
through constructor chaining.
No restrictions.
An abstract class cannot be instantiated
using the new operator.
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
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
45 / 45
Next Lecture

More on interfaces
Download