8: Interfaces Interfaces “Multiple inheritance” in Java –Name collisions when combining interfaces Extending an interface with inheritance Grouping constants.

Slides:



Advertisements
Similar presentations
Chapter 8: Interfaces and inner classes ● We learnt a number of mechanisms to abstract concepts and reuse them: – Data abstraction (Encapsulation and Information.
Advertisements

Object Oriented Programming
TÉCNICAS DE PROGRAMACIÓN Lenguaje Java Java – 3 Daniel Finol.
Chapter 7: Polymorphism ● Polymorphism is the thrid essential feature of OOP – You know the other two already: Data Abstraction and Inheritance ● It helps.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Line Efficiency     Percentage Month Today’s Date
Chubaka Producciones Presenta :.
Jan Object Oriented Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
HOW TO MAKE A CLIMATE GRAPH CLIMATE GRAPHING ASSIGNMENT PT.2.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
In-class exercise 1. 1.downloading Java Java SE Java SE (JDK) 6 JDK 6u2 jdk-6u2-windows-i568-p.exe.
Object-Oriented Design Running Time Recursion - Ed. 2 and 3.: Chapter 2, 3 - Ed. 4: Chapter 2, 3, 4.
Java Interfaces Overview Java Interfaces: A Definition.
1 Inner Classes Overview  Java Inner Classes: A Definition.  Overview of Nested Classes.  Top level Nested Classes.  Non-static Inner Classes.  Non-static.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar.
1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Grouping Related Items
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
Date processing and conversion Please use speaker notes for additional information!
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Interfaces and Inner Classes
Interfaces and Polymorphism CS 162 (Summer 2009).
11 Implementing Abstract Classes. 2 Contents Define Abstract Class Rules on Abstract Class Define Interface Implementing Interface Uses of Interface Abstract.
14 BirthMonth1February BirthMonth CE : Fundamental Programming Techniques.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
7: Polymorphism Upcasting revisited Forgetting the object type
Jan 2016 Solar Lunar Data.
Baltimore.
solve the following problem...
Implementing Abstract Classes
CMSC 202 Static Methods.
CMSC 202 Interfaces.
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Irregularities in DoRIS
CSC 113: Computer programming II
FY 2019 Close Schedule Bi-Weekly Payroll governs close schedule
2009 TIMELINE PROJECT PLANNING 12 Months Example text Jan Feb March
CMSC 202 Interfaces.
McDonald’s calendar 2007.
A Climate Study of Daily Temperature Change From the Previous Day
Classes and Objects Static Methods
Irregularities in DoRIS
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
McDonald’s calendar 2007.
CMSC 202 Interfaces.
2009 TIMELINE PROJECT PLANNING 12 Months Example text Jan Feb March
CS 240 – Advanced Programming Concepts
2015 January February March April May June July August September
Presentation transcript:

8: Interfaces Interfaces “Multiple inheritance” in Java –Name collisions when combining interfaces Extending an interface with inheritance Grouping constants Initializing fields in interfaces Exercises

Interfaces interface =a “ pure ” abstract class. –All the methods are abstract ( with names, argument lists, and return types, but without method bodies). –methods implicitly public and abstract. –Fields: implicitly static and final. To create an interface, –use the interface keyword instead of the class keyword. –Like a class, an interface can be defined as public (add the public keyword before the interface keyword ) –or leave it off to “ friendly ” To make a class that conforms to a particular interface (or group of interfaces) use the implements keyword. The interface is more than just an abstract class taken to the extreme, since it allows you to perform a variation on C++ ’ s “ multiple inheritance

Interfaces import java.util.*; interface Instrument { // Compile-time constant: int i = 5; // static & final // Cannot have method definitions: void play(); // Automatically public String what(); void adjust(); } class Wind implements Instrument { public void play() { System.out.println("Wind.play()"); } public String what() { return "Wind"; } public void adjust() {} } class Percussion implements Instrument { ...... }

class Stringed implements Instrument { ......} class Brass extends Wind { public void play() { System.out.println("Brass.play()"); } public void adjust() { System.out.println("Brass.adjust()"); } } class Woodwind extends Wind { ......} public class Music5 { // Doesn't care about type, so new types // added to the system still work right: static void tune(Instrument i) { i.play(); } static void tuneAll(Instrument[] e) { for(int i = 0; i < e.length; i++) tune(e[i]); } public static void main(String[] args) { Instrument[] orchestra = new Instrument[5]; int i = 0; // Upcasting during addition to the array: orchestra[i++] = new Wind(); orchestra[i++] = new Percussion(); orchestra[i++] = new Stringed(); orchestra[i++] = new Brass(); orchestra[i++] = new Woodwind(); tuneAll(orchestra); } } ///:~

“Multiple inheritance” in Java The interface isn ’ t simply a “ more pure ” form of abstract class. It has a higher purpose than that. Because an interface has no implementation at all — that is, there is no storage associated with an interface, so the problems seen in C++ do not occur with Java when combining multiple interfaces.

import java.util.*; interface CanFight { void fight(); } interface CanSwim { void swim(); } interface CanFly { void fly(); } class ActionCharacter { public void fight() {} } class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly { public void swim() {} public void fly() {} } public class Adventure { static void t(CanFight x) { x.fight(); } static void u(CanSwim x) { x.swim(); } static void v(CanFly x) { x.fly(); } static void w(ActionCharacter x) { x.fight(); } public static void main(String[] args) { Hero h = new Hero(); t(h); // Treat it as a CanFight u(h); // Treat it as a CanSwim v(h); // Treat it as a CanFly w(h); // Treat it as an ActionCharacter // CanFight c=h; // ActionCharacter a=h; CanFight c=a; } } ///:~

Extending an interface with inheritance interface Monster { void menace(); } interface DangerousMonster extends Monster { void destroy(); } interface Lethal { void kill(); } class DragonZilla implements DangerousMonster { public void menace() {} public void destroy() {} } interface Vampire extends DangerousMonster, Lethal { void drinkBlood(); } // how to implement Vampire? public class HorrorShow { static void u(Monster b) { b.menace(); } static void v(DangerousMonster d) { d.menace(); d.destroy(); } public static void main(String[] args) { DragonZilla if2 = new DragonZilla(); u(if2); v(if2); } } ///:~

Name collisions when combining interfaces interface I1 { void f(); } interface I2 { int f(int i); } interface I3 { int f(); } class C { public int f() { return 1; } } class C2 implements I1, I2 { public void f() {} public int f(int i) { return 1; } // overloaded } class C3 extends C implements I2 { public int f(int i) { return 1; } // overloaded } class C4 extends C implements I3 { // Identical, no problem: public int f() { return 1; } } // Methods differ only by return type: //! class C5 extends C implements I1 {} //! interface I4 extends I1, I3 {} ///:~

Grouping constants Because any fields in an interface are automatically static and final, the interface is a convenient tool for creating groups of constant values, as you would with an enum in C or C++. For example: package c08; public interface Months { int JANUARY = 1, FEBRUARY = 2, MARCH = 3, APRIL = 4, MAY = 5, JUNE = 6, JULY = 7, AUGUST = 8, SEPTEMBER = 9, OCTOBER = 10, NOVEMBER = 11, DECEMBER = 12; } ///:~ Now you can reference the values with expressions like Months.JANUARY. Of course, what you get is just an int, so there isn ’ t the extra type safety that C++ ’ s enum has. But this (commonly used) technique is certainly an improvement over hard-coding numbers into your programs. (That approach is often referred to as using “ magic numbers ” and it produces very difficult-to-maintain code.)

public final class Month2 { private String name; private int order; private Month2(int ord, String nm) { order = ord; name = nm; } public String toString() { return name; } public final static Month2 JAN = new Month2(1, "January"), FEB = new Month2(2, "February"), MAR = new Month2(3, "March"), APR = new Month2(4, "April"), MAY = new Month2(5, "May"), JUN = new Month2(6, "June"), JUL = new Month2(7, "July"), AUG = new Month2(8, "August"), SEP = new Month2(9, "September"), OCT = new Month2(10, "October"), NOV = new Month2(11, "November"), DEC = new Month2(12, "December"); public final static Month2[] month = { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; public final static Month2 number(int ord) { return month[ord - 1]; } public static void main(String[] args) { Month2 m = Month2.JAN; System.out.println(m); m = Month2.number(12); System.out.println(m); System.out.println(m == Month2.DEC); System.out.println(m.equals(Month2.DEC)) ; }

System.out.println print(boolean) 打印一个布尔值。 print(char) 打印一个字符。 print(char[]) 打印一个字符数组。 print(double) 打印一个双精度浮点数。 print(float) 打印一个单精度浮点数。 print(int) 打印一个整型数。 print(long) 打印一个长整型数。 print(Object) 打印一个对象。 print(String) 打印一个串。 println() 结束行。 println(boolean) 打印一个布尔值后结束此行。 println(char) 打印一个字符后结束此行。 println(char[]) 打印一个字符数组后结束此行。 println(double) 打印一个双精度浮点数后结束此行。 println(float) 打印一个单精度浮点数后结束此行。 println(int) 打印一个整数,结束此行。 println(long) 打印一个长整数,结束此行。 println(Object) 打印一个对象,结束此行。 println(String) 打印一个串,结束此行。

Initializing fields in interfaces Fields defined in interfaces are automatically static and final. These cannot be “blank finals,” but they can be initialized with nonconstant expressions. For example: //: c08:RandVals.java // Initializing interface fields with // non-constant initializers. import java.util.*; public interface RandVals { Random rand = new Random(); int randomInt = rand.nextInt(10); long randomLong = rand.nextLong() * 10; float randomFloat = rand.nextLong() * 10; double randomDouble = rand.nextDouble() * 10; } ///:~

Exercises 5.Create three interfaces, each owning two methods. Inherit a new interface from these three interface, adding a new method. Create a class by implementing the new interface and also inheriting from a concrete class. Now write four methods, each of which takes one of the four interfaces as its argument(just like Page320 Adventure.java). In main( ), create an object of your class and pass it to each of the methods.

Inner classes public class Parcel1 { class Contents { private int i = 11; public int value() { return i; } } class Destination { private String label; Destination(String whereTo) { label = whereTo; } String readLabel() { return label; } } // Using inner classes looks just like // using any other class, within Parcel1: public void ship(String dest) { Contents c = new Contents(); Destination d = new Destination(dest); System.out.println(d.readLabel()); } public static void main(String[] args) { Parcel1 p = new Parcel1(); p.ship("Tanzania"); } } ///:~

RTTI import java.util.*; class Useful { void p(String s) {System.out.println(s); } public void f() { p("U.f");} public void g() { p("U.g"); } } class MoreUseful extends Useful { public void f() {p("M.f");} public void g() {p("M.g");} public void u() {p("M.u");} public void v() {p("M.v");} public void w() {p("M.w");} } public class RTTI { public static void main(String[] args) { Useful[] x = { new Useful(), new MoreUseful() }; x[0].f(); x[1].g(); ((MoreUseful)x[1]).u(); // Exception thrown ((MoreUseful)x[0]).u(); System.out.println("Terminate normally!"); } } ///:~

RTTI import java.util.*; class Useful { void p(String s) {System.out.println(s); } public void f() { p("U.f");} public void g() { p("U.g"); } } class MoreUseful extends Useful { public void f() {p("M.f");} public void g() {p("M.g");} public void u() {p("M.u");} public void v() {p("M.v");} public void w() {p("M.w");} } public class RTTI { public static void main(String[] args) { Useful[] x = { new Useful(), new MoreUseful() }; x[0].f(); x[1].g(); if ( x[1] instanceof MoreUseful ) ((MoreUseful)x[1]).u(); if ( x[0] instanceof MoreUseful ) ((MoreUseful)x[0]).u(); System.out.println("Terminate normally!"); } } ///:~