Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Aphorismen
Applications
Business Economics & Admin.
My Computers
Cooking
Devices
Folders
Food
Hardware
Infos
Software Development
Sports
Operation Instructions
Todos
Test
Help
Glossary
Community portal
adaptions
Sidebar anpassen
Wiki RB4
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
JavaLanguage
(section)
Page
Discussion
English
Read
Edit
View history
Toolbox
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Interfaces=== An interface defines a protocol of behavior. A Java interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior. A class that implements an interface adheres to the protocol defined by that interface. To declare a class that implements an interface, include an implements clause in the class declaration. Your class can implement more than one interface (Java supports multiple interface inheritance), so the implements keyword is followed by a comma-delimited list of the interfaces implemented by the class. '''class''' <ClassName> '''implements''' <Interfaces> { ... } When a class declares that it implements an interface, it's basically signing a contract to the effect that it will provide at least an empty implementations for all of the methods in the interface. An interface is defined by ['''public'''] '''interface''' <InterfaceName> ['''extends''' <SuperInterfaces>] { ['''public abstract'''] <ReturnType> <MethodName>'''('''<ParameterList>''');''' ... ['''public static final'''] <Type> <ConstantName> '''=''' <Value>'''; // '''constants } Der default modifier einer Methode ist public. Es ist ein Fehler wenn Interfaces sich selbst direkt oder indirekt extenden. A method declaration in an interface body may not include any of the modifiers final, native, static, or synchronized. Interfaces können Attribute definieren, die aber immer implicit statisch und final sind und explizit initialisiert werden müssen. Interfaces können auch als Parameter übergeben werden oder als Typ einer lokalen Variablen verwendet werden. Eine andere Verwendung ist folgende: <Interface> <Variable> '''= new''' <ClassWithInterface>'''();''' Interface with no methods are called marker interfaces, because they mark that the class capable of doing something. At this point, many programmers wonder how an interface differs from an abstract class. An interface is simply a list of unimplemented, and therefore abstract, methods. Wouldn't the following Sleeper class do the same thing as the Sleeper interface? abstract class Sleeper { public abstract void wakeUp();} No. The two are not equivalent. If Sleeper is an abstract class, then all objects that wish to use AlarmClock must be instances of a class inherited from Sleeper. However, many objects that wish to use AlarmClock already have a superclass. For example, the GUIClock is an Applet; it must be an applet to run inside a browser. But Java doesn't support multiple inheritance. So GUIClock can't be both a Sleeper and an Applet. Hence, you use an interface instead. This is the practical explanation of the problem. The conceptual explanation is this: AlarmClock should not force a class relationship on its users. It doesn't matter what their class is. It simply matters that they implement a specific method. Often interfaces are touted as an alternative to multiple class inheritance. While interfaces may solve similar problems, interface and multiple class inheritance are quite different animals, in particular: A class inherits only constants from an interface. A class cannot inherit method implementations from an interface. The interface hierarchy is independent of the class hierarchy. Classes that implement the same interface may or may not be related through the class hierarchy. This is not true for multiple inheritance. Yet, Java does allow multiple interface inheritance. That is, an interface can have multiple ''superinterfaces''. You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following: Capturing similarities between unrelated classes without artificially forcing a class relationship Declaring methods that one or more classes are expected to implement Revealing an object's programming interface without revealing its class. (Objects such as these are called ''anonymous objects'' and can be useful when shipping a package of classes to other developers.)
Summary:
Please note that all contributions to Wiki RB4 may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Uwe Heuer Wiki New:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width