Java notes: Packages and Interfaces

Source: Internet
Author: User

One, the package

Use the PACKAGE keyword to declare packages. The role of a package is equivalent to a namespace. If the package to which the class belongs is not explicitly declared, then the class is placed in the default package, and the default package does not have a name. Package support is created hierarchically, which supports nesting.

Import the package using the Import keyword. Supports importing a class or package from a package, and if you do not import a declaration variable, you need to write the full path.

import java.lang.*; // Importing Packages import java.util.ArrayList; // Import Class class Solution {    ArrayList ArrayList; // has been imported    Java.util.LinkedList LinkedList; // not imported }

Second, the interface

Declares an interface. The normal method of an interface cannot exist in a specific implementation.

Interface Color {    void setcolor (int  color);     int GetColor ();} Interface Line {    void setLength (int  length);     int getlength ();}

Implements the interface. Class can implement multiple interfaces at the same time.

classColorfullineImplementsColor, line {intcolor; intlength; Colorfulline (intColorintlength) {         This. color =color;  This. length =length; } @Override Public voidSetColor (intcolor) {         This. color =color; } @Override Public intGetColor () {returncolor; } @Override Public voidSetLength (intlength) {         This. length =length; } @Override Public intGetLength () {returnlength; }}

nested interfaces. When an interface is a member of a class or other interface, there are more access restrictions to choose from.

class A {    publicinterface  I {        boolean isnegative (int  x) ;    }} class Implements A.I {    @Override    publicboolean isnegative (int  x) {         return x < 0;    }}

interface variables. You can use interfaces to import shared constants into multiple classes.

Interface RBG {    int RED = 0;     int BLUE = 1;     int GREEN = 2;}

Extension interface. Interfaces can inherit other interfaces.

Interface extends RBG {    int ORANGE = 3;     int YELLOW = 4;     int CYAN = 5;     int PURPLE = 6;}

The default implementation. Interfaces can declare default methods and can be overridden.

Interface I {    defaultvoid  Show () {        System.out.println ("Hi");}    } class Implements I {    @Override    publicvoid  Show () {        System.out.println (" Hello ");}    }

Variable members and static methods can be called directly.

Interface I {    int val = 0;     Static void Show () {        System.out.println (val);    }} class Solution {    publicstaticvoid  main (string[] args) {        I.show ();        System.out.println (I.val);    }}

Java notes: Packages and Interfaces

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.