The concept of inheritance, encapsulation, polymorphism and the basis of distinguishing __java

Source: Internet
Author: User
Tags inheritance instance method

One, the three main characteristics of object-oriented:

1. Encapsulation: is to encapsulate the objective things into abstract classes, and the class can put its own data and methods to allow only trusted classes or objects to manipulate the untrusted information to hide. Encapsulation is one of the characteristics of a class, it should be well understood, like some private, other classes can not be accessed Ah, have access to, the more secure.

2. Inheritance: Refers to the ability to use all the features of an existing class and to extend them without having to rewrite the original classes.     Inheritance has three ways to implement inheritance, interface inheritance, and visual inheritance. 3. Polymorphism: Simply put, a pointer to a subclass type is allowed to be assigned to a pointer to a parent class type. To achieve polymorphism, there are two ways to override, method overload

Second, the succession

Inheritance is a mechanism for creating new classes from existing classes, meaning extending functionality on the basis of existing classes. Inheritance is divided into subclasses and parent classes, and classes can have two important members: member variables and methods. A subset of the members of a subclass is defined by its own declaration, and the other part is inherited from its parent class.

The subclass inherits all the properties and methods in the parent class, but for private signs and methods, because this is the privacy of the parent class, the subclass is inherited, but there is no reference to access the properties and methods, so it is equivalent to not inheriting to. Many times, it can be understood that there is no inheritance.

(1). A subclass inherits the member variables of the parent class as one of its own member variables, as if they were declared directly in a subclass, and you can manipulate any instance methods that you declare in a quilt class.

(2). A subclass inherits a method of a parent class as a method in a subclass, as if they were declared directly in a subclass, and can be called by any instance method that it declares itself in the quilt class.

1.Java only supports single inheritance, that is, only one parent class, using the extends keyword inheritance;

Class a{             }//definition Classes-A class
b{             }//defines class B class
C extends a,b{     //Is wrong because two parent classes are inherited at the same time          

multi-layer inheritance allowed in 2.Java

Class a{          }//definition Classes A class
B extends a{           }//b inherit a
class C extends b{}//c           both inherit B and a

3. When the subclass has the same method definition as the parent class (that is, the return type, method name, and parameter list are identical, only the method body is different), called the Method Rewrite (OverRide);

The method overrides should respect the "three same small one big" rule, the "three Same" means the same method name, the parameter list is the same, the return value type is the same. "One small" means that the exception class thrown by a subclass method declaration should be smaller or more equal than the exception class thrown by the parent class method declaration. "One large" means that the access rights of the subclass method should be greater or equal than the parent class method, and it should be noted that the overlay method and the overridden method are either class methods or both instance methods, not one class method, and the other is an instance method.

Class baseclass{public  
static void Test () {...}
}
Class Subclass extends baseclass{public
void Test () {...}}
 

(1) For an object created by a subclass, if the subclass overrides the method of the parent class, the Run-time system calls the method of the subclass override, and if the subclass inherits the method of the parent class (not overridden), then the object created by the subclass can also call the method, except that the method produces the same behavior as the parent class.

(2) You cannot reduce the access rights of a method when overriding a parent class method

(3) If you override the method of the parent class and use the overridden method of the parent class, you can use super in the method of the subclass. Overridden Method ();

(4) Subclass does not inherit the constructor method of the parent class

(5) The constructor method of a subclass must invoke the constructor method of the parent class

(6) If the parent class constructor is not explicitly invoked in the constructor of a subclass, and if the other constructed method of the overload is not invoked using the This keyword, then the system defaults to calling the parent class without parameters, equivalent to omitting the super ();

(7) If the parent class construction method is not explicitly called in the subclass constructor, and the parent class has only the constructor method with parameters, and there is no parameterless constructor, then the compilation error

(8) The constructor method of the parent class can be invoked using statement super (Argument_list) in the constructor of the subclass, but must be the first sentence of the constructor method.

(9) A subclass, when instantiated, actually invokes the constructor method of the parent class, and then the constructor of the subclass that is called.

Third, packaging

Encapsulation refers to the object's state information hidden inside the object, not allowing external programs to directly access the information inside the object, but through the method provided by the class to achieve internal information operations and access. Encapsulation is an object-oriented programming language simulation of the objective world, the objective world of attributes are hidden inside the object, the outside can not directly manipulate and modify.
The benefits of encapsulation are many:
1, hide the implementation details of the class
2, so that users can only through a predetermined method to access the data, so you can add control logic in the method, restricting the property of unreasonable access.
3, can carry on the data inspection, thus helps to guarantee the object information integrity.
4, easy to modify, improve the maintainability of the code.

Iv. Polymorphism

Polymorphism means that when a method of a parent class is overridden by its subclass, it can produce its own functional behavior. In simple terms, polymorphism is a feature of the ability to perform multiple forms. When a class has many subclasses, and these subclasses override a method in the parent class. So when we put a reference to an object created by a class that is in the object of a parent class, we get a transition object of that object, then the object on the turn may have multiple forms when calling this method.

Polymorphism is the parent class reference that can hold a subclass object. Only methods in the parent class can be invoked at this time, and the unique methods in subclasses are inaccessible. Because at this time (compile time) you see him as the reason for the parent object, but at runtime, the compiler will find that the parent class reference is originally a subclass of the image, so if the parent class and subclass have the same method, The invocation is a method in a subclass, not a parent class.

(1) For a variable of a reference type, the Java compiler handles it according to the type of its declaration

(2) For a variable of a reference type, the runtime Java Virtual machine is processed according to the object it actually references

1, subclasses override the parent class method

2, when writing a method, call the method defined by the parent class

3. At run time, depending on the type of object actually created, decide which method to use dynamically






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.