Classes and objects in Java

Source: Internet
Author: User
Tags modifier

Classes and objects

Class: A class is an abstraction of an object, which means that the class is the generic name of the same class of objects that have the same properties and methods.

Object : Object is a specific thing , Java as an object-oriented language, it can be said in Java everything is the object. The object itself has its own properties and methods.

Take a life example: we live in the common people, mobile phones, computers, cars, birds and so on can be thought of as a class, and then each of the different people is the object of this class, Millet 6, Apple 7 is the mobile phone class of different objects.

Knowing what a class is, what is there in the class?

Let's take a look at the definition of a class:

 Public class class Name {  access modifier type Name property name;
 Construction method
Access modifier constructor Method name ([data type parameter name]) {
}
 //Common method The access modifier returns a value type method name (data type parameter name ...) { } }

You can see that a class consists of a property (which can be a variable or a constant) and a method;

The methods within the class can be divided into construction methods and other methods.

There are several types of methods in Java: Common method, construction method, static method (static decoration), final method (fianl decoration)

Construction method

When we instantiate an object, we usually use this way: Class Name Object name =new class name ();

Of course, it is said that this is the name of the class is not very accurate, more accurate to say here is the name of the construction method behind new. A constructor method is a special method that is invoked every time an object is instantiated.

The definition of the constructor method is this: public class name (parameter type argument name ...) {  }

Here are a few of the ways to be aware of the construction method:

1. If the constructor is not defined in the class, we can create a new object as well, in which case the JVM will provide a default method for constructing an parameterless constructor for each class. This is also very easy to prove, in the class do not write any of the construction method can be normal instantiation of the object, if you write a parameterless construction method the JVM will not error.

2. If you define an argument constructor within a class, the JVM will no longer provide a default parameterless constructor for the class. This allows you to define an argument constructor in a class, and then invoke the parameterless construction method at instantiation time, and you can see that the JVM will error.

3. The method name of the construction method must be consistent with the class name, and the constructor method does not return a value type.

The function of the construction method:

1. Instantiating an object (the New keyword simply opens up a space in memory, and the constructor writes the properties and methods of the class to the heap memory and returns the first address of the memory)

2. Assigning an initial value to a property

Overloading of methods

1. Method overloading conditions: must be in the same class or have an inheritance relationship of the classes, method overloads when the method name must be exactly the same, and then the parameters of the method must be different, the type of the return value can be the same or different.

2. Under what circumstances do I need to use the overloaded method?

Overloading of common methods: You can imagine examples of life, such as learning or working tired we have a way to relax, but everyone's way of relaxation is different, such as: Relax (travel), relax (play the game), relax (sleep).

Overloading of construction methods: for example, to produce two kinds of mobile phones, one is a keyboard machine, one is a touch screen machine, so when you create a new object, you can call different construction methods to instantiate the two different objects.

3. Invocation of overloaded methods: Depending on the parameters of the method, different parameters are called to invoke different methods.

Here's one thing to be aware of is overloading and rewriting :

  overloading : In a class of the same class or with an inheritance relationship, methods have the same method name, different parameters , and are used to handle different types of data.

  overriding (overwrite, overwriting): In a class that has an inheritance relationship, the method name, parameter, and return value of the method must be consistent with the method being overwritten. This time the new method overrides the inherited method, so it is called a method rewrite or overwrite. Method overrides are primarily used to implement functions that are not the same as the parent class.

this keyword

1, the use of this keyword:

In a constructed method, the This keyword is used to represent the properties of the current class if the parameter name of the constructor method is the same as the property name of the class. If you do not use the This keyword, Java assigns a value to the variable based on the on-the-go principle, which is to assign the parameter to the parameter without any effect on the properties of the class.

This can also invoke methods of the current class, such as calling the constructor method: this (parameter list), calling the common method: this. Method name (parameter list);

2, who is the expression of this keyword?

The This keyword represents the current object, the object that is currently calling the method, and is the object represented by the THSI keyword.

Person p1=New person (); the. tell (); // is to represent P1 this object calls the Tell method. person p2=new person (); the. tell (); // This is the object that P2. 

Classes and objects in Java

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.