Dark Horse Program Ape--java Object-oriented features: encapsulation, inheritance, polymorphism

Source: Internet
Author: User

                                       - ---------- Android Training ,Java Training, Java Learning Technology blog, look forward to communicating with you! ------------ 

One, Java object-oriented features. Encapsulation, inheritance, polymorphism

Packaging

Encapsulation is the main feature of object and class concepts.

Packaging. That is, to encapsulate objective things into abstract classes. And the class can put its own data and methods only to let the trusted class or object operation, to the untrusted information hiding.

Inherited

One of the main functions of object-oriented programming (OOP) language is "inheritance". Inheritance refers to the ability to use the full functionality of an existing class. and extend these functions without having to write the original class again.

A new class created through inheritance is called a subclass or derived class, and the inherited class is called the base class, parent class, or superclass.

The process of inheritance is from the general to the special process.

To implement inheritance, it can be implemented through "inheritance" (inheritance) and "combination" (composition). In some OOP languages. A subclass can inherit multiple base classes. However, under normal circumstances, a subclass can have only one base class, to implement multiple inheritance. Can be achieved through multilevel inheritance.

Inheritance concepts are implemented in three categories: implementation inheritance, interface inheritance, and visual inheritance.

Implementation inheritance refers to the ability to use the properties and methods of a base class without additional coding;

Interface inheritance refers to the ability to use only the names of properties and methods, but the subclasses must provide the implementation;

Visual inheritance is the ability of a child window (class) to use the appearance of a base window (class) and to implement code.

When considering the use of inheritance, it is important to note that the relationship between the two classes should be a "belongs" relationship. For example, Employee is a person, Manager is also a person, so these two classes can inherit the People class. But the Leg class can not inherit the person class, because the legs are not alone.

Abstract classes define only the generic properties and methods that will be created by the subclass. OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (inheritance and compositing) → Designing and implementing several stages with classes and instances.

Polymorphic

polymorphism (polymorphism) is a technique that allows you to set the parent object to be equal to one or more of his child objects, and after assignment, the parent can operate differently depending on the attributes of the child object currently assigned to it. Simply put, it is a sentence: agree to assign a pointer to a subclass type to a pointer of the parent class type.

Achieve polymorphism. There are two different ways. Overwrite, overload.

Override, which is the practice of defining the virtual function of the parent class again.

Overloading (overload) is the agreement that there are multiple functions with the same name, and that the tables of these functions are different (perhaps different in number, perhaps different in the type of the parameters, maybe both).

In fact, the concept of overloading does not belong to "object-oriented programming", the implementation of overloading is: The compiler according to the function of different tables, the name of the same name is decorated, and then the same name function is a different function (at least for the compiler).

For example, there are two functions of the same name: Functionfunc (p:integer): Integer, and Functionfunc (p:string): integer;.

Then the compiler has done a modified function name may be this: Int_func, Str_func. For calls to these two functions. has been determined between the compilers and is static (remember: is static). Other words. Their addresses are bound (early bound) at compile time, so overloading and polymorphism are irrelevant ! The true and polymorphic correlation is "overlay". After the subclass defines the virtual function of the parent class again. The parent class pointer is based on the different child-class pointers assigned to it, dynamic (remember: is dynamic!)

The call to this function is a subclass that cannot be determined during compilation (the address of the virtual function of the called subclass cannot be given).

Therefore, this function address is bound in the execution period (late bonding). The conclusion is that overloading is only a linguistic feature, independent of polymorphism, and irrelevant to object-oriented !

about polymorphism " don't be foolish. Assuming it's not late binding, it's not polymorphic.

”

So what is the role of polymorphism? We know. Encapsulation hides implementation details, makes code modular, and inherits the ability to extend existing code modules (classes). Their purpose is to-- code reuse . And polymorphism is to achieve there is a purpose-- interface Reuse ! The role of polymorphism is to ensure that classes are called correctly when inheriting and deriving a property of an instance of any class in the family tree.

Second, in detail with an example to show

public class Basicinfo {
private String name;
private int age;

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

public int getage () {
return age;
}

public void Setage (int.) {
This.age = age;
}

Public Basicinfo () {
Super ();
Name = "Zhang San";
Age = 18;
}

Public Basicinfo (String name, intage) {
Super ();
THIS.name = name;
This.age = age;
}

@Override
Public String toString () {
Return "Basicinfo[name=" + name + ", age=" + Age + "]";
}

}

The student class inherits from the Basicinfo class, the constructor is the same polymorphism, and the property school is encapsulated
public class Student extendsbasicinfo{
Private String School;

Public String Getschool () {
return school;
}

public void Setschool (Stringschool) {
This.school = School;

}

Public Student (Stringschool) {
Super ();
This.school = School;
}

Public Student () {
Super ();
School = "University of London, England";
}

Public Student (String Name,int age) {
Super (name, age);
School = "University of London, England";
}

@Override
Public String toString () {
Return "student[school=" + School + ", Name=" +getname () + ", age=" +getage () + "]";
}

}

For the two classes provided above, a basic information class. To realize the encapsulation and polymorphism of the attributes in the information class, a student class realizes the inheritance of the basic information class and encapsulates its own attribute, which better explains its characteristics.

Dark Horse Program Ape--java Object-oriented features: encapsulation, inheritance, polymorphism

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.