Inheritance of three features of object-oriented (extends) -- Java notes (6), extendsjava

Source: Internet
Author: User
Tags export class

Inheritance of three features of object-oriented (extends) -- Java notes (6), extendsjava
Inheritance: a general-to-special relationship is an extended relationship. The subclass object is a type of parent class, also known as the generalization of the "is a" link: the process of extracting the commonalities in the subclass into the parent class is special: The subclass defines its unique behavior feature process format on the basis of the parent class: modifier class SubClass extends SuperClass {} SubClass: Export class, inheritance class, SubClass SuperClass: parent class, base class, SuperClass, and source class sub-classes SubClass inherit parent class SuperClass inheritance through extends inheritance improves code reusability, this improves the efficiency of software development so that the relationship between classes is generated. This is the prerequisite for polymorphism. When a subclass object accesses instance members, it is found in the subclass, if the parent class cannot be found, and each class has only one direct parent class, and the other class is not displayed, the default direct parent class is the object class. Once a class shows that it inherits other classes, in this case, the default direct parent class object will be canceled. Java only supports single inheritance and does not support multi-inheritance of private objects in the parent class. Subclass cannot inherit from the inheritance operation, for subclass Object Instantiation, The subclass object must first call the constructor in the parent class before instantiation, and then call its own constructor to prove that:

1 class Super {2 public Super () {3 System. out. println ("My parent class super"); 4} 5} 6 class Sub extends Super {7 public Sub () {8 System. out. println ("I'm a subclass sub"); 9} 10} 11 public class InstanceDemo {12 public static void main (String [] args) {13 new Sub (); 14/** 15 * I am a parent class super16 * I am a subclass sub17 * 18 * first print 19 in the printed subclass of the parent class * From this we can see that it is first call the constructor of the parent class to create the parent class object, and then call the constructor 20*21 */22} 23 in the subclass}
Before initialization, The subclass object will call the constructor without parameters of the parent class by default. However, once the subclass shows that other constructor methods of the parent class are called, the constructor called by default will be overwritten by the canceled method ): when a method of the parent class is not suitable for the feature behavior of the Child class, the method that should be changed in the parent class should be rewritten according to the principle: Method signature (method name + parameter list) the Return Value Type of the same subclass method must be smaller than the return value type of the parent class method or equal. The exception thrown by the subclass method declaration should be smaller or equal than the exception thrown by the parent class method declaration, that is, the subclass method cannot throw a new exception type. The subclass method can declare that multiple parent classes are thrown and the exception subclass is thrown (except RuntimeException) the access permission of the subclass method should be greater or equal than that of the parent class method overload and override

 

  Differences Overload) Override)
1 Judgment Rules Different parameter lists have different order, types, and numbers, which can make up the same type of Chinese method names for heavy loads. However, the parameter lists for different methods are independent of the return value type and modifier of methods. Method signature (method name + parameter list) the Return Value Type of the same subclass method must be smaller than the return value type of the parent class method or equal. The exception thrown by the subclass method declaration should be smaller or equal to the access permission of the subclass method declaration. greater or equal than the parent class Method
2 Permission No permission requirements The method to be rewritten cannot have more strict permissions than the parent class.
3 Range Occurs in a class Occurs in the inheritance relationship
4 Polymorphism Polymorphism during compilation Runtime polymorphism
A constructor cannot override the super keyword to indicate the default reference of the parent class object. If the subclass needs to call the instance method that the parent class is overwritten, super can be used as the caller to call the instance method covered by the parent class. Using super to call the parent class method, super must be the first statement in the constructor. this cannot be used. appears in the static modifier method, similarly, super cannot appear in the static modifier method because the static modifier method belongs to the class, that is, the caller is a class. this and super
  Differentiation This Super
1 Use Call fields or methods in this class Calls a field or method of the parent class from a subclass.
2 Construction You can call the constructor in this class, and one constructor must be used as an exit. The constructor that calls the parent class from the subclass. No matter how the sub-class is arranged, the constructor that calls the parent class without parameters is called by default.
3 Request Place it in the first line of the constructor when calling its own Constructor When calling the parent class constructor, place it in the first line of the subclass constructor.
4 Special Indicates the current object Parent Class Object
Ps: super () and this () cannot be displayed simultaneously in the same constructor.

 


How can we easily understand the three features of Java object-oriented '?

Encapsulation:
First, attributes can be used to describe the characteristics of a class of things. behavior can describe the operations that a class of things can perform. encapsulation refers to the commonality of the same class of things (including attributes and behaviors) to a class for ease of use. for example, a person can be encapsulated in the following method:
Persons {
Age (attribute 1)
Height (attribute 2)
Gender (attribute 3)

Work (Behavior 1)
Walking (behavior 2)
Voice (Act 3)
}

