CIS3023: Programming Fundamentals for CIS Majors II Summer 2010
description
Transcript of CIS3023: Programming Fundamentals for CIS Majors II Summer 2010
CIS3023: Programming Fundamentals for CIS Majors IISummer 2010
Ganesh Viswanathan
Interfaces (Part I)
Course Lecture Slides9 June 2010
“ As far as the customer is concerned, the interface is the product. ” --Jef Raskin
ANIMALpicturefoodsleep()roam()makeNoise()eat()
FELINE
roam()
CANINE
roam()
LIONmakeNoise()eat()
CATmakeNoise()eat()
WOLFmakeNoise()eat()
DOGmakeNoise()eat()
Problem
Need to add following methods to model Pet behaviors to the pet animal classes
• play()• beFriendly()
Option 1
Add concrete methods: play() and beFriendly() to Animal class
Animal
Feline Canine
Lion Cat Wolf Dog
Option 2Add abstract methods: play() and
beFriendly() to Animal class
Provide empty body for them in Lion and Wolf classes
Provide non-empty body for them in Cat and Dog classes
Animal
Feline Canine
Lion Cat Wolf Dog
Option 3
Add concrete methods: play() and beFriendly() only in Cat and Dog classes
Animal
Feline Canine
Lion Cat Wolf Dog
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 Canine
Lion Cat Wolf Dog
Pet
But Java does not allow multiple inheritance! Why?
Multiple Inheritance Issues
DigitalRecorder
CDBurner
burn()DVDBurner()
burn()
ComboDrive
Solution: Interfaces
Animal
Feline Canine
Lion Cat Wolf Dog
Petabstract play();abstract beFriendly();
Make Pet an interface and put all pet behaviors in it as abstract methods
InterfacesAn 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 Interfaceclass 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”);
} }
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();
}}
Using the Pet Interface
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
Animal
Feline Canine
Lion Cat Wolf Dog
Petabstract play();abstract beFriendly();
Robot
RoboDog Roomba
Classes from different inheritance hierarchy can implement same interface!
Interfaces vs. Abstract Classes
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
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