Lec10_Interfaces_NestedClasses_2per

Lec10_Interfaces_NestedClasses_2per - 1 EECS EECS EECS EECS...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 EECS285 Lecture 10 Interfaces and Inner Classes Van der Linden Ch. 11-12 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 2 Andrew M. Morgan Interfaces, Motivation Java does not support multiple inheritance A subclass can have only one direct parent Would be nice to group classes together using their common functionality Some modes of transportation can be refueled Some modes of transportation can fly Some modes of transportation must drive on specified roads ModeOfTransportation Bicycle driveOnRoad() more Car refuel() driveOnRoad() more Airplane refuel() fly() more Helicopter refuel() fly() more ModeOfTransportation Bicycle more Car refuel() more Airplane refuel() more Helicopter refuel() more FlyingTransportation fly() RoadwayTransportation driveOnRoad() Grouped some common functionality together at base class level, but what about refuel(), which is shared by 3 of the 4 modes of transportation? 2 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 3 Andrew M. Morgan Interfaces, Motivation In a language that supports multiple inheritance, this would work But it can be confusing does Airplane have two sets of attributes inherited from ModeOfTransportation now? Java allows you to create interfaces that are similar to abstract classes, but really just specify functionality that can be shared by any class that implements the interface Classes can implement as many interfaces as necessary ModeOfTransportation Bicycle more Car more Airplane more Helicopter more FlyingTransportation fly() RoadwayTransportation driveOnRoad() FueledTransportation refuel() EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 4 Andrew M. Morgan Interfaces, Description Like an abstract class, the function descriptions in an interface are an interface only contain no implementation Classes that implement an interface must provide implementations for all functions in the interface are also considered of the interface type Objects can be passed to functions as an interface type Interfaces have no data members Actually, they can have members, but they are always static and final No "per-object" data members would be a better way to say it Allows for constants that relate to the interface to be stored as part of the interface 3 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 5 Andrew M. Morgan Interfaces and Implementations public class Helicopter extends ModeOfTransportation implements SkyTravelable, Fuelable { public void fly() { out.println("Helicopter fly");out....
View Full Document

This note was uploaded on 01/19/2012 for the course EECS 285 taught by Professor Idk during the Fall '08 term at University of Michigan.

Page1 / 7

Lec10_Interfaces_NestedClasses_2per - 1 EECS EECS EECS EECS...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online