The No. 05 Chapter: Object-Oriented (advanced)

Source: Internet
Author: User

The No. 05 Chapter: Object-Oriented (advanced)3: Code block (understanding)
(1) Code enclosed in {}.
(2) Classification:
A: Local code block
Used to limit the life cycle of variables, release early, and improve memory utilization.
B: Building Blocks of code
The same code in multiple construction methods can be put here, before each construction method executes, the construction code block is executed first.
C: Static code block
Initializes the data for the class and executes only once.
(3) Static code block, construct code block, construct method order problem?
Static code blocks > Constructing code blocks > Construction Methods

4: Inheritance (Mastery)
(1) The same members in multiple classes are extracted to define a separate class. Then let the multiple classes and the independent class produce a relationship,
These are the things that these classes have. This relationship is called inheritance.
(2) How is inheritance represented in Java? what is the format?
A: Use keyword extends to express
B: Format:
Class subclass name extends parent class name {}
(3) Benefits of inheritance:
A: Improved reusability of the code
B: Improved maintainability of the code
C: Let the class and the class have a relationship, is the precondition of polymorphism
(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.
Principle: Low coupling, high cohesion.
Coupling: Class-to-class relationships
The ability to accomplish something by oneself
B: Breaking the encapsulation
(5) Characteristics of inheritance in Java
Class in A:java only supports single inheritance
Multiple (Heavy) inheritance in B:java (inheritance system)
(6) Considerations of succession:
A: Subclasses cannot inherit private members of parent class
B: Subclasses cannot inherit the constructor of a parent class, but can access it through super
C: Do not inherit for part of the function
(7) When do I use inheritance?
A: Inheritance embodies: is a relationship.
B: Using the Assumption method
(8) Member Relationships in Java inheritance
A: Member variables
A: The member variable name of the subclass is not the same as the member variable name in the parent class, this is too simple
B: The member variable name of the child class is the same as the member variable name in the parent class, how is this accessed?
The method of the subclass accesses the lookup order of the variables:
Find the local scope of the subclass method and use it.
In the subclass of the member scope to find, there is the use.
In the member scope of the parent class, it is used.
If you can't find it, you'll get an error.
B: Construction method
A: The constructor of a subclass is accessed by default to access the parent class's parameterless construction method
is for subclasses to access the initialization of the parent class data
B: What if there is no parameterless construction method in the parent class?
Subclasses explicitly invoke a parameter construct with super
Subclasses use this to invoke other constructs of their own, but there must be a construct that accesses the parent class.
To have the parent class provide an argument-free construct
C: Member Method
A: The member method of the subclass is not the same as the member method name in the parent class, this is too simple
B: The member method of the subclass is the same as the member method name in the parent class, how is this accessed?
To access the lookup order of a method through a subclass object:
Look in the subclass and use it.
Look in the parent class and use
If you can't find it, you'll get an error
(9) Two face questions:
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?
(10) Data initialization of the face question
A: Initialization process for a class
B: The construction of the child parent class execution process
C: Hierarchical initialization
(11) Case:
A: Student and teacher case
Before inheriting
After inheritance
B: Analysis and implementation of cat and dog cases


1:final Keywords (master)
(1) is the final meaning, can be modified class, method, variable.
(2) Features:
A: It modifies the class and cannot be inherited.
B: It modifies the method and cannot be rewritten.
C: The variable it modifies is a constant.
(3) Interview Related:
A: Local Variables
A: The base type value cannot be changed
B: The reference type address value cannot be changed, but the content of the object can be changed
B: Timing of initialization
A: can only be initialized once.
B: Common given values
Definition of the time. Recommended
Constructs the method.

2: Polymorphism (mastering)
(1) The different states that the same object manifests at different times.
(2) The precondition of polymorphism:
A: There is a succession or realization of the relationship.
B: There are methods to rewrite.
C: There is a parent or parent interface reference to the child class object.

Classification of polymorphic:
A: Specific types of polymorphism
Class Fu {}
Class Zi extends Fu {}

Fu f = new Zi ();
B: Abstract polymorphic
Abstract class Fu {}
Class Zi extends Fu {}

Fu f = new Zi ();
C: Interface polymorphism
Interface Fu {}
Class Zi implements Fu {}

Fu f = new Zi ();
(3) Features of member access in polymorphic states
A: Member variables
Compile look left, run look left
B: Construction method
The construction of the subclass will have access to the parent class construction by default
C: Member Method
Compile look left, run look right
D: Static method
Compile look left, run look left

Why?
Because the member method has overrides.
(4) The benefits of polymorphism:
A: Improve the maintenance of code (inheritance embodies)
B: Improve code Extensibility (polymorphic representation)
(5) The disadvantages of polymorphism:
The parent cannot use the unique features of the child.

Phenomenon:
A child can be used as a parent, and the parent cannot be used as a child.
(6) Transformation in polymorphism
A: Upward transformation
From child to Parent
B: Down transformation
From parent to Child
(7) The case of Confucius pretending to be a father helps us understand polymorphism
(8) Multi-state exercises
A: Cat and Dog case
B: Teacher and student case


3: Abstract class (Master)
(1) To extract a number of common things into a class, this is the practice of inheritance.
But there are many things that are common, in some cases, method declarations, but method bodies.
That is, the method declaration is the same, but each specific object is not the same when the concrete implementation.
Therefore, when we define these common methods, we cannot give the concrete method body.
A method that does not have a concrete method body is an abstract method.
In a class, if there is an abstract method, the class must be defined as an abstract class.
(2) Characteristics of abstract classes
A: Abstract classes and abstract methods must be modified with the keyword abstract
B: Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be abstract classes
C: Abstract class cannot be instantiated
D: Subclass of Abstract class
A: is an abstract class.
B: is a specific class. This class must override all abstract methods in the abstract class.
(3) Characteristics of abstract class Members:
A: Member variables
There are variables, there are constants
B: Construction method
There are construction methods
C: Member Method
There is abstraction, there is non-abstract
(4) Practice of abstract classes
A: Cat and dog case exercise
B: Teacher Case Exercises
C: Student Case Exercise
D: Employee Case Exercise
(5) Some small problems of abstract class
A: Abstract class has a construction method, can not be instantiated, then what is the use of the construction method?
Initialization for subclass access to the parent class data
B: What is the use of a class if it has no abstract method but is defined for abstract classes?
To keep the object from being created
C:abstract cannot coexist with which keywords
A:final Conflict
B:private conflict
C:static no meaning


4: Interface (Master)
(1) Looking back at cat and dog cases, they only provide some basic functions.
For example: Cat drill fire ring, dog high jump and other functions, not the animals themselves have,
Is trained in the back of the training, this extra function, Java provides an interface representation.
(2) Features of the interface:
A: interface with keyword interface decoration
Interface interface Name {}
B: Class implementation interface with implements modification
Class name implements interface name {}
C: interface cannot be instantiated
D: Implementation class of the interface
A: is an abstract class.
B: is a specific class that must override all the abstract methods in the interface.
(3) member features of the interface:
A: Member variables
can only be constants
Default modifier: public static final
B: Construction method
No construction method
C: Member Method
It can only be abstract.
Default modifier: Public abstract
(4) Classes and classes, classes and interfaces, interfaces and interfaces
A: Classes and classes
Inheritance relationship, can only be single-inheritance, multi-layer inheritance
B: Classes and Interfaces
The implementation of the relationship, can be implemented alone, or more.
You can also implement multiple interfaces while inheriting a class
C: Interface and interface
Inheritance, can be single-inheritance, or multiple inheritance
(5) The difference between abstract class and interface (self-completion)?
A: Member Differences
Abstract class:
Interface:
B: Relationship differences:
Classes and classes:
Classes and Interfaces:
Interfaces and interfaces:
C: Different design concepts
Abstract class: Is a, which is defined in the abstract class as a generic function.
Interface: Like a, an interface that defines an extension function.
(6) Practice:
A: Cat and Dog case, join the high jump function
B: Teacher and student case, add smoking function 0

6: Inner Class (understanding)
(1) The class is defined inside another class, which is called an inner class.
Example: Class B is defined in Class A, and Class B is called an inner class.
(2) Access rules for internal classes
A: Members of external classes can be accessed directly, including private
B: External class to access internal class members, you must create an object
(3) Classification of internal classes
A: Member Inner class
B: Local inner class
(4) member inner class
A:private for the security of the data
B:static for ease of access

The member inner class is not static:
The external class name. Internal class Name Object name = new External class name. New internal class name ();
The member inner class is static:
The external class name. Internal class Name Object name = new External class name. Internal class name ();
(5) Questions of inner class of members (fill in the blanks)
30,20,10

Class Outer {
public int num = 10;

Class Inner {
public int num = 20;

Public Viod Show () {
int num = 30;

SYSTEM.OUT.PRINTLN (num);
System.out.println (This.num);
System.out.println (Outer.this.num);
}
}
}
(6) Local inner class
A: Local internal class access local variables must be final decorated.
B: Why?
Because local variables are used to disappear, the data for heap memory does not disappear immediately.
So, the heap memory is still using that variable, and the amount of change is gone.
In order for the value to exist, the final decoration is added.
With the anti-compilation tool, we see that, when final is added, the heap memory stores the value directly, not the variable name.
(7) Anonymous inner class (master)
A: A simplified form of a local inner class
B: Prerequisites
There is a class or interface
C: Format:
New class name or interface name () {
Override method;
}
D: Nature:
is actually a subclass of anonymous objects that inherit the class or implement an interface
(8) Use of anonymous internal classes in development
When we develop, we see abstract classes, or interfaces as parameters.
And this time, we know that what is actually needed is a subclass object.
If the method is called only once, we can use the format of the anonymous inner class to simplify.

Interface Person {
public abstract Void Study ();
}

Class Persondemo {
public void Method (person p) {
P.study ();
}
}

Class Persontest {
public static void Main (string[] args) {
Persondemo PD = new Persondemo ();
Pd.method (new person () {
public void Study () {
System.out.println ("Good study, day up");
}
});
}
}

(9) Anonymous inner class of face question (completion code)
Interface Inter {
void Show ();
}

Class Outer {
The code is padded
public static Inter method () {
return new Inter () {
public void Show () {
System.out.println ("HelloWorld");
}
};
}
}

Class Outerdemo {
public static void Main (string[] args) {
Outer.method (). Show (); "HelloWorld"
}
}

The No. 05 Chapter: Object-Oriented (advanced)

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.