Three main features of Java object-oriented

Source: Internet
Author: User

Three main features of Java object-oriented: encapsulation, inheritance, polymorphism

Encapsulation and inheritance are almost always prepared for polymorphism

First, the package

First, attributes can describe the characteristics of things, and methods can describe the actions of things. Encapsulation is the inclusion of the same kind of things (including properties and methods) in the same class, easy to use.

    1. Encapsulation: encapsulation, also known as information hiding, refers to the use of abstract data types to encapsulate data and data-based operations, making it an indivisible whole, the data is hidden inside the abstract data, as far as possible to hide the details of the data, only to retain some interfaces to the outside world to contact. This means that the user does not need to know the details of the implementation of the internal data and methods, just according to the interface left on the outside to operate on the line.
    2. Benefits of Encapsulation:

1) achieve a professional division of labor

2) Good encapsulation can reduce coupling

3) The structure within the class can be modified in its own

4) More precise control of the members

5) hiding information, implementing details

    1. Steps for Encapsulation

1) Modify the visibility of attributes to restrict access to properties

2) Create a team assignment and value method for each attribute that is used to access these properties

3) in the assignment and value method, add the access limit to the attribute

To achieve good encapsulation, we usually declare the member variable of the class as private, which is accessed through the public method. For the operation of a variable, there are generally 2 operations to read and assign, we define 2 methods to achieve these 2 operations, one is getxx (xx means the name of the member variable to access) to read this member variable, and the other is SETXX () is used to assign a value to the variable.

Let's take a look at the following example:

public class Husband {

/*

     * Encapsulation of attributes A person's name, gender, age, wife are the private attributes of this person

*/

private String name;

Private String sex;

private int age;

Private Wife Wife;

/*

* Setter () , Getter () is the interface that the object developed externally

*/

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.) {

This.age = age;

}

public void Setwife (Wife Wife) {

This.wife = wife;

}

}

Ii. inheritance
    1. 1. Java inheritance

Java inheritance is one of the most prominent features of object-oriented. Inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities. Java does not support multiple inheritance, single inheritance makes Java's inheritance very simple, a class can have only one parent class, easy to manage the program, the parent class is the generalization of the subclass, the subclass is the parent class specialization (materialization)

Inheritance is expressed as an intersection between object classes, which enables a class of objects to inherit data members and member methods of another class of objects. If Class B inherits from Class A, then the object belonging to B has all or part of the nature of Class A (data attributes) and functions (operations), we call the inherited class A as a base class, a parent class, or a superclass, whereas inheriting Class B is a derived class or subclass of a.

Inheritance avoids repeating descriptions of common features between general classes and special classes. At the same time, inheritance can clearly express the conceptual scope in which each common feature is adapted--attributes and operations defined in a generic class are adapted to the class itself and to all objects of each layer of special classes below it. The application of the principle of inheritance makes the system model more concise and more clear.

    1. 2. features of Java inheritance

1) The inheritance relationship is passed. If Class C Inherits Class B and Class B inherits Class A (multiple inheritance), then Class C has properties and methods inherited from Class B, as well as properties and methods inherited from Class A, and can have its own newly defined properties and methods. Inherited properties and methods, although implicit, are still properties and methods of Class C.

2) inheritance provides the software reuse function. If Class B inherits from Class A, then building Class B requires only a few more features (data members and member methods) that are different from the base class (Class A). This method can reduce the redundancy of code and data, and greatly increase the reusability of the program.

3) Inheritance greatly increases the maintainability of the program by increasing the consistency to reduce the interface and interface between modules.

    1. 3. instances of inheritance

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);

}

}

Three, polymorphic

The rewriting, overloading and dynamic connection of the method are polymorphic.

Java introduces the concept of polymorphism, one of the reasons is that it is in the inheritance of the class and C + +, which allows multiple inheritance, which does give it a very powerful function, but the complex inheritance of the C + + developers have also brought greater trouble, in order to avoid the risk, Java only allow single inheritance, There is a is-a relationship between the derived class and the base class (that is, "cat" is a "animal"). Although this ensures that the inheritance relationship is simple and clear, but there is bound to be a large number of functional limitations, so Java introduced the concept of polymorphism to compensate for this shortcoming, in addition, abstract classes and interfaces are also important means to solve the single inheritance restrictions. At the same time, polymorphism is also the essence of object-oriented programming.

To understand polymorphism, you first need to know what is "upward transformation".

I defined a subclass cat, which inherits the animal class, which is the parent class of the former. I can pass

Cat C = new Cat (); To instantiate a Cat object, this is not difficult to understand.

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

What does that mean?

Very simply, it means I have defined a reference to the animal type, pointing to the new cat type object. Because Cat is inherited from its parent class animal, references to animal types can point to objects of the cat type. So what's the point of doing that? Because subclasses are an improvement and an extension to the parent class, the generic subclass is more powerful than the parent class, the property is more unique than the parent class, and the object that defines a reference to a child class of a parent class can use both the powerful functions of the subclass and the commonality of the parent class. So

The parent class reference can only invoke methods and properties that exist in the parent class, and cannot invoke the extended part of the subclass, because the parent class reference refers to the parent class that inherits from the child class object of the heap, but if you force the superclass to be converted to subclasses, you can call methods that are newly added in the subclass and that the superclass does not. )

At the same time, a method in the parent class can be called by a reference to the parent class only if it is defined in the parent class and is not overridden in the subclass.

For a method defined in a parent class, if the method is overridden in a subclass, then a reference to the parent class type invokes this method in the subclass, which is the dynamic connection.

Three main features of Java object-oriented

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.