- ---------- Android Training ,Java Training, Java Learning Technology blog, look forward to communicating with you! ------------
Java object-oriented features, encapsulation, inheritance, polymorphism
Packaging
Encapsulation is the main feature of object and class concepts.
Encapsulation, which is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only 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 all the functionality of an existing class and to extend these capabilities without rewriting the original class.
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, in general, a subclass can have only one base class, and to implement multiple inheritance, it can be implemented by 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 subclasses must provide the implementation;
Visual inheritance is the ability of a subform (class) to use the appearance of a base form (class) and to implement code.
When considering using inheritance, it is important to note that the relationship between the two classes should be a "belongs" relationship. For example, the Employee is a person and the Manager is a person, so these two classes can inherit the People class. But the Leg class cannot inherit the person class, because the leg is not a human.
Abstract classes only define generic properties and methods that are created by subclasses, and the 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 a parent object to be equal to one or more of his child objects, after which the parent object can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type.
To achieve polymorphism, there are two ways, covering, overloading.
Override, which is the practice of redefining the virtual function of a parent class by a subclass.
Overloading (overload) means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (perhaps with different number of arguments, perhaps different types of arguments, or both).
In fact, the concept of overloading is not "object-oriented programming", the implementation of overloading is: The compiler according to the function of a different parameter table, 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. The invocation of these two functions has been determined between the compilers and is static (remember: Static). That is, their addresses are bound (early bound) at compile time, so overloading and polymorphism are irrelevant ! The true and polymorphic correlation is "overlay". When a subclass has redefined the virtual function of the parent class, the parent pointer is dynamically based on the different child-class pointers assigned to it (remember: it's dynamic!). Call to the function of the subclass, such that the function call cannot be determined during compilation (the address of the virtual function of the calling subclass cannot be given). Therefore, such a function address is bound at run time (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 silly, if it's not late binding, it's not polymorphic." ”
So what is the role of polymorphism? We know that encapsulation can hide implementation details and make code modular; Inheritance can extend existing code modules (classes); they are all designed to- code reuse . And polymorphism is for another 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, the specific use of 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 also polymorphic, 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 above two classes, a basic information class, implementation of the information class in the encapsulation and polymorphism of attributes, a student class implementation of the basic information class inheritance and the encapsulation of its own attributes, better explain its characteristics.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dark Horse Programmer--java Object-oriented features: encapsulation, inheritance, polymorphism