Java Learning-Basic knowledge next day-notes

Source: Internet
Author: User

Today's content
Inherited
Anonymous object and final keyword
Abstract class

Inherited
Overview of Inheritance:
The inheritance in the real world is the inheritance of the son's father.
Inheritance in the Java language causes a relationship between a class and a class (the child parent class), which can inherit the non-private members of the parent class (the private member cannot be inherited, the constructor cannot be inherited or overridden, and can be called by the Quilt Class)

The origin of the inheritance system:
Multiple classes have common member variables and member methods, the common part is extracted upward into the parent class, and then multiple classes inherit the parent class, which forms the inheritance system

Inherited format:
Class Subclass extends Parent class {
}

Note: You need to use the extends keyword

Characteristics of Inheritance:
(1) Classes in Java support only single inheritance, not multiple inheritance
Class a{}
Class b{}
Class C extends a,b{}//error, which is multiple inheritance
(2) Classes in Java support multiple layers of inheritance
Class a{}
Class B extends a{}
Class C extends b{}
(3) Subclasses and parent classes are a relative concept
(4) A class can be inherited by multiple classes
Class a{}
Class B extends a{}
Class C extends a{}

Characteristics of member variables in inheritance:
Follow the principle of proximity: Who to use when you are close to me
Characteristics of member methods in inheritance:
Follow the principle of proximity
The method in the parent class is not available in the subclass
If this method is overridden in a subclass, the following method is used when the subclass is overridden


This and Super keywords:
This: Represents a reference to the current object
Super: It represents the space of the parent class.

This
(1) When the local variable and the member variable have the same name, this is the way to identify that this is a member variable
(2) Calling member variables and member methods in this class
(3) The first line of this (parameter list) in the constructor method accesses the other constructor methods in this class
Super
(1) Calling member variables and member methods in the parent class in a class
(2) In the constructor method, the first row super (argument list) calls the construction method of the parent class

The execution order of the constructor methods in the inheritance:
The constructor of the parent class is executed first, and then the constructor of the subclass is executed.

The first row of each construction method has a super () by default, which calls the Null parameter construction method of the parent class
If you manually give code such as this (parameter) or super (parameter), call the other constructor methods in this class manually, or the constructor of the parent class, the default super () has no

Differences between method overrides (override) and overloads (overload)
Overloading: In a class, methods with the same method name, different parameter lists (different numbers, different types, different order), and other (return value) Independent
Override: In two classes (child parent Class), the method of the subclass is re-implemented by the method that inherits from the parent class, and the declaration of the method generally does not change

overriding considerations:
Parent-class Private methods cannot be overridden
The construction method of the parent class cannot be overridden

Two small
The return value type of a method overridden by a subclass is less than or equal to the return value type of the parent class method
Subclass overridden by method throws an exception that is less than or equal to (less than equals) the exception thrown by the parent class method
Two identical
Method name is the same
Same argument list
A large
Subclasses override permissions for methods that are greater than equal to the parent class method
public>protected> default >private

Advantages and disadvantages of inheritance
Advantages:
Improve Code reusability
Improve the maintainability of your code
Disadvantages:
Coupling enhancement

Anonymous object and final keyword
Anonymous objects
Student s = new Student ();
New Student ();//Anonymous Object

Application Scenarios:
When the object is used only once

(1) Case where only one method is called
New Student (). study ();
(2) When invoking a method as an argument pass for a method
Method (New Student ());
(3) as the return value of the method
public static Student method (Student s) {
return new Student ();
}

Final keyword
Final is the ultimate meaning that you can modify classes, methods, and variables

Final modified class, which cannot be inherited
Final decoration method, the method cannot be overridden
Final modified variable, which cannot be re-assigned, becomes a constant

Abstract class
The origin of abstract classes
Multiple classes have common content, the content of the general extraction, to get the parent class, the specific subclass of the function is specific, but in the parent class can not clear the specific function
, since it is not clear that we would simply not write the method body, and the method without method body must be decorated with the abstract keyword, this is called abstract method, and there is
The class of an abstract method must be declared as an abstract class.

Features of abstract classes:
(1) If there is an abstract method in a class, the class must be an abstract class
(2) Abstract and abstract methods are all modified with the abstract keyword
(3) Abstract classes cannot create objects (cannot be instantiated)
(4) The relationship between the general class and the abstract class is the inheritance relationship, the abstract class is generally the parent class, the ordinary class is generally inherited abstract class
(5) A class inherits an abstract class, must implement all abstract methods in the abstract class, or the class must also be an abstract class
(6) Abstract classes can have non-abstract methods

Characteristics of members in abstract classes:
Member variables
can have
Member Methods
Abstract methods that can have
Non-abstract method, can have
Construction method
Yes

Which keywords cannot coexist with the abstract keyword?
Final
Private
Static


Java Learning-Basic knowledge next day-notes

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.