Interfaces can include
However, if the property or indexer uses explicit implementation, the accessors must match. For more information about explicit implementation, see Explicit Interface Implementation and Interface Properties.
Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces. That class may be implicitly converted to the derived interface or any of its base interfaces.
A class might include an interface multiple times through base classes that it inherits or through interfaces that other interfaces inherit. However, the class can provide an implementation of an interface only one time and only if the class declares the interface as part of the definition of the class class ClassName : InterfaceName.
If the interface is inherited because you inherited a base class that implements the interface, the base class provides the implementation of the members of the interface. However, the derived class can reimplement any virtual interface members instead of using the inherited implementation.
When interfaces declare a default implementation of a method, any class implementing that interface inherits that implementation You need to cast the class instance to the interface type to access the default implementation on the Interface member. A base class can also implement interface members by using virtual members. In that case, a derived class can change the interface behavior by overriding the virtual members.
For more information about virtual members, see Polymorphism. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. That way there is no doubt about which implementation the class will have. The implementation in the class takes precedence over any default implementations. Java interfaces are a way to achieve polymorphism. Polymorphism is a concept that takes some practice and thought to master.
Basically, polymorphism means that an instance of an class an object can be used as if it were of different types. Here, a type means either a class or an interface.
The classes above are all parts of a model representing different types of vehicles and drivers, with fields and methods. That is the responsibility of these classes - to model these entities from real life. You want that implemented using a single method for each operation, available on each Car , Truck or Vehicle object. Please forget for a while, that implementing this functionality as methods directly on the objects may lead to a messy class hierarchy. Just imagine that this is how you want the operations implemented.
Where in the above diagram would you put these three methods, so they are accessible on all classes? One way to solve this problem would be to create a common superclass for the Vehicle and Driver class, which has the storage and serialization methods. However, this would result in a conceptual mess. The class hierarchy would no longer model vehicles and drivers, but also be tied to the storage and serialization mechanisms used in your application. A better solution would be to create some interfaces with the storage and serialization methods on, and let the classes implement these interfaces.
Here are examples of such interfaces:. When each class implements these two interfaces and their methods, you can access the methods of these interfaces by casting the objects to instances of the interface types.
You don't need to know exactly what class a given object is of, as long as you know what interface it implements. As you can probably imagine by now, interfaces provide a cleaner way of implementing cross cutting functionality in classes than inheritance.
A generic Java interface is an interface which can be typed - meaning it can be specialized to work with a specific type e. Let me first create a simple Java interface that contains a single method:.
This interface represents an interface which contains a single method called produce which can produce a single object. Since the return value of produce is Object , it can return any Java object. The above class CarProducer implements the MyProducer interface. The implementation of the produce method returns a new Car object every time it is called.
Here is how it looks to use the CarProducer class:. Notice how the object returned from the carProducer. Using Java Generics you can type the MyProducer interface so you can specify what type of object it produces when you use it. Here is first a generic version of the MyProducer interface:.
Now when I implement the MyProducer interface in the CarProducer class, I have to include the generic type declaration too, like this:. Now, when creating a CarProducer I can specify its generic interface type, like this:. As you can see, since the generic type for the CarProducer instance is set to Car , it is no longer necessary to cast the object returned from the produce method, since the original method declaration in the MyProducer interface states, that this method returns the same type as is specified in the generic type when used.
But - now it is actually possible to specify another generic type for a CarProducer instance than the type it actually returns from it's produce method implementation. If you scroll up, you can see that the CarProducer. So, the following declaration is possible, but would return in a ClassCastException when executed:. Instead, you can lock down the generic type of the MyProducer interface already when you implement it, in the CarProducer class.
An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. The interface keyword is used to declare an interface. An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface. When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface.
If a class does not perform all the behaviors of the interface, the class must declare itself as abstract. A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.
Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method. In this example, the Drawable interface has only one method. Its implementation is provided by Rectangle and Circle classes. In a real scenario, an interface is defined by someone else, but its implementation is provided by different implementation providers.
Moreover, it is used by someone else. The implementation part is hidden by the user who uses the interface. If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance. As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity.
It is because its implementation is provided by the implementation class. For example:. As you can see in the above example, Printable and Showable interface have same methods but its implementation is provided by class TestTnterface1, so there is no ambiguity. Since Java 8, we can have method body in interface. But we need to make it default method. Let's see an example:. An interface which has no member is known as a marker or tagged interface, for example, Serializable , Cloneable, Remote, etc.
Note: An interface can have another interface which is known as a nested interface. We will learn it in detail in the nested classes chapter. JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services. Please mail your requirement at [email protected] Duration: 1 week to 2 week.
Java Training Basics of Java. Abstract class Interface Abstract vs Interface.
0コメント