Java Study For Seven Day (Object-Oriented 3)

Source: Internet
Author: User

--- Restore content start ---

Inheritance

Student "good study" Worker "good work"View Code

1. Extract the same features and behaviors of many classes and describe them using one class. Let multiple classes have a relationship with this class

In this case, multiple classes can omit a lot of code. This relationship is inherited, represented by the Java keyword extends.

2. inherited Architecture

A. Multiple specific objects are constantly extracted from shared content to form A system called inheritance system.

B. Learning and Principles of the inheritance system

Learn the top-level content. The reason is that it is the common content of the system.

Create a subclass. That is, use the underlying object

3. inheritance features

In Java, it can only be a single inheritance.

Java can be inherited in multiple layers.

4. Benefits of Inheritance

The emergence of inheritance improves code reusability

Inheritance creates a relationship between classes and is a prerequisite for polymorphism.

5. Relationship between Members in the Child parent class

A. member variables

When using a variable in the subclass method, first find the variable in the local variable of the method.

Otherwise, find the member variable in this class. If yes, use it. Otherwise, find the member variable in the parent class. If yes, use it. Otherwise, an error is returned.

B. Member Methods

When using a subclass object, first find this method in the subclass. If yes, use it. Otherwise, in the parent class

Find this method. If yes, use it. Otherwise, an error is returned.

Differences between rewriting and overloading

Overload: in the same class, the method name is the same but the list of parameters is different, and the return type cannot be used as the basis for judgment.

Rewrite: in different classes (in the Child parent class), the method declaration (the return type, method name, and parameter list must be consistent ).

Note the following before Rewriting:

The access permission of the subclass method must be greater than or equal to that of the parent method.

Static data can only be rewritten.

Constructor

Child column instantiation Process

When a subclass creates an object, it first creates the object of the parent class. By default, it calls the five-parameter constructor of the parent class.

Note: In the subclass constructor, the first line is super by default because it inherits the use of members of the parent class.

These members must be initialized. Therefore, a parent class object will be created first. Therefore, when the parent class has no parameter constructor, you must use this or super

Call other constructors.

6. Differences between this and super

This indicates the reference of the object of this class, and super indicates the bucket of the parent class.

Num = 4 "vb" Zi num = 5 "java" = Zi (); System. out. println (zi. num + "... "+" number "NewTel" name "" picture"View Code

Final keywords

1. The final meaning can be used to modify classes, methods, and variables.

2. The final modified class cannot be inherited. The modified method cannot be rewritten. The modified variable is a constant and can only be assigned once.

Internal classes can only access local variables modified by final.

X = 3; y = 4; y = 4; pai = 3.14; SubDemoView Code

Abstract class

1. Multiple classes have the same method declaration, but the method bodies are different. In this case, you can consider extracting the method declaration so that the subclass can inherit it.

There is no method body to implement the method body, so we need to use the abstract sign. The keyword is abstract

2. abstract class

This method is called an abstract method. classes that contain abstract methods are abstract classes.

3. Characteristics of abstract classes

Abstract classes and abstract methods must be modified with abstract.

Abstract classes cannot be instantiated.

Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be abstract classes.

4. Data features in abstract classes

Member variables

The abstract class can contain constants or variables.

Member Method

Abstract classes can have abstract methods or non-abstract methods.

Constructor

An abstract class is a class. All constructors can be instantiated but cannot be instantiated.

5. Problems in abstract classes

A. constructor in the abstract class cannot be instantiated but can be instantiated by the subclass.

B. Can abstract keywords coexist with those keywords?

Private content cannot be inherited, so it cannot be rewritten, but the abstract modifier method must be overwritten, so conflict

The final modifier method cannot be overwritten or conflicted. static if an abstract method can be modified through static, this method can be called directly by class name. Abstract methods do not have a method body. Such a call is meaningless. Therefore, static modification is not allowed.

C. Can there be no abstract methods in abstract classes?

Abstract classes do not provide abstract methods, just to prevent other classes from creating this abstract class object. This is embodied in awt.

X = 3; y = 4; y = 4; pai = 3.14; SubDemo *. name =. id =. pay = Manage pay ,. bonus = "manage work" Pro "pro work"View Code

Interface

1. When methods in a class are abstract, Java provides another representation, called interface

The relationship between classes and interfaces is represented by the keyword implements.

NUM = 3 Test Demo B C D =View Code

2. Features of interface members

Member variables

Is a constant. The default value is public static final.

Member Method

All are abstract. The default value is public abstract.

3. Relationship

Relationship between classes

It is an inheritance relationship. Classes and classes can only be individually inherited and can be inherited in multiple ways.

Relationship between classes and interfaces

Yes. Multiple classes can implement interfaces. Classes inherit one class and can implement multiple interfaces.

Relationship between interfaces

Yes. interfaces can inherit multiple interfaces.

4. interface features

Externally exposed rules are extended functions, and the appearance of interfaces reduces coupling (relationships between classes)

5. Differences between interfaces and abstract classes

Abstract classes can only be single inheritance, and interfaces can be implemented multiple times, avoiding the limitations of Multi-inheritance.

Data features in abstract classes:
Member variable: it can be a variable or a constant.
Member method: it can be an abstract method or a non-abstract method.
Constructor: Constructor

Interface data features

Member variable: constant public static final
Member Methods: all are abstract methods. All have the default modification public abstract
Constructor: No Constructor

Abstract Classes define the common functions of the inheritance system, while interfaces define extended functions of the inheritance system.

The abstract class is inherited from the is a relation: XX is the middle of XX, and the interface is implemented as the like a relation: XX is a type of XX.

"Sleep" ZhangSan Student LiSi WangWu SporterView Code

Appendix:

Use of the Random class

Number of random numbers

Master content

Constructor

Random () creates a new Random number generator.

Random (long seed) uses a single long seed to create a new Random number generator

Member Method

Int nextInt () returns the next pseudo-random number, which is the int value uniformly distributed in the sequence of the random generator.

Int nextInt (int n) returns a pseudo-random number, which is an int value that is evenly distributed between 0 (inclusive) and the specified value (excluded) from the sequence of the random number generator.

Usage of the category class

1. input data from the keyboard

2. Master content

Constructor

Extract (inputStream source) to construct a new shard. The generated value is scanned from the specified input stream.

Example: required SC = new partition (System. in );

Method Summary

SC. nextInt () to obtain Integer Data

SC. nextLine

--- Restore content end ---

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.