Abstract classes and Interfaces in Java

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/xiaoao808/archive/2008/03/11/1100886.html

Abstract class Implementation Interface

http://fishswing.iteye.com/blog/1527166

In Java, using an abstract class to implement an interface is not useless. On the contrary, there is time to play a big role.

When you want to implement only the individual methods in the interface (not all methods), you can write an abstract class to implement the interface, and implement all methods except the one you want (the method body is empty). Then use your class to inherit the abstract class, which only implements the method you need, so you can achieve your needs. However, if you implement the interface directly, you need to implement all the methods of the interface.

The following example can be used to understand well:

Example: There is an interface window, there are three methods, Draw (), Putcolor (), SetPosition () Three methods, programmers in the design of the page only focus on positioning it (draw draw () and coloring Putcolor () is implemented by the artist), So he only needs to implement the SetPosition () method, while the remaining two are not implemented. The design is as follows:

Interface

Interface window{

public void Draw ();

public void Putcolor ();

public void SetPosition ();

}

/**

* Abstract class, do not implement the required methods. The method body that implements the unwanted method is set to NULL,

*/

Abstract class Designedpage implements window{

public void Draw () {}

public void Putcolor () {}

}

/**

* Implement the class specifically, implement a specific method, only the required methods in the class

*/

public class Desingedpagea extends designedpage{

public void SetPosition () {

Set the window position

}

}

Typically, if we need to implement the draw () method in a concrete implementation class, it is called the draw () method of the parent class (Super.draw ()).

Self-understanding: In fact, this first by the abstract class implementation interface, and then let the implementation class inherit the benefits of abstract class, only when there are many kinds of implementation classes, can be revealed. Because, when there is only one implementation class, it is entirely possible to implement the interface in this implementation class only, and to set the unnecessary method to null (but if there is really only one implementation class that does not need abstract classes, if you want to introduce abstract classes naturally, consider that there will be many implementation classes in the future, So there is only one implementation of the case is not realistic, and the other is that there are many implementation classes, then you can see the benefits of this approach, because if we first use an abstract class in the interface we do not care about the method first through the empty implementation, and later our implementation class in inheriting this abstract class, These already implemented functions (that is, non-abstract functions), we can inherit directly, so that we do not have in each implementation class, the repetition of those we do not care about the method (even if we simply put them empty, but you do not think this duplication of useless work is boring), Abstract functions are not inherited and can only be implemented by rewriting.

Abstract classes and Interfaces in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.