CIS3023: Programming Fundamentals for CIS Majors II
Summer 2010
Interfaces (Part I)
“ As far as the customer is concerned, the interface is the product. ”
--Jef Raskin
Course Lecture Slides
9 June 2010
Ganesh Viswanathan
picture
food
sleep()
roam()
makeNoise()
eat()
roam()
roam()
makeNoise()
eat()
makeNoise()
eat()
makeNoise()
eat()
makeNoise()
eat()
Problem
Need to add following methods to model Pet
behaviors to the pet animal classes
• play()
• beFriendly()
Option 1
Animal
Feline
Lion
Canine
Cat
Wolf
Dog
Add concrete methods:
play() and beFriendly() to
Animal class
Option 2
Add abstract methods: play() and
beFriendly() to Animal class
Animal
Feline
Lion
Canine
Cat
Wolf
Dog
Provide empty body for them in
Lion and Wolf classes
Provide non-empty body for
them in Cat and Dog classes
Option 3
Animal
Feline
Lion
Canine
Cat
Wolf
Dog
Add concrete methods:
play() and beFriendly()
only in Cat and Dog
classes
What do we need?
Pet behaviors just in Pet classes like Dog and Cat
A way to guarantee that all Pet classes use the
same signature for these Pet methods
A way to take advantage of Polymorphism
What do we need?
Animal
Feline
Lion
Pet
Canine
Cat
Wolf
Dog
But Java does not allow multiple inheritance! Why?
Multiple Inheritance Issues
DigitalRecorder
CDBurner
DVDBurner()
burn()
burn()
ComboDrive
Solution: Interfaces
Make Pet an interface and put all pet behaviors in it as
abstract methods
Pet
abstract play();
abstract beFriendly();
Animal
Feline
Lion
Canine
Cat
Wolf
Dog
Interfaces
An interface is a classlike construct that
• contains only constants and abstract methods.
• cannot contain variables or concrete methods.
Syntax for declaring interfaces
public interface <InterfaceName> {
// constant declarations;
// method signatures;
}
Pet Interface
public interface Pet {
public void beFriendly();
public void play();
}
Interfaces, cont.
To implement an interface, a class must:
1. include the phrase
implements <InterfaceName>
at the start of the class definition
2. Provide body for all the methods listed in the
definition of the interface or it must be
declared abstract
Implementing Pet Interface
class Dog extends Canine implements Pet{
public void beFriendly(){
System.out.println(“Wag tail”);
}
public void play(){
System.out.println(“Catch ball”);
}
}
class Cat extends Feline implements Pet{
public void beFriendly(){
System.out.println(“Curl up ”);
}
public void play(){
System.out.println(“Roll ball”);
}
}
Using the Pet Interface
class Main{
public static void main(String[] args){
Pet[] pets = new Pet[2];
pet[0] = new Dog();
pet[1] = new Cat();
for(int i = 0; i < 2; i++) pet[i].play();
}
}
Characteristics of Interfaces
• Interfaces are defined in their own file
• All methods of an interface are abstract. They do
not have implementations. You may omit the
abstract keyword.
• Interfaces do not have instance fields.
• Interfaces can have constant fields. These are
automatically treated as public, static, final; you
may omit any of these keywords
Characteristics of Interfaces, cont.
• A class can implement any number of
interfaces by providing implementations for
all the methods of all the interfaces it claims
to implement.
Example:
public class A implements I1, I2, I3 {
…
}
Characteristics of Interfaces, cont.
• Interfaces can not be instantiated
• But can be used as a data type for a variable
Classes from different inheritance hierarchy can implement
same interface!
Robot
Pet
abstract play();
abstract beFriendly();
Animal
Feline
Canine
RoboDog
Lion
Cat
Wolf
Dog
Roomba
Interfaces vs. Abstract Classes
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
Interfaces vs. Abstract Classes
• You can not inherit from multiple classes but
a class can implement multiple interfaces
• Interfaces is used to model “can do” type
relationships while Inheritance used to model
“is a” type of relationship
21
Get more info!
• Interfaces @Java-doc:
http://java.sun.com/docs/books/tutorial/java/IandI/createinterface.html
• Why use interfaces?
http://www.codestyle.org/java/faq-Interface.shtml
• Characteristics of interfaces:
http://mindprod.com/jgloss/interface.html
22
© Copyright 2025 Paperzz