Java Object-Oriented Programming I

Source: Internet
Author: User

Java Object-Oriented Programming I

Three main features:

Encapsulation: Encapsulation

Inheritance: Inheritance

Polymorphism: Polymorphism

Creation object:

Person per = NULL; // declare an object

Per = new person (); // instantiate the object

Or

Person per = new person (); // Declaration + instantiate object

Object Attributes are stored in heap memory.

The object method is stored in the code area and is global-oriented.

Nullpointerexception is a common Java error, indicating that null points.

One stack memory can allocate Multiple Heap Memory Spaces

For example:

Person Per1 = NULL;

Person per2 = NULL;

Per1 = new person ();

Per2 = Per1;

The Per1 content is also changed after the attribute in per2 is changed!

Private is the encapsulation method of attributes. It protects attributes!

The constructor cannot contain return values.

The constructor is not called when an object is declared.

Constructor is not required

If a constructor is not explicitly declared in a class, the system automatically generates a constructor without any parameters.

If the constructor with parameters is declared in the class, the system will not declare the constructor without parameters again.

The constructor can be overloaded.

You can overload a constructor if its parameter types or numbers are different.

The constructor differs from the method in the following three aspects: modifier, return value, and name.

1. Like the method, the constructor can have any access modifier: public, protected, private, or not modified (usually called by package and friendly ). unlike methods, constructors cannot have the following non-access modifiers: abstract, final, native, static, or synchronized.

2. The return type is also very important. The method can return any type of value or no return value (void). The constructor does not return a value or require void.
3. The name of the two. The constructor uses the same name as the class, while the method is different. According to the habit, the method usually starts with a lower-case letter, while the constructor usually starts with a upper-case letter. The constructor is usually a noun because it is the same as the class name, and the method is usually closer to the verb because it indicates an operation.

Anonymous object

The object is declared without the use of the stack memory space, that is, new person (). Tell () is used directly ();

Class Design Analysis

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.