professional documents
home
Profile
docsters
request
Blogs
Upload
Word Document

Java Interface center doc

 

Schwerpunkte 8. Grundkonzepte der Objektorientierung (5): Interfaces Java-Beispiele: ScheduleInt.java ScheduleAbstr.java UmkehrungNU.java KeyboardIApp.java Druck.java K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Version: 17. Jan. 2006 • Aufgaben des Interface • Java-Interface als Abstraktion • Implementation von Interfaces: implements • Interface: wirkt wie neuer Typ (Variablen vereinbaren u.a.) • Interface: Spezialfall abstrakter Klassen • Mit Interfaces: (eingeschränkte) Mehrfachvererbung in Java • Typische Anwendungsfälle K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 2 Java-Programm: Menge von Komponenten Interface: Überblick und Grundprinzip C3 C5 C1 C4 C1 C2 Komponente Ci: Klasse oder Interface K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 3 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 4 Komponenten in Java (zumindest syntaktisch) Java-Syntax: Komponentenarten Quelltextdatei ::= [Paketfestlegung] {Import} {Typdeklaration} zwei Arten Klasse class Interface interface wird implementiert durch (spezielle) abstrakte Klasse Typdeklaration ::= Klassendeklaration | Schnittstellendeklaration . Klassenkonzept kann Interface modellieren K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Interface benötigt Klassenkonzept 5 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 6 Beispiel: volle Form der Quelltextdatei package demo; import java.util.* class C1 ... } { Rolle von Java-'interface'-Komponenten • Inhaltlich-logische Funktion: -Trennung Interface -Implementation interface I1 ... } { • Technische Funktion: -eingeschränkte Mehrfachvererbung Definition einer Komponente C1/I1 kann voraussetzen: -package: Zuordnung zu einem Paket (Sammlung von Klassen mit ähnlichen Aufgaben) -import: Paket angegeben, aus dem Klassen verwendet werden sollen K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 7 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 8 SW-Komponenten sind Abstraktionen Komponente: Interface + Implementation Java-Klasse: Wo ist die Abstraktion? class Time { SW – "Restsystem" private int hour, minute; ... public void addMinutes (int m) { Interface Implementation ("versteckt") Schnittstelle zur "Außenwelt" Hiervon kann die "Außenwelt" abstrahieren } ... } int totalMinutes = (60*hour + minute + m) % (24 * 60); if (totalMinutes < 0) totalMinutes = totalMinutes + 24 * 60; hour = totalMinutes /60; ... Java-Klasse ist Mischung aus Interface und Implementation: Abstraktionsprinzip nicht umgesetzt -Interface -Implementation 10 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 9 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Interface eines ADT SW – "Restsystem" C++-Klasse: eine Abstraktion ? class Time public: Time Time void void ... { Konsequenz: Klassen in C++ sind bessere Abstraktionen als Klassen in Java Interface: Operationen Implementation: ("versteckt") • Art der Datendarstellung, z.B. int [] sort; • Implementation der Operationen (Algorithmen) K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Signatur aller Operationen des ADT (); (int h, int m); addMinutes (int m); printTime (); private: int hour, minute; Hiervon kann die "Aussenwelt" abstrahieren } Aber: Java-Interface überwindet diesen Mangel -Interface 11 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 -Implementation 12 Java–Interface: eine (fast) "saubere" Abstraktion interface public public public public } du l eI TimeI { nt. jav void addMinutes (int m); a void substractMinutes (int m); void printTime (); void printTimeInMinutes (); Sc he Interface in Java: -Definition -Implementation -Anwendung -Vergleich mit abstrakten Klassen • Nur die Köpfe der Methoden • Keine Daten + keine Algorithmen • Was fehlt? Konstruktoren (im Interface nicht erlaubt): TimeI(int h, int m); TimeI(); K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 13 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 14 Implementation eines Interface durch Klassen Nutzer interface public public public public } Anwendung eines Interface interface public public public public } TimeI { void addMinute (int m); void substractMinutes (int m); void printTime (); void printTimeInMinutes (); class Time implements TimeI { private int hour, minute; public Time (int h, int m) public void addMinutes (int m) ... } Sc TimeI { he du void addMinute (int m); l eI void substractMinutes (int m); nt. jav void printTime (); a void printTimeInMinutes (); class Time implements TimeI { private int hour, minute; Nutzer muss auch von implementierender Klasse kennen: Name, Konstruktoren Interface wie neuer (nutzerdefinierter) Typ Sc he du public Time (int h, int m) {...} public void addMinutes (int m) { int totalMinutes = (60*hour + minute + m) % (24 * 60); if (totalMinutes < 0) totalMinutes = totalMinutes + 24 * 60; hour = totalMinutes /60; Hinzugefügt: ... • Lokale Variablen } • Konstruktor ... } Variablen vom Interface-Typ 'TimeI' public static void main (...) { TimeI t1; t1 = new Time(8,30); t1.addMinutes(30); l eI nt . ja va • Körper der Methoden 15 Erlaubte Werte: Objekte von ALLEN } implementierenden Klassen (Kompatibilitätsregel) Konstruktor identifiziert ausgewählte implementierende Klasse 16 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Interface: Spezialfall abstrakter Klassen abstract class TimeI { public abstract void addMinutes (int m); public abstract void substractMinutes (int m); public abstract void printTime (); public abstract void printTimeInMinutes(); } class Time extends TimeI { private int hour, minute; public void addMinutes (int m); ... } ... } Rolle von Java-'interface'-Komponenten • Inhaltlich-logische Funktion r.jav Trennung Interface -Implementation Sche d ul e Abst a public static void main (...) { TimeI t1; t1 = new Time(8,30); ... } Nutzerkomponente Als eine logische Einheit auffassen: interface TimeI (...) class Time implements TimeI{...} { Interface und abstrakte Klasse: logisch identisch – technische Unterschiede K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 17 • Technische Funktion: -eingeschränkte Mehrfachvererbung 18 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Interface: eingeschränkte Mehrfachvererbung für Klassen verboten – für Interface erlaubt: Komponente 1 Komponente 2 Vererbung Mehrfachvererbung in Java durch Interface-Komponenten Komponente 3 (abstract) class K1 ... falsch (abstract) class K2 ... class K3 extends K1, K2 ... interface I1 ... ok interface I2 ... class K3 implements I1, I2 ... K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 19 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 20 Beispiel: Hierarchie mit Mehrfachvererbung (1) class Fahrzeug class Sammlerstueck Beispiel: Hierarchie mit Mehrfachvererbung (2) interface Fahrzeug interface Sammlerstueck class Auto class falsch class Oldtimer class Flugzeug Auto OK class Oldtimer class Flugzeug K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 21 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 22 Beispiel: Vererbungshierarchie in Java interface Fahrzeug ... interface Sammlerstueck ... class Auto implements Fahrzeug ... Erbt alle Elemente (Variablen, Methoden) von 'Auto' und 'Sammlerstueck' Beispiel: volle Form (nach: Krüger: Go To Java 2) interface Fahrzeug { public int kapazitaet(); public double verbrauch(); } interface Sammlerstueck { public double wert(); public boolean ausgestellt(); } class Oldtimer extends Auto implements Sammlerstueck Erbt alle Elemente von 'Fahrzeug' und Sammlerstueck' class Auto implements Fahrzeug { public String name; private int anzSitze; private double spritVerbrauch; public int kapazitaet() { return anzSitze; } public double verbrauch() { return spritVerbrauch;} } class Oldtimer extends Auto implements Sammlerstueck { private double sammlerWert; private boolean ausst; public double wert () { return sammlerWert; } public ausgestellt () { return ausst; } } class Flugzeug implements Fahrzeug, Sammlerstueck class Flugzeug implements Fahrzeug, Sammlerstueck ... 23 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 24 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Interface: Welche Komponentenart modelliert? Vgl. III.4 Komponentenarten interface public public static } Fahrzeug { int kapazitaet(); double verbrauch(); final int anzahl = 100000; Interface: modellierbare Komponentenart viele semantisch sehr unterschiedliche Komponenten a r t e n sinnlose Konstruktionen Festlegung (gilt auch implizit für Interface): Methoden: public, abstract, non-static Variablen: public, static, final imperativ FunktionsKonstantensammlung sammlung andere Datenabstraktion objektorientiert ADT Datensammlung ADT mit Konstanten ADT mit Klassenelementen 26 ADT mit Konstanten K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 25 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Interface: nicht modellierbare Komponentenarten viele semantisch sehr unterschiedliche Komponenten a r t e n Java-Interface: Methoden mit Interface-Parametern interface TimeI { public void addMinutes (int m); public void printTime (); ... pulic boolean before (TimeI t); } Dieselbe Signatur: Interface-Typ auch in Klasse class Time implements TimeI { public boolean before ( TimeI t1 ) { Time t = (Time) t1; return ((hour < t.hour) || ...) } Nicht für jede sinnlose Komponentenart Konstruktionen kann ein Interface angeboten werden imperativ FunktionsKonstantensammlung sammlung h Datenabstraktion bo a K ey rd objektorientiert ADT Datensammlung ADT mit Konstanten ADT mit Klassenelementen Ti m eC 27 Typ-Transformation: Parameter hat Interface-Typ allgemeiner Typ (Interface) spezieller Typ ss cla t Ma g. andere an .l va ja Cast-Operation kann Laufzeitfehler erzeugen: aktueller Parameter einer anderen implementierenden Klasse übergeben K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 ClassCastException 28 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Interface: Anwendungsfälle • E i n Interface eines ADT -z w e i (mehrere) Implementationen: -UmkehrungNU.java: beschränkt und unbeschränkt Interface: wichtige Anwendungen • Interface für Datenabstraktionen: -KeyboardIApp.java • Import gemeinsamer Konstanten von Klassen • Algorithmen mit 'Funktionsparametern': -Druck.java: Druck beliebiger Funktionen • Java-API: List – Set – Collection ... K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 29 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 30 Java-API: Vielzahl von Interfaces Interface Serializable Cloneable Collection Set Map extends Interface: Anwendungsfälle • E i n Interface eines ADT -z w e i (mehrere) Implementationen: -UmkehrungNU.java: beschränkt und unbeschränkt List SortedSet • Interface für Datenabstraktionen: implements AbstractList Vector Klassen -KeyboardIApp.java HashTable LinkedList TreeSet • Import gemeinsamer Konstanten von Klassen • Algorithmen mit 'Funktionsparametern': -Druck.java: Druck beliebiger Funktionen extends Stack public class Vector extends AbstractList implements List, Cloneable, Serializable K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Mehrfachvererbung durch Vielzahl von Interfaces im Java-API 31 • Java-API: List – Set – Collection ... K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 32 E i n Interface eines ADT – zwei (mehrere) Implementationen: Um interface Stack { ke hr public boolean isempty(); un gN public void push(char x); U. j av public char top(); a pulic void pop(); } class StackN implements Stack { private char[] stackElements; private int top; ... beschränkte Größe } class StackU implements Stack private class Zelle {...} private Zelle top; ... } { unbeschränkt Anwendung: nur Interface bekannt + Konstruktor interface Stack { public boolean isempty(); public void push(char x); public char top(); pulic void pop(); } Anwendung: Um ke h rungNU .java Vorteil: Gute Modifizierbarkeit, falls die Anwendung nur mit den Informationen aus dem Interface arbeitet Besonderheit von UmkehrungNU.java: Erst dynamisch wird die implementierende Klasse ausgewählt dynamische Bindung von Methoden zur Laufzeit Auswechseln der Implementation: Nur der Konstruktor ist auszuwechseln Stack s; s = new StackN(n); oder Algorithmus korrekt für beliebige Implementationen: -Begrenzte Stacks -Unbegrenzte Stacks 33 s = new StackU(); while (!s.isempty()) { System.out.print(s.top()); s.pop(); } Methoden aus dem Interface 34 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Interface: Anwendungsfälle • E i n Interface eines ADT -z w e i (mehrere) Implementationen: -UmkehrungNU.java: beschränkt und unbeschränkt Klasse 'Keyboard`: eine Datenabstraktion import java.io.*; class Keyboard { ////////Author: M. Dennis Mickunas, June 9, 1997 Primitive Keyboard input of integers, reals, strings, and characters. boolean iseof = false; char c; int i; double d; String s; public static int readInt () { if (iseof) return 0; System.out.flush(); try { s = input.readLine(); } catch (IOException e) { System.exit(-1); } if (s==null) { iseof=true; return 0; } i = new Integer(s.trim()).intValue(); return i; } public static char readChar () { if (iseof) return (char)0; System.out.flush(); ... • Interface für Datenabstraktionen: -KeyboardIApp.java • Import gemeinsamer Konstanten von Klassen • Algorithmen mit 'Funktionsparametern': -Druck.java: Druck beliebiger Funktionen static static static static static /* WARNING: THE BUFFER VALUE IS SET TO 1 HERE TO OVERCOME ** A KNOWN BUG IN WIN95 (WITH JDK 1.1.3 ONWARDS)*/static BufferedReader input Klasse realisiert = new BufferedReader ( Abstraktionsprinzip nicht: new Kein Interface für Nutzer InputStreamReader(System.in),1); • Java-API: List – Set – Collection ... K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 35 erkennbar K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Realisierung als Interface möglich? 36 Interface: modellierbare Komponentenart viele semantisch sehr unterschiedliche Komponenten a r t e n Interface für Datenabstraktionen Interface für den Nutzer interface public public public public public } sinnlose Konstruktionen Datenabstraktion nicht durch ‘interface‘ realisierbar ADT yb KeyboardI { oa r dI int readInt(); Ap pl. char readChar(); j av double readDouble(); a String readString(); boolean eof(); jetzt: Instanzmethoden ADT Ke imperativ FunktionsKonstantensammlung sammlung andere Datenabstraktion bo a K ey K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 objektorientiert ADT Datensammlung ADT mit Konstanten ADT mit Klassenelementen 37 Unwichtige Implementationsdetails Anwendung: technisch andere Realisierung Objekt erzeugt class Keyboard implements KeyboardI //110 Zeilen Implementation } { Keyboard kb = new Keyboard(); jn = kb.readChar(); n = kb.readInt(); vgl. Keyboard.readChar() 38 rd K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Interface: Anwendungsfälle • E i n Interface eines ADT -z w e i (mehrere) Implementationen: -UmkehrungNU.java: beschränkt und unbeschränkt Import gemeinsamer Konstanten Konstanten: Steuerzeichen interface Steuerzeichen { public static final char BS = ’\b’, HT = ’\t’, LF = ’\n’, FF = ’\f’, CR = ’\r’; } 'implements' – ein adäquater Konstanten verfügbar in Ausgabe1 Begriff? /Ausgabe2 class Ausgabe1 implements Steuerzeichen //Methoden der Ausgabe 1 } class Ausgabe2 implements Steuerzeichen //Methoden der Ausgabe 2 } { • Interface für Datenabstraktionen: -KeyboardIApp.java • Import gemeinsamer Konstanten von Klassen • Algorithmen mit 'Funktionsparametern': -Druck.java: Druck beliebiger Funktionen { • Java-API: List – Set – Collection ... K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 39 dieselbe Technik: include-Files in C /C++ K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 40 Interface: Anwendungsfälle • E i n Interface eines ADT -z w e i (mehrere) Implementationen: -UmkehrungNU.java: beschränkt und unbeschränkt Algorithmen mit 'Funktionsparametern' Problem: • Algorithmen: arbeiten mit (beliebigen) mathematischen Funktionen • Funktion ist Parameter des Algorithmus -Pascal: Funktionsparamter (spezielle Parameterart) function druckeKurve (x0: real; x1: real; delta: real; function F(x: real): real ) ... Aufruf: • Interface für Datenabstraktionen: -KeyboardIApp.java • Import gemeinsamer Konstanten von Klassen • Algorithmen mit 'Funktionsparametern': -Druck.java: Druck beliebiger Funktionen druckeKurve(0.5, 1.5, 0.1, sin); druckeKurve(0.5, 1.5, 0.1, cos); -C, C++: Zeiger auf Funktionen als Parameter -Java: Interface oder abstrakte Klassen *) *) Funktionsparameter exisitieren nicht Parameter: elementare Typen oder Objekte Funktion in Interface "verpacken" (Wrapper für Funktion) 41 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 42 • Java-API: List – Set – Collection ... K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Beispiel: Algorithmen mit 'Funktionsparametern' (1) Dru interface Function { double apply (double x); } Beispiel: Algorithmen mit 'Funktionsparametern' (2) interface Function { double apply (double x); } ck.j ava Dr uc allgemeinstes Schema einer einstelligen reellwertigen Funktion { k.j av a Nutzer: arbeitet nur mit allgemeinem Schema (interface) static void druckeKurve (double x0, double x1, double delta, Function f) System.out.print( f.apply(x0) ); ... } Anwendung: konkrete Funktionen 'sin', 'cos' class SinFunction implements Function public double apply (double x) { return Math.sin(x); } } 2 Spezialisierungen: Sinus, Cosinus class CosFunction implements Function public double apply (double x) { return Math.cos(x); } } K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 { { public static void main (...) { SinFunction sinus = new SinFunction(); CosFunction cosinus = new CosFunction(); druckeKurve(0, Math.PI/2, 0.1, sinus); druckeKurve(0, Math.PI/2, 0.1, cosinus); } 43 K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 Technisch anders als in Pascal, C, ...: Hier wird Objekt übergeben, das passende Methode umfasst (Wrapper) 44 Druck aller Funktionswerte von x0 bis x1 (Verbesserung von Druck.java) static void druckeKurve (double x0, double x1, double delta, Function f ) for (int x = x0; x<= x1; x = x+delta) System.out.print( f.apply(x) ); ... } { K. Bothe, Inst. f. Informatik, HU Berlin, PI1, WS 2006/07 45
rate this doc
email this doc
embed this doc
add to folder
digg reddit stumble delicious
flag this doc
264
2
not rated
0
9/14/2007
english
search termpage on Googletimes searched
Preview

Interface driven Model based Generation of Java Test Drivers

NIST 7/2/2008 | 10 | 0 | 0 | legal
Preview

copy as interface

Larry76 5/21/2008 | 24 | 0 | 0 | technology
Preview

design patterns

arnoldsayz 7/13/2008 | 28 | 3 | 0 |
Preview

Java

shanti12 1/10/2008 | 264 | 14 | 0 |
Preview

JAVA

shanti12 1/10/2008 | 155 | 8 | 0 |
Preview

Java

Semaj1212 4/23/2008 | 21 | 0 | 0 | technology
Preview

Java Intermediate 2

anonymous 11/12/2007 | 206 | 17 | 0 | educational
Preview

Java emanufacturing

Jharan 5/24/2008 | 34 | 0 | 0 | technology
Preview

MANUAL DE JAVA

arcenal 12/22/2007 | 1261 | 78 | 0 | educational
Preview

brief history of java

BeunaventuraLongjas 7/23/2008 | 7 | 0 | 0 | technology
Preview

Java Collections

anonymous 11/12/2007 | 177 | 24 | 0 | educational
Preview

Assessing java clients with the beanshell

Jharan 5/24/2008 | 21 | 0 | 0 | technology
Preview

Remote Method Invocation

ziaul 9/24/2007 | 210 | 15 | 0 | technology
Preview

Java Interview

shanti12 1/10/2008 | 457 | 44 | 0 |
Preview

java starter

shanti12 1/10/2008 | 133 | 7 | 0 |
interface konstruktor13
java interface abstract12
java interface konstruktor12
java interface übergeben12
class-oldtimer11
java interface methoden körper11
abstract un interface in java11
implements java interface31
interface or abstract class11
java interface konstanten11
java interface abstract class31
public interfacein java11
java interface vererbung31
java private interface implementation11
java interface konstruktor11
interface typ transformation11
"implements list" in java11
usb java interface11
java interface beispiel61
konstruktor interface11
 
review this doc