开发者

Explain Java Interfacing?

开发者 https://www.devze.com 2022-12-23 01:23 出处:网络
Can someone explain what or h开发者_C百科ow interfacing works (in Java), never really understood it.It makes your class more formal about the behavior. As interface in every thing,in java it shows a w

Can someone explain what or h开发者_C百科ow interfacing works (in Java), never really understood it.


It makes your class more formal about the behavior. As interface in every thing,in java it shows a way to communicate with your class. If you mean the mechanic - I think it is not so important to understand it. Concept is main thing here.


An Interface describes methods and their signature without having an implementation. All classes which implement an interface can be replaced by each other.

See also Interface concepts

Example:

The interface java.util.List is specialization of Collection and is implemented by AbstractList, ArrayList, LinkedList, Vector this means all methods declared in this interface can be called on an instance of objects of this type.

EDIT: Less abstract

interface PlaySound {
    public void play();
}

public class MP3Player implements PlaySound {
    public void play() {
       // do complicated stuff
    }
}

public class WAVPlayer implements PlaySound {
    public void play() {
       // do complicated stuff
    }
}


To get the point across about interfaces, I recommend you read Head First Java, especially the inheritance chapter in the book. Infact, everything in the book is spot on for any java beginner to understand.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号