interface and implementation in Java

Source: Internet
Author: User

We usually think of access control as a way to "hide implementation details." When you encapsulate data and methods within a class, you can generate a data type that has its own characteristics and behavior. But for two important reasons, Access adds its own boundaries to that data type. The first reason is to specify which client programmers can use and which cannot. We can build our own internal mechanism in the structure without having to worry about the client programmer using it as part of the interface to use it freely or "misuse" it.

This is a direct result of the second reason: we need to separate the interface from the implementation details. If the structure is used in a series of programs, but the user cannot do anything other than send the message to the public interface, we can change everything that is not public (such as "friendly", protected and private), The user is not required to make any changes to their code.
We are now in an object-oriented programming environment in which one class (class) actually refers to "a class of objects", as we say "fish" or "birds." All objects belonging to this class share these characteristics and behavior. "Class" is a description of the appearance and behavior of all objects belonging to this class.
In some early OOP languages, such as Simula-67, the role of the keyword class is to describe a new type of data. The same keywords are applied in most object-oriented programming languages. It's actually the focus of the entire language: There are many more situations where you need to create a new data type than a "container" that holds data and methods.
In Java, classes are the most basic OOP concept. It is one of the key words in the book that is not in bold print-because there are too many, it can cause serious confusion in page layout.
For clarity, consider creating a class with a special style: place public members at the very beginning, followed by protected, friendly, and private members. The advantage of this is that the user of the class can read from top to bottom, and first see what is most important to you (that is, public members, because they can be accessed from outside the file), and stop reading after encountering Non-public members, which are already part of the internal implementation details. However, the readability of the code has been addressed to a large extent by using the annotation documentation supported by Javadoc (introduced in chapter 2nd).

public class X {
public void Pub1 (?) { /* . . . */ }
public void Pub2 (?) { /* . . . */ }
public void Pub3 (?) { /* . . . */ }
private void Priv1 (?) { /* . . . */ }
private void Priv2 (?) { /* . . . */ }
private void Priv3 (?) { /* . . . */ }
private int i;
// . . .
}

Because the interface and implementation details are still mixed together, it is only partially easy to read. In other words, you can still see the source code-implementation details, because they need to be kept in the class. Showing the interface to a class of consumers is actually a "class browser" job. This tool finds all available classes, summarizes all the actions that can be taken on them (such as which members can be used, etc.), and displays them in a refreshing and pleasing form. By the time you read this book, all the best Java development tools should have their own browsers.

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.