Advanced Concepts for Java Learning Notes (vii) classes

Source: Internet
Author: User
Tags modifier modifiers

The advanced concepts of the class include access modifiers, encapsulation, static members, initialization blocks, and mixed content such as inner classes.

Access modifiers:

(1). Open access level: Use the public keyword modifier. Members that are decorated with the public keyword are exposed externally, that is, public members can be accessed by any other object.

(2). Protected Access level: Use the protected keyword modifier. Protected members can be accessed by classes in the same package, and can also be accessed by subclasses of the class, regardless of which package the subclass is in.

(3). Default Access level: No access modifier. Members of the default access level can be accessed by other classes in the same package.

(4). Private access level: decorated with the private keyword. It is the lowest level in the four access modifiers. Private members are accessible only to the class itself, not to the public.

The class itself also has an access modifier. But the tired access modifier is only public and the default two.

Encapsulation: Encapsulation is the technique of making member variables in a class private and providing public methods to access these member variables. If a member variable is declared private, it cannot be accessed by other classes, thus hiding the member variables in the class. So encapsulation is also known as data hiding.

Inheritance, encapsulation, polymorphism and abstraction are the four basic concepts of object-oriented programming.

The Get method that allows you to get member variables is called the accessor (accessor) method, which allows you to modify a member variable's get method called the modifier (mutator) method.

public class salesperson{

private String name;

private int id;

No parameter constructor

Public Salesperson () {

}

A parametric constructor

Public Salesperson (String name,int ID) {

Super ();

THIS.name = name;

This.id = ID;

}

public void SetName (String name) {

name = name;

}

public void GetName () {

return name;

}

public void setId (int id) {

Id=id;

}

public void GetName () {

return ID;

}

}

When writing get (), the Set () method is named in a way that typically has an operation that automatically generates these methods on the application.

The benefits of encapsulation: the member variables of a class can be read-only or write-only; A class can have a holistic control over what is stored in its member variables; The user of the class does not need to know how the class stores the data and implements the reuse of the code.

Static member: Keyword static allows a member variable or method not to be associated with a specific instance of a class. A member variable or method declared through the keyword static can be considered global, and any other class can directly access a static member variable or call a static method without requiring an instance of the class.

A static member of a class is often referred to as a class member because a static member is associated with a class, not an instance. Non-static member variables and methods are often referred to as instance members because non-static member variables and methods exist only in instances of the class.

Static methods cannot access non-static members.

Instance Initialization block:

/**

* Parent Class

*/

public class electronics{

Public Electronics () {

System.out.println ("Construction of an electronic device");

}

}

/**

* Instance initialization block

*/

public class CDPlayer extends electronics{

private int songnumber;

public cdplayer (int x) {

Super ();

System.out.println ("Construct a CD player");

Songnumber=x;

}

{

System.out.println ("within the instance Initialization block");

}

}

/**

* Run

*/

public class instanceinitdemo{

public static void Main (String [], args) {

System.out.println ("Within the Main method");

CDPlayer C1 = new CDPlayer (1);

CDPlayer C2 = new CDPlayer (7);

}

}

Order of Occurrence:

The corresponding constructor in the subclass is called, the call to super is executed, the control flow jumps to the corresponding parent class constructor, the parent class constructor finishes executing, the control process jumps back to the subclass constructor, and the instance initialization block executes before any statements after super () in the subclass constructor are executed; Executes the statement after Super () in the subclass constructor.

Inner class:

Static inner class: As a static member of a class, exists in the interior of a class;

member Inner class: As a member of a class, exists in the interior of a class;

Local inner class: A class that exists inside a method;

Anonymous inner class: A class that exists inside a class but has no class name.

Advanced concepts of the Java Learning Note (vii) class

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.