Java Interfaces and packages

Source: Internet
Author: User

A The role of API packages, interfaces, exception handling

A package is a collection of classes and interfaces. Packages can be used to put common classes or functions similar classes in a package. The Java language provides a system package that contains a large number of classes that can be referenced directly when writing Java programs. To facilitate the management and use of them, these classes are divided into different packages. The package is also known as a class library or API package, the so-called API (Application program Interface) is the application interface. API package-aspects to provide a wealth of classes and methods for everyone to use, such as drawing graphics, playing sound and so on, on the other hand, and the system hardware and software to deal with the successful implementation of user program functions. All Java API packages are in "java." Start with a different user-created package.

In the Java language, an interface is a special syntax structure that can contain a set of method declarations and some constants. A Java class can implement multiple Java interfaces, which also makes up for the weaknesses of the Java class that do not support multiple inheritance.

Two Create and use a custom package

1. Customizing how packages are declared

<package> < custom package names >

The declaration package statement must be added to the first line of the source program, indicating that all classes declared by the program file belong to this package.

2. Use of packages

A package's general public or public interfaces can be accessed outside of the package, while non-public types are scoped to the package, accessible within the same package, and externally hidden. Even the nested package is hidden; When we want to use a package, we can do it with the keyword import :

Import PackageName;

Note: When the package is introduced, the classes and interfaces of the nested package are not introduced automatically, for example:

Import java.swing.event.*;

Only represents the introduction of all the interfaces in the package java.swing.evevt , but the classes and interfaces in the package java.swing are not introduced.

Issues to be aware of when creating a package: the definition of the package to be created under the specified file, if not, the problem arises as follows: The package MyPackage does not exist, we have two methods to solve the above problem. We can create a new MyPackage folder to put the Ymd.java file into the mypackage file, this compiled Ymd.class The file is automatically compiled into the file, but in myelicpse , if we want to use this package, we must specify the folder where the report is located when the package is created, that is, the new class must be under the specified package, that is, under the folder of the package you created.

Three, the definition and function of the interface

An interface can be thought of as a collection of methods and constants that are not implemented. Interfaces are similar to abstract classes in that the methods in an interface simply declare and do not define any specific methods of operation. Interfaces are used to solve problems in the Java language that do not support multiple inheritance. Single inheritance can make the language itself simple structure, clear level, easy to manage, safe and reliable, avoid conflict. But it also restricts the functionality of the language. In order to implement multiple inheritance functions in practical applications, Java uses interface technology, a class can implement multiple interfaces to achieve the use of common constants and some common methods.

Experience: interfaces are the same conventions that the class adheres to, and it is an innovative way for Java to implement multiple inheritance.

Java Interfaces and packages

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.