1
COMP200
INTERFACES
OOP using Java, from slides by Shayan Javed
1
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
1
INTERFACES
OOP using Java, from slides by Shayan Javed
1
2
3 ANIMAL
picture food sleep() roam() makeNoise() eat()
4 ANIMAL
picture food sleep() roam() makeNoise() eat()
FELINE roam() CANINE roam()
5 ANIMAL
picture food sleep() roam() makeNoise() eat()
FELINE roam() CANINE roam() LION makeNoise() eat() CAT makeNoise() eat() WOLF makeNoise() eat() DOG makeNoise() eat()
6
Need to add Pet behaviors to the pet animal
classes (Cat and Dog):
7
Add concrete methods:
play() to Animal class
Animal Feline Canine Lion Cat Wolf Dog
8
Add concrete methods:
play() to Animal class
What’s the problem?
Animal Feline Canine Lion Cat Wolf Dog
9
Add concrete methods:
play() to Animal class
What’s the problem? Also exist for Lion/Wolf
Animal Feline Canine Lion Cat Wolf Dog
10
Add abstract methods: play() to Animal class
Animal Feline Canine Lion Cat Wolf Dog
11
Add abstract methods: play() to Animal class Provide empty body for it in Lion and Wolf classes
Animal Feline Canine Lion Cat Wolf Dog
12
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
13
Add concrete methods:
play() to Cat and Dog classes
Animal Feline Canine Lion Cat Wolf Dog
14
Pet behaviors just in Pet classes (like Dog and
Cat)
15
Pet behaviors just in Pet classes (like Dog and
Cat)
Consistency - Guarantee that all Pet classes
use the same signature for Pet methods
16
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
Animal Feline Canine Lion Cat Wolf Dog Pet
18
Animal Feline Canine Lion Cat Wolf Dog Pet But Java does not allow multiple inheritance! Why?
19
Not allowed Mainly to avoid complexity/confusion. Want to keep it simple.
20
SuperClass method() A method() B method() C
21
SuperClass method() A method() B method() C
What happens when method() is called in C?
22
Animal Feline Canine Lion Cat Wolf Dog Pet abstract play(); Make Pet an interface and put all pet behaviors in it as abstract methods
23
Classlike construct
methods.
24
Classlike construct
methods.
methods.
25
Classlike construct
Declaration:
public interface <InterfaceName> { // constant declarations; // method signatures; }
26
public interface Pet { public void play(); }
27
class Dog extends Canine implements Pet{ public void play(){ System.out.println(“Catch ball”); } }
28
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
Interfaces are defined in their own file
30
Interfaces are defined in their own file Methods are abstract. No implementations. May omit the abstract keyword.
31
Interfaces are defined in their own file Methods are abstract. No implementations. May omit the abstract keyword. Do not have instance fields.
32
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
public interface Interface1 {
int aNumber = 0; public void aMethod();
}
34
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
A class can implement any number of interfaces
public class aClass implements I1, I2, I3 { // methods from I1, I2, I3... }
36
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
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
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
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 Animal Feline Canine Lion Cat Wolf Dog Pet abstract play(); Robot RoboDog Roomba Classes from different inheritance hierarchy can implement same interface!
41
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
42
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
43
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
44
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
More on interfaces