Basic Java object-oriented features

Source: Internet
Author: User

1. Call stack: Simply said is a method call in the maintenance program, the more the first execution method is closer to the bottom of the call stack, when a method is executed, it will be removed from the top of the call stack.

If a Java program is a multithreaded application, it may have multiple call stacks.


2. There are three possible scenarios after the method call:

(1) The method returns a value, either a base data type or a reference type.

(2) no return value.

(3) throws an exception to the caller of the function, for example, nullpointerexception (null pointer exception), arrayindexoutofboundsexception (array subscript out-of-bounds exception).

Note: Methods in Java can only appear in the class, and then the Java class without the Main method is not a program.


3. Method signature: The method signature refers to all content before the method {}. A method method signature consists of the following parts:

(1) Access modifier:

A. Public access level, allowed to access the object includes: objects of this class, the same package of different classes of objects, different packages of objects, sub-class objects;

B. Protected Access protection level, the objects allowed to access include: objects of this class, the same package of different classes of objects, sub-class objects;

C. Default (Do not write) class, the object that is allowed to access includes: objects of this category, objects of different classes of the same package;

D. Private access level, allowing access to objects only: objects of this class;

(2) Optional modifiers:

Static, Final (final), abstract (abstract), native (local), synchronized (synchronous).

Where the member of the static adornment is a static member and is shared for all members of the class.

In a static method, only static members can be called directly, and non-static members cannot be called directly. Required for invocation: class name. Member Name

In a member method (that is, a method that is not decorated with static), any member can be called directly.

(3) Return value type:

Includes, the base data type, the reference data type, or no return value of void.

(4) Method name:

Any valid Java notation can be used.

(5) Formal parameter list:

The method name is enclosed in parentheses to pass the data parameter when the method is called, and the method that passes the parameter in Java is passed only by value.

(6) Throw exception list:

With the throws keyword, when a method does not itself or does not want to solve the problem, throw an exception, can throw multiple exceptions, separated by commas.

4. Method overloading: Overloads of the method are called when there are two or more of the same method names in the same class, but there are different methods for the argument list.

5. Constructor (construction method):

(1). is a special method that does not declare a return value type, and the method name must be the same as the class name to initialize the member variables in the object. This method is invoked when instantiating an object.

(2). When a constructor is not added to the class, the compiler automatically adds a parameterless constructor, and if we add the constructor ourselves, the compiler does not add the default constructor.

(3). In the constructor, you can use this () to invoke the other constructors in the same class, but be aware that the this statement that invokes the constructor must be the first executable statement in this constructor.

6. Arrays:

(1). Declare an array: data type [] variable name;

(2). Allocation space: Variable name = new data type [length]; After allocating space, the array will have a default value.

(3). Allocating space for a declaration: data type [] Variable name = new data type [length];

(4). Initialize array: data type [] Variable name = new data type []{}; or data type [] variable name = {};

(5). The length of the array is fixed, but the declaration cannot grow or shrink. Then the length of the array can be obtained by using the array name. length.

(6). foreach (for array or collection traversal) syntax: for (type variable name: Array or collection name) {}

7. Inheritance:

(1). In object-oriented programming, you can extend an existing class and inherit the class's properties and behavior to create a new class.

(2). An existing class is called a parent class or base class, a superclass, or a subclass, which can also be called a derived class.

(3). Java does not support multiple inheritance, single inheritance makes Java's inheritance simple, a class can have only one parent class, easy to manage the program, and a class may implement multiple interfaces, thereby overcoming the disadvantages of single inheritance.

(4). The inheritance relationship is passed. If Class C Inherits Class B and Class B inherits Class A, then Class C has a property method inherited from Class B, a property method inherited from Class A, and a new property method that you define.

    

Basic Java object-oriented features

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.