Java Fifth Week study

Source: Internet
Author: User
Tags define abstract modifier throwable

Relationships between classes and classes in Java

1. Aggregation relationship: Two classes have a low degree of association and can exist separately.

2. Combined relationship: Two are relatively high degree of association.

How to represent this relationship in Java.

A Class A as an attribute exists in category B.

3. Dependency: Two classes have a higher degree of association.

A method is defined in a class that takes another class as a parameter to the method.

4. Relationship: The way it is written and the relationship between the whole and the part is kind, the meaning of the expression is much the same.

5. Inheritance Relationship

One of the three main features of object-oriented: inheritance

Want to determine if there is an inheritance relationship between two classes?

By judging whether there is a relationship between the two classes. is a statement to judge

How to describe the inheritance relationship of two classes in Java:

Describe the inheritance relationship by extends key.

Format:

Class A extends b{}

Class A is called a subclass of Class B.

Class B is called the parent class of Class A (superclass, base class)

The role of inheritance: reduce duplication of code (how to optimize code)

The use of inheritance note points:

1. Do not inherit in order to save code. To use inheritance, there must be an inheritance relationship.

Inheritance in 2.java belongs to single inheritance. Only one parent class is allowed for a child class. Multiple inheritance is allowed.

3. The parent class is generally a member of the public (properties and behaviors) of the child class. Subclasses are behaviors and attributes that allow themselves to be used alone.

4. A private member of a parent class class cannot be inherited.

5. The construction method is also not inherited.

6. When a subclass creates an object, it calls the parent class's parameterless construction method by default.

7. There is a better way to construct a parent class that has no arguments.

8. If a child class and a member of the same name are in the parent class, the subclass is preferred by default.

9. If the constructor method for calling the parent class is specified in the subclass construction method, then no parameterless constructor method is called in the parent class.

Super keyword

Super: The space used to reference the parent class

The Super keyword app:

1. Used in subclasses to call directly the member variable in the parent class.

2. The constructor method of the parent class is called in the constructor of the child class.

The use of the super Call construction method is a point of note:

    1. If super is used to invoke the constructor of the parent class, then the super statement must be the first statement in the constructor of the child class.

The difference between the Super keyword and the This keyword:

1 meaning Comparison of keywords

The caller object of the method to which the 1.this keyword belongs.

2.super represents a reference to the parent class space

2 using the premise to compare

1.this keywords do not need inheritance or can be used

2.super keyword must have inheritance

3 Call construction Method comparison

The 1.this keyword is the constructor that invokes the current class

2.super the method of constructing a surrogate parent class.

Override of Method

Role: Extends the methods in the parent class.

Usage Scenario: Methods in the parent class do not meet subclasses

Note Points for method overrides:

1. A method in a subclass has the same name as a method in the parent class

2. The list of arguments in the subclass is the same as the parent class

3. The permission modifier for a subclass is greater than or equal to the permission modifier of the parent class.

4. The return value type is the same as the parent class, or is a subclass of the parent class.

Final modifier

Meaning: final, immutable.

Function: 1, can be used to modify variables

2. Can be used to modify the method

3. Can be used to modify the class

Use note points:

1. A variable must be initialized if it is final decorated

2. If a basic data type variable is final, the variable cannot be modified

3. If you use final to modify a reference data type variable, then this variable can no longer reference other objects

4. If the final decoration method, the method does not allow overrides.

5. If final modifies a class, then this class is not allowed to be inherited.

Abstract class

The usage scenario of an abstract method: There is a thing that has some kind of behavior, but no concrete implementation. Can be implemented by an abstract class.

Define abstract methods: Abstract keyword

Abstract method Definition Format: Abstract method name ();

Abstract class-defined format: Abstract class class Name {}

Notes on the use of abstract:

The 1.abstract retouching method is an abstract method that does not allow the body to be used.

2. If there is an abstract method in a class, then this class must be an abstract class.

3. If you inherit an abstract class from a class, you must override all the abstract methods in the abstract class.

4. An abstract class allows both abstract and non-abstract methods to coexist.

5. Abstract classes cannot create objects.

6. A constructor method can be defined in an abstract class.

7. There can be no abstract method in an abstract class.

The abstract modification method cannot be used with a keyword:

Abstract cannot modify the method with private.

Abstract cannot modify the method with static.

Abstract cannot be combined with final to modify the method.

The difference between an abstract class and a normal class:

1. Abstract class has the abstract keyword decoration, the ordinary class does not have

2. Abstract class has an abstract method, ordinary No.

3. Abstract classes cannot create objects, and ordinary classes can create objects.

Interface

Use the interface keyword to represent the interface.

Define the format of the interface:

