Three main features of Java object-oriented, java object-oriented

Source: Internet
Author: User

Three main features of Java object-oriented, java object-oriented
Java object-oriented features: encapsulation, inheritance, and Polymorphism

Encapsulation and inheritance are almost always prepared for polymorphism.

I. Encapsulation

First, attributes can describe the features of things, and methods can describe the actions of things. Encapsulation is to classify the commonalities (including attributes and methods) of the same class into the same class for ease of use.

1) achieve a professional division of labor

2) good encapsulation can reduce Coupling

3) the internal structure of the class can be modified by itself

4) more precise control over members

5) hide information and implement details

1) modify the attribute visibility to restrict access to the attribute

2) create a group of value assignment and value methods for each attribute to access these attributes.

3) add access restrictions on attributes to the assignment and value methods.

 

To achieve good encapsulation, we usually declare the class member variables as private and access this variable through the public method. For an operation on a variable, there are generally two operations for reading and assigning values. We define two methods to implement these two operations respectively, getXX (XX indicates the name of the member variable to be accessed) is used to read this member variable, and setXX () is used to assign values to this variable.

 

Let's take a look at this example:

Public class Husband {

 

/*

*Encapsulation of attributesA person's name, gender, age, and wife are private attributes of the person.

*/

Private String name;

Private String sex;

Private int age;

Private Wife wife;

 

/*

* Setter (),Getter ()Is the interface for external development of this object

*/

Public String getName (){

Return name;

}

 

Public void setName (String name ){

This. name = name;

}

 

Public String getSex (){

Return sex;

}

 

Public void setSex (String sex ){

This. sex = sex;

}

 

Public int getAge (){

Return age;

}

 

Public void setAge (int age ){

This. age = age;

}

 

Public void setWife (Wife wife ){

This. wife = wife;

}

}

Ii. Inheritance

Java inheritance is the most significant feature of object-oriented systems. Inheritance is to derive a new class from an existing class. The new class can absorb the data attributes and behaviors of the existing class and expand new capabilities. JAVA does not support multi-inheritance. Single inheritance makes the inheritance relationship of JAVA very simple. A class can only have one parent class, which is easy to manage programs. The parent class is a generalization of sub-classes, subclass is the special (concrete) of the parent class)

Inheritance represents the intersection between object classes. It allows an object to inherit the data member and member methods of another object. If Class B inherits Class A, the object belonging to Class B has all or part of Class A (data attributes) and functions (operations ), we call the inherited Class A as the base class, parent class, or superclass, And the inherited Class B as the derived class or subclass of Class.

Inheritance avoids repeated descriptions of common features between general and special classes. At the same time, through inheritance, you can clearly express the scope of concepts that each common feature adapts to-attributes and operations defined in a general class are adapted to the class itself and all of the special classes below it object. The inheritance principle makes the system model concise and clear.

1) the inheritance relationship is passed. If Class C inherits class B and class B inherits Class A (Multi-inheritance), Class C has the attributes and methods inherited from Class B, there are also attributes and methods inherited from Class A, and new attributes and methods. Although the inherited attributes and methods are implicit, they are still attributes and methods of class C.

2) Inheritance provides software reuse. If Class B inherits Class A, you only need to describe A small number of features (data member and member methods) different from the base class (Class A) when creating Class B. This method can reduce code and data redundancy and greatly increase the reusability of the program.

3) Inheritance reduces inter-module interfaces and interfaces by enhancing consistency, greatly increasing the maintainability of programs.

Class Person1 {

Public String name = "xiaomiao ";

Public int age = 20;

}

 

Class Student extends Person1 {

Void study (){

System. out. println ("I can study! ");

}

}

 

Public class JiCheng {

Public static void main (String args []) {

Student stu = new Student ();

// Stu. name = "zhangsan ";

// Stu. age = 20;

System. out. println ("name =" + stu. name + "," + "age =" + stu. age );

}

}

Iii. Polymorphism

Method rewriting, overloading, and dynamic connection constitute polymorphism;

One of the reasons why Java introduces the concept of polymorphism is that it is different from C ++ in class inheritance. The latter allows multi-inheritance, which indeed brings a very powerful function to it, however, the complex inheritance relationships also cause greater trouble for C ++ developers. To avoid risks, Java only allows single inheritance, the relationship between the derived class and the base class is IS-A (that is, "cat" is a "animal "). Although this ensures that the inheritance relationship is simple and clear, it is bound to have a lot of functional limitations. Therefore, Java introduces the concept of polymorphism to make up for this deficiency. In addition, abstract classes and interfaces are also an important means to solve the limitation of single inheritance. At the same time, polymorphism is also the essence of object-oriented programming.

To understand polymorphism, we must first know what is "upward transformation ".

I have defined a subclass Cat, which inherits the Animal class, so the latter is the parent class of the former. I can use

Cat c = new Cat (); it is not hard to understand to take a Cat object as an example.

But when I define it like this: Animal a = new Cat ();

What does this mean?

It means that I have defined an Animal type reference, pointing to the newly created Cat type object. Since Cat inherits its parent class Animal, the reference of the Animal type can point to the Cat type object. So what is the significance of this? Because subclass is an improvement and extension of the parent class, it is generally more powerful than the parent class in terms of functionality and has more unique attributes than the parent class, defining a reference of a parent class pointing to an object of a subclass can both use the powerful functions of the subclass and extract the commonalities of the parent class. So,

The parent class reference can only call methods and attributes in the parent class, but cannot call the extension part of the subclass. Because the parent class reference points to the parent class inherited by the Child class object in the heap; (However, if the superclass is forced to be converted into a subclass, you can call the method newly added to the subclass but the superclass does not have .)

At the same time, a method in the parent class can be referenced and called by the parent class only when it is defined in the parent class but not overwritten in the subclass;

For methods defined in the parent class, if this method is rewritten in the subclass, the reference of the parent class type will call this method in the subclass, which is a dynamic connection.

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.