Java Basic Learning _ Object Oriented (bottom) 01_day08 summary

Source: Internet
Author: User

=============================================================================
=============================================================================
the knowledge points involved are:
1: Inheritance (Mastery)
(0) Java Inheritance Overview
(1) Definition of inheritance
(2) How is inheritance represented in Java? What is the format?
(3) Benefits of inheritance
(4) The drawbacks of inheritance
A: To enhance the coupling of the class. Such a change in a class will affect other classes related to that class.
B: Breaking the encapsulation.
(5) Characteristics of inheritance in Java
(6) Considerations of succession
the A:java class only supports single inheritance and does not support multiple inheritance.
multiple (Heavy) inheritance in B:java (inheritance system)
(7) When do you use inheritance?   
A: Inheritance embodies: is a relationship.
B: Adopt the Hypothesis method.
(8) member relationships in Java inheritance  
A: member variables
B: Construction Method
C: Member Method
(9) Method overrides: (method = member method)  
(10) method rewrite of the two-face question
the difference between A:override and overload? Can overload change the return value type?
What are the differences between B:this and super and their respective roles?
(11) data initialization of the face question  
A: Initialization process for a class
B: The construction of the child parent class execution Process
C: Initialization of the child parent class (hierarchical initialization)
(12) Succession cases  
A: Student and teacher case
B: Analysis and implementation of cat and dog cases
=============================================================================
=============================================================================
1: Inheritance (Mastery)
(0) Java Inheritance Overview  
(1) Inheritance definition: Extract the same members from multiple classes into a separate class. Then let these multiple classes have a relationship with that independent class, and these classes will have the content. This relationship is called inheritance.   
(2) How is inheritance represented in Java? What is the format?   
A: Use the keyword extends expression.     
B: format:    
Class subclass name extends parent class name {}
---------------------------------------
(3) Benefits of inheritance:  
A: improves the reusability of the code.     
B: improves the maintainability of the code.     
C: A relationship between classes and classes is a precondition for polymorphism.     
classes and classes have a relationship, but they are also a disadvantage of inheritance:
the coupling of the class is enhanced.
---------------------------------------
(4) Disadvantages of inheritance:  
A: To enhance the coupling of the class. Such a change in a class will affect other classes related to that class.     
principles of development and design: low coupling, high cohesion.
coupling: The relationship between a class and a class.
cohesion: The ability to accomplish something yourself.
B: Breaking the encapsulation.     
as we have said: Members of a class try not to let the outside world directly access, after the subclass inherits the parent class, then the parent class's encapsulation is broken.
---------------------------------------
(5) Characteristics of inheritance in Java:  
the A:java class only supports single inheritance and does not support multiple inheritance.     
that is, a class can have only one parent class, and there can be no more than one parent class.
class Father {}
class Mother {}
class Son exnteds Father {}//correct
class Son extends Father,mother {}//Wrong
Some languages support multiple inheritance, such as C + +. The format is: extends Class 1, Class 2,...
multiple (Heavy) inheritance in B:java (inheritance system)    
class A {}
class B extends A {}
class C extends B {}
---------------------------------------
(6) Considerations of succession:  
A: Subclasses cannot inherit private members (member variables and member methods) of the parent class.
In fact, this also embodies another disadvantage of inheritance: breaking the encapsulation.
as we have said: Members of a class try not to let the outside world directly access, after the subclass inherits the parent class, then the parent class's encapsulation is broken.
B: Subclasses cannot inherit the constructor of the parent class, but can access the parent class construction method through the Super keyword.     
because the construction method is special, not to inherit, the construction method is to initialize the object data.
C: Do not inherit for part of the function.     
For example:
class A {
Public void Show1 () {}
Public void Show2 () {}
}

class B {
Public void Show2 () {}
Public void Show3 () {}
}