Interface interface Name {Interface's contents}

Implementing the interface:

Implements: Indicates implementation

Class B implements interface name {

}

Note points for using the interface:

1. The method defined in the interface is not a principal. Methods in an interface are abstract methods by default.

2. The variables defined in the interface are constants by default. Adornment method: public static final.

3. Interfaces cannot define construction methods

4. The interface needs to be implemented with other classes.

5. If an ordinary class implements an interface, then all the methods in the interface must be implemented.

6. If an abstract class implements an interface, the class does not necessarily need to implement the methods in the interface.

7. Interfaces are implemented in many support. A class can implement multiple interfaces.

8. The interface cannot create an object.

The third major feature of Java object-oriented: polymorphism

Polymorphic forms of monetization: 1. The type of the parent class points to the object of the child class

2. Interface types that point to an implementation object of an interface type

1. The type of the parent class points to the object of the child class:

Example:

Animal a = new Dog ();

Notes on the use of polymorphism:

1. There must be inheritance to be polymorphic.

2. Rewriting of methods in polymorphism

3. A non-static method with the same name in the subclass and parent class is called by default to the method in the child class.

4. A variable with the same name in the subclass and parent class is called in the parent class by default.

5. A static method with the same name is in the subclass and parent class. The method in the parent class is called by default.

Summary: A member of the same name exists in a subclass and parent class, except for a non-static method, which is called in the parent class by default.

Applications for polymorphism:

1. Used in the method to pass the formal parameter. can enhance the scalability of the Code

2. For return values in a method, you can return multiple types of objects

(This is the time to use a data type conversion, before the data conversion needs to determine whether an object belongs to a type)

In the reference data type: Subclasses convert to parent class called automatic type conversion.

Example Aniaml a = new Dog ();

In the reference data type: The parent class is converted to a subclass called coercion type conversion.

Example: a instanceof Mouse

Mouse m = (Mouse) A

2. The variable of the interface type points to the object of the interface implementation class

Example:

Interface A = new implementation class ();

A You can implement the method inside the interface

Inner class

Definition: There is another class inside a class.

Naming of class files for inner classes: External class $ inner class

Usage scenarios for internal classes:

To use a class A in another Class B, Class B is also used in Class A, this time you need to use the inner class.

There are two types of classes within a class:

1 member Inner class

2. Local inner class

member Inner class

Definition inside the method outside the class is called the member inner class.

How to access the inner class:

Way One:

Provides a method in an external class to initialize an inner object, and to manipulate the object in a method.

Way two: Only members inner classes can be used.

Indirect creation of inner class objects in other classes is directly used.

Format: Outer class. Inner class object = new External Class (). New inner Class ();

Use note points for internal classes:

1. Members defined in the external class can be accessed directly in the inner class

2. If the member in the inner class has the same name as the member of the outer class, the nearest principle is called in the inner class.

3. If the members in the inner class have the same name as members of the external class, you can get the properties of the external class by using the name of the external class. this.

4. If static modifies the inner class, the members that are accessed in the inner class need to be statically.

Local inner class

The class that is defined in the method.

How to access local inner classes: Provides a method in an external class to initialize internal objects, and to manipulate objects in a method

Note points for using local inner classes:

    1. The inner class wants to access the local variables in the outer class, and this local variable must be decorated with final

Anonymous inner class

Anonymous inner class: is an inner class without a name.

The role of anonymous inner classes: simplifying code

Anonymous internal classes need to be implemented through inheritance or interfaces

Abnormal

Abnormal procedures and code, called exceptions

Exception class: Use a class to describe all exception conditions called exception classes

Anomaly System: All exception classes are combined to be called anomaly systems.

Object: So the exception and the wrong super class

Throwable: Superclass of all exceptions

Error: Wrong class

Execption: Exception class

Inheritance diagram:

Throwable: Three of the most common methods in a class

1 toString () returns a short description of this throwable: Describes the class's full class name + incoming exception information.

2 GetMessage () Gets the incoming exception information

3 Printstacktrace () print exception stack information

Error: Errors: Refers to errors caused by the JVM or hardware. No manual processing required

Execption: Exception: Refers to the exception generated by the JVM virtual machine

To determine whether the print is an exception or a message:

If the print message is followed by an error, the print is dead and alive with a false message.

If the printed information is followed by the end of the execption, the print is the exception information.

There are two ways to handle exceptions:

1. Catch Handling Exceptions:

To capture the format for handling exceptions:

try{//Possible exception code}catch (the variable that receives the exception) {

Operations that handle exceptions

}

Catch the use of the exception note point:

1. If the code in the try has an exception, the code after the exception is not executed.

2. You can handle multiple exceptions after a try.

3. When catching an exception, you can capture one of the largest exceptions directly execption

4. The caught exceptions need to be arranged in order from small to large.

Java Fifth Week study

Related Article

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.