Java Object-oriented programming (v)--the abstraction and encapsulation of four features

Source: Internet
Author: User
Tags custom name

1. Abstraction

When we define a class in front, we actually extract the common attributes and behaviors of a kind of things and form a physical model (template). This method of studying the problem is called abstraction.

2. Encapsulation

Encapsulation is the abstraction of the data and the operation of the data is encapsulated together, the data is protected inside, the other parts of the program only through the authorized operation (member method), the data can be manipulated.

Ways to implement encapsulation in Java:

Case: Can't casually see people's age, salary and other privacy [Demo116.java]

//package case [Demo116.java] Public classdemo116{ Public Static voidMain (String []args) {//Create an employeeClerk clerk1=NewClerk ("Floret", 24,4567.6f); System.out.println ("The name is" +clerk1.name+ "salary" +clerk1.getsal ()); }}//Staffclassclerk{ PublicString name; //Private, public-owned    Private intAge ; Private floatsalary;  PublicClerk (String name,intAgefloatSal) {         This. name=name;  This. age=Age ;  This. salary=Sal; }    //control and access to private properties through a member method     Public floatgetsal () {return  This. Salary; }}

Package -- access Control modifiers

Java provides access to four kinds of access control modifier notation control methods and variables:

1, public level: with public decoration, open to the outside.

2. Protected level: modified with protected, exposed to subclasses and classes in the same package.

3. Default level: No modifier symbols are exposed to the same package class.

4, Private level: with private decoration, only the class itself can be accessed, not public.

the necessity of the package

Now there are two programmers together to develop a Java project, programmer Xiao Ming wants to define a class named dog, programmer Xiao Qiang also want to define a class also called dog. The question arises, how can we solve this conflict?

Package -- three major functions

1. Classes that distinguish the same name

2, when a lot of classes, can be very good management class

3. Control the scope of access

This shows that the introduction of the package is a good solution to the above problems.

Packaging commands

Package com. custom name;

Note: Packing commands are usually placed at the beginning of a file.

Package Naming conventions

Lowercase letters, such as Com.sina.davis

Common packages

A package that contains a lot of classes, the common packages in Java are:

Java.lang.* package automatically introduces JAVA.UTIL.* Toolkit

JAVA.NET.* Package Network Development Package java.awt.* Package Window Toolkit

How to introduce Packages

Syntax: Import package;

such as import java.awt.*;

We introduce the primary purpose of a package to use the class under that package

At this point, the definition of our class can be perfected one step further:

 package package name;  class      class name {member variable;    Construction method; member method;}  

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.