//We found that the Show2 () method is the same as Class A in class B, so we use inheritance to represent it.
class B extends A {
Public void Show3 () {}
}
This is actually not good, because you have not only show2 (), but also more show1 ().
It is possible that show1 () is not what you want.
---------------------------------------
(7) When do you use inheritance?   
A: Inheritance embodies: is a relationship.
For example:
Person
Student
Teacher
Fruit
Apple
Banana
Orange
B: Adopt the Hypothesis method.     
Suppose there are two classes, a, B. You can consider using inheritance only if they meet the one of A is B, or B is a.
---------------------------------------
(8) member relationships in Java inheritance:  
A: member variables
A: This is too simple when the member variable name of a subclass is not the same as the member variable name in the parent class.
B: When the member variable name of a child class is the same as the member variable name in the parent class, how does this access?
to access the lookup order of a member variable in a method of a subclass:
1. Find the local scope of the subclass method and use it.           
2. in the subclass of the member scope to find, there is the use.           
3. in the member scope of the parent class, use it.           
4. can not find, the error.           
B: Construction Method    
A: All constructor methods for subclasses will access the parent class's parameterless constructor by default.
because subclasses inherit data from the parent class, data from the parent class may also be used.
therefore, before subclasses are initialized, it is important to complete the initialization of the parent class data first.
Special Note: in fact, the first statement of each constructor method of a subclass is: Super ();        
B: If there is no parameterless construction method in the parent class (that is, only the constructor method is given in the parent class), what happens to the subclass's construction method?
Method 1: The subclass is constructed by super (...), which shows the constructor method that invokes the parent class.         
Method 2: The construction method of the subclass passes this ();/this (...); Call the other constructor methods of this class, but there must be a way to access the parent class in the other constructor methods of the subclass.         
Method 3: Let the parent class provide an parameterless construct.         

Precautions : this ();/this (...); Super (...); These three statements must be placed on the first sentence when accessing the constructor of a subclass or parent class.         
Otherwise, the parent class data may be initialized more than once.
C: Member Method    
A: This is too simple when the member method name of a subclass is different from the member method name in the parent class.
B: When the member method name of the subclass is the same as the member method name in the parent class, how does this access?
to access the lookup order of a member method through a subclass object:
1. Find in the member method of the subclass and use it.
2. Find in the member method of the parent class and use it.
3. Can not find, the error.
---------------------------------------
(9) Method overrides: (method = member method)  
method Overrides: a method in a subclass that is exactly the same as a method declaration in the parent class (the same method name, parameter list, and return value type) is also referred to as method overrides, method replication.     
method Overloading: The method name that appears in this class is different from the parameter list, regardless of the return value type.     

the use of method overrides is characterized by:    
if the child class and the parent class have different method names, the corresponding method is called.
if the child class and the parent class have the same method name, the child class will end up using its own method.
methods to override the application:    
when a subclass needs the functionality of a parent class, and the body subclass of the feature has its own content, you can override the methods in the parent class, which is to follow the functionality of the parent class and define the content that is unique to the subclass.
Considerations for method overrides:    
1. Private methods in the parent class cannot be overridden.       
because the parent class private method subclass cannot inherit at all.
2. When a subclass overrides a parent class method, the access permission cannot be lower. (The best permissions are consistent.) )      
The default permissions are less than the permissions of public.
3. The static method of the parent class, and the subclass must also be overridden by a static method.       
(In fact, this is not the method of rewriting, but the phenomenon is true, as for why not the method rewrite, polymorphic I will explain.) )
Summary: when subclasses rewrite the parent class method, it is best to declare exactly the same.       
---------------------------------------
(10) method rewrite of the two-face question:
the difference between A:override and overload? Can overload change the return value type?     
method Overrides:      
In a subclass, a behavior that is identical to the method declared in the parent class appears. (includes method name, parameter list, and return value type)
method overloads:     
in the same class, the method names that appear are the same, and the argument lists are different, regardless of the return value type.
the method overload can change the return value type because it is independent of the return value type.       
Override: Method Overrides
Overload: Method Overloading
What are the differences between B:this and super and their respective roles?     
This : represents an object reference for the current class.       
Super: represents the identity of the parent class storage space. (It can be understood as a reference to the parent class, through which the members of the parent class can be accessed.) )      
Application Scenarios:      
member variables:
This . Member Variable
Super. Member Variables
Construction Method:        
This (...)
Super (...)
member Methods:        
This . Member Method
Super. Member Methods
---------------------------------------
(11) data initialization of the face question  
A: Initialization process for a class    
Initialize the member variables first:
Default Initialization
Display Initialization
constructor Method Initialization
B: Constructor method of the child parent class the execution of the hair    
all constructor methods of a subclass are accessed by default to access the parent class's parameterless construction method.
C: Initialization of the child parent class (hierarchical initialization)    
initializes the parent class first, and then initializes the child class.
---------------------------------------
(12) Succession cases:
A: Student and teacher case   
Pre-succession cases
post-inheritance case
B: Analysis and implementation of cat and dog cases    
when doing design, first find the specific things, and then find the specific things in common, and then extract a parent class.
=============================================================================

Java Basic Learning _ Object Oriented (bottom) 01_day08 summary

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.