CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
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 - there's nothing to prevent many interfaces from being combined. This is valuable because there are times when you need to say "An x is an a and a b and a c." In C++, this act of combining multiple class interfaces is called multiple inheritance, and it carries some rather sticky baggage because each class can have an implementation. In Java, you can perform the same act, but only one of the classes can have an implementation, so the problems seen in C++ do not occur with Java when combining multiple interfaces:
In a derived class, you aren't forced to have a base class that is either an abstract or "concrete" (one with no abstract methods). If you do inherit from a non-interface, you can inherit from only one. All the rest of the base elements
must be interfaces. You place all
the interface names after the implements
keyword and separate them with commas. You can have as many interfaces as you want and each one
becomes an independent type that you can upcast to. The following example shows
a concrete class combined with several interfaces
to produce a new class:
//: Adventure.java
// Multiple interfaces
import java.util.*;
interface CanFight
interface CanSwim
interface CanFly
class ActionCharacter
}
class Hero extends ActionCharacter
implements CanFight, CanSwim, CanFly
public void fly()
}
public class Adventure
static void u(CanSwim x)
static void v(CanFly x)
static void w(ActionCharacter x)
public static void main(String[] args)
} ///:~
You can see that Hero combines the concrete class ActionCharacter with the interfaces CanFight, CanSwim, and CanFly. When you combine a concrete class with interfaces this way, the concrete class must come first, then the interfaces. (The compiler gives an error otherwise.)
Note that the signature for fight( ) is the same in the interface CanFight and the class ActionCharacter, and that fight( ) is not provided with a definition in Hero. The rule for an interface is that you can inherit from it (as you will see shortly), but then you've got another interface. If you want to create an object of the new type, it must be a class with all definitions provided. Even though Hero does not explicitly provide a definition for fight( ), the definition comes along with ActionCharacter so it is automatically provided and it's possible to create objects of Hero.
In class Adventure, you can see that there are four methods that take as arguments the various interfaces and the concrete class. When a Hero object is created, it can be passed to any of these methods, which means it is being upcast to each interface in turn. Because of the way interfaces are designed in Java, this works without a hitch and without any particular effort on the part of the programmer.
Keep in mind that the core reason for interfaces is shown in the above example: to be able to upcast to more than one base type. However, a second reason for using interfaces is the same as using an abstract base class: to prevent the client programmer from making an object of this class and to establish that it is only an interface. This brings up a question: Should you use an interface or an abstract class? An interface gives you the benefits of an abstract class and the benefits of an interface, so if it's possible to create your base class without any method definitions or member variables you should always prefer interfaces to abstract classes. In fact, if you know something is going to be a base class, your first choice should be to make it an interface, and only if you're forced to have method definitions or member variables should you change to an abstract class.
You can easily add new method declarations to an interface using inheritance, and you can also combine several interfaces into a new interface with inheritance. In both cases you get a new interface, as seen in this example:
//: HorrorShow.java
// Extending an interface with inheritance
interface Monster
interface DangerousMonster extends Monster
interface Lethal
class DragonZilla implements DangerousMonster {
public void menace()
public void destroy()
}
interface Vampire
extends DangerousMonster, Lethal
class HorrorShow
static void v(DangerousMonster d)
public static void main(String[] args)
} ///:~
DangerousMonster is a simple extension to Monster that produces a new interface. This is implemented in DragonZilla.
The syntax used in Vampire works only when inheriting interfaces. Normally, you can use extends with only a single class, but since an interface can be made from multiple other interfaces, extends can refer to multiple base interfaces when building a new interface. As you can see, the interface names are simply separated with commas.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 618
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved