Java object-oriented-class

Source: Internet
Author: User

Oop--object oriented programming is the biggest feature of Java, and he is not like C + +, Java belongs to the downright object-oriented, each file is in the form of a class, the main function is also included in a large class.

1, the declaration about the class defines the following template:

Class user{

String name; //member variables

int age;

public void Show ()

{

System.out.println ("User name is:" +name+ ", Age is:" +age); //Member method

}

}

2, modifier of member variable:

Static: Represents a class member statics variable, which allocates memory only when the first object is generated, equivalent to a global variable, and all instance objects share one, which can change the effect on each other. belongs to the class and can be accessed directly through the class name.

Final: Represents a constant that can only be referenced and cannot be changed, equivalent to a const in C + +.


3. Member method: Note that you must belong to a class and you cannot define a method that does not belong to any class. Modifier:

static method, which can be called directly through the class name, or by an instance object, of course.

Abstract: Abstraction method, only method declaration, no method body.

Final: The ultimate method, which cannot be overridden in a subclass.


4, the construction method of the class

The name is consistent with the class name and is used to initialize the member variables when the object is created. Note that the name should be the same as the class name, and cannot have a return value, especially to note that the front even void can not be added.

In addition, the construction method can be overloaded (that is, the same name and different parameters).


5. Creation of objects

< class name > < object name > = new construction Method ();

Such as:

User USR1 = new user (); //Default construction method

User USR2 = new User ("Sun", 28); //Custom construction method (overloaded)


6. Encapsulation Permissions for classes

There are only two types of access rights, default and public. In the same file, only one is public, and the name of the class must be the same as the file name, and the main method is in the public class. The default permissions cannot exceed the package. The Main method is written: public static void Main (String argv[]) {}

Access rights for members:

Public: can be accessed by any other class.

Protected: Can be accessed by classes in the same package and subclasses of that class in other packages.

Default: Can be accessed by classes in the same package.

Private: can only be accessed by members in the same package.


7. Inheritance of Classes

Class Subclassname extends superclassname{}

Unlike C + +, Java does not support multiple inheritance, and subclasses can have only one parent class.

About overloads and overrides of methods:

Overloading: Same class, same name, different parameter.

Overwrite: Parent class and subclass, subclass method Overrides parent class method, name and argument should be the same.


8. Abstract class

The idea of defining method declarations, not necessarily methods, is understood first.

This is defined by adding an abstract before the class name and the method name:

Abstract class student{

int stu_id;

void set ID (int id) {

stu_id = ID;

}

Defining abstract methods

abstract void show ID (); //must pay attention to the semicolon, indicating the end, no specific method body

}

Note that abstract classes can also have non-abstract methods, but classes that have abstract methods are abstract classes. If its subclasses do not implement the abstract method concretely, then the subclass becomes an abstract class.


9. Final class

The class name is preceded by final, which means that the class cannot be inherited, and that the developer-written class is not modified after it has been inherited by others.

Java object-oriented-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.