Inheritance:
Because of encapsulation, all the descriptive information of a class of things with common characteristics is classified into one class. However, we know that this is not omnipotent. Some things have commonalities, but there are still differences, for example, teachers are simply encapsulated as follows:
Instructor {
Age (attribute 1)
Height (attribute 2)
Gender (attribute 3)

Work (Behavior 1)
Walking (behavior 2)
Voice (Act 3)

Teaching (behavior 4)
}
The encapsulation of "Teachers" is basically the same as that of "people", but it only has one more characteristic behavior: teaching,
Teachers share the same characteristics as people, but we cannot say "people teach", that is, we cannot encapsulate teaching in "people". Teaching is one of the characteristic actions of teachers. to facilitate the encapsulation of teachers (code reuse, this is only one of the reasons for inheritance), teachers can be allowed to inherit, such:
Instructor extends {
Teaching (Act 3)
}
In this way, we do not need to redefine the attributes and behaviors encapsulated by the "person" class, but only need to use the inheritance method, expand the teacher's proprietary behavior on the basis of people, that is, "Teaching" can describe the teacher, that is to say, teachers also possess all attributes and behaviors encapsulated in "persons" and their own characteristic behaviors "Teaching ".

Polymorphism:
The concept of polymorphism is developed on the basis of encapsulation and inheritance (in fact, I think abstraction is also one of the major characteristics of object-oriented. To encapsulate, abstraction is necessary)

A simple understanding of polymorphism, such:
The human class encapsulates many features common to humans,
A teacher is a child class of a person and inherits his/her attributes and behaviors. Of course, a teacher has his/her own characteristic behaviors, such as teaching;
Students are child classes of people and inherit the attributes and behaviors of people. Of course, students have their own characteristic behaviors, such as learning and doing homework;

Now, when we need to describe the actions of teachers and students, we can separate them to say "teachers are teaching" and "students are doing homework ", but if we want to describe their behaviors from an abstract perspective, that is, from the perspective of both the teacher and the student's parent class "person", how do we describe them? "Persons are teaching "? "People are doing homework "? Is this strange and inappropriate? The problem is that we use abstract-level "people" for the behavior subject, but for the behavior itself, we use the specific Dongdong "Teaching" and "Teaching ". how can this problem be solved? Then we need to solve the abstract and specific contradictions.
Since we describe the behavior from the abstract point of view, can we describe the behavior at the same time? For example, "people are doing things" (both teachers and students are doing things as adults). This solves the conflict between abstract levels and specific levels.

In this step, we can describe the following two parts: "teachers are doing things" and "students are doing things ",
Then, we can call the instructor's own characteristic behavior in the "teacher"'s "work" Behavior & quo ...... the remaining full text>

How can we easily understand the three features of Java object-oriented '?

A class is a template for creating objects. An object is a specific instance of a class. This is the relationship between classes and objects.
You must first have a class to have an object! After defining a class, you can create the class object. Class defines attributes and methods, and attributes are member variables, which can be used to store object states. methods can be used to create object behavior models. For example: Let's say cat, what is a cat? A cat is a class, so what is an object? This cat is an object. Specifically, a cat is an object. A cat has a hair and a color. Then the cat is a black cat, and the cat is a white cat, the hair and color belong to the attributes of the cat. Therefore, the attributes can be used to store the object state. That is to say, the attributes show the object state. Different objects are separated by different attribute values. Methods: catch mice, black cats, white cats, and so on. The methods are inseparable from them. Therefore, the methods are only the actions of objects. These objects of a class have the same behavior, however, because their attributes can be different, we can influence the effect of the method through attributes: both me and you belong to humans, and we are two objects of the human class, we all have these attributes, but the attribute values are different. My mouth is big and your mouth is small, so I eat faster than you. Eating this behavior is what we share, but I eat faster than you, because different attribute values affect the internal operation of the method, and I call this method both, the return value may be different.

Object-oriented thinking is actually not difficult. It is very close to our real life. It is equivalent to transforming our cognition of the real world into programming language through a small transformation, so the object-oriented approach is closer to our human thinking. Do you think about the example above? So don't feel that you are away from the direction. You can use your natural way of thinking to think about it, but we may not get used to it, thinking said very well in Java: "We may be overwhelmed by this simplicity." A little good. Maybe we are too accustomed to the process-oriented programming ideas, so it takes a long time to get used to it. I guess the landlord is just learning, so don't force yourself to master all the details in a short time, I can't do it. I just want to write more code. It's good to look at the pictures and pictures. It's all imitated at the beginning. When I write more, I will suddenly wake up, oh ~ This is the case, so the landlord may wish to think like this when writing a program:
Object-oriented thinking, you can see objects one by one, so when solving a problem:
1) First, consider which classes and objects should be available for this problem (the relationships between objects and classes have been mentioned above ).
2) then consider the attributes and methods that these classes and objects should have.
3) consider the relationship between classes.
If you think too much about it, it is an object-oriented thinking.
Three object-oriented features: encapsulation, inheritance, and polymorphism. The so-called encapsulation means to abstract the essential features of a thing by defining the class and adding access control to the class attributes and methods. Inheritance refers to code reuse, while polymorphism separates interfaces and implementations from another perspective.
Object-oriented code has better reusability, scalability, maintainability, and ,.... And so on.

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.