220 likes | 347 Views
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.
E N D
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Interfaces (Part I) “ As far as the customer is concerned, the interface is the product. ” --JefRaskin Course Lecture Slides9 June 2010 Ganesh Viswanathan
Animal picture food sleep() roam() makeNoise() eat() Canine roam() Feline roam() Lion makeNoise() eat() Cat makeNoise() eat() Wolf makeNoise() eat() Dog makeNoise() 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 2 • Add 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 Pet Feline Canine Lion Cat Wolf Dog But Java does not allow multiple inheritance! Why?
DigitalRecorder CDBurner burn() DVDBurner() burn() ComboDrive Multiple Inheritance Issues
Solution: Interfaces Make Pet an interface and put all pet behaviors in it as abstract methods Animal Pet abstract play(); abstract beFriendly(); Feline Canine Lion 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: • 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 Animal Pet abstract play(); abstract beFriendly(); Feline Canine RoboDog Roomba Lion Cat Wolf Dog
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
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