Dark Horse Programmer-java Basics-Object-oriented-polymorphic, object, inner class, exception

Source: Internet
Author: User
Tags finally block sleep function throw exception throwable

First Lecture polymorphic

1. Definition

Can be understood as the existence of a variety of forms of expression.

In Java, this method can be defined as an abstract method, allowing its subclasses to be implemented when the functional content of the function is undefined. When a subclass object is not simultaneously called, the same function name is executed, and different function bodies are performed, resulting in different results, thus manifesting polymorphism.

2. The manifestation form of polymorphism

    • The reference to the parent class points to its own subclass object: Fu f = new Zi ();
    • A reference to a parent class can also accept its own subclass object;

3. Preconditions for polymorphism

    • Must be a relationship between a class and a class, either inherited or implemented (interface);
    • There is an overlay (override) between functions;

4. The benefits of polymorphism

Greatly improve the extension of the program. --When the subclass object increases, the code is as small as possible.

5. Polymorphic defects

Extensibility is improved, but members in the parent class can only be accessed using a reference to the parent class. A reference to a parent class cannot access a member in a subclass.

as follows, a reference to the parent class An1 accesses the subclass-specific method Kanjia ();

  

6. Child Parent class type conversion

Fu f = new Zi (); ———— type automatically promotes, transitions upward, and turns subtypes into parent types.

Zi z = (Cat) F; ———— forces a reference to the parent class to be transformed into a subclass type, down.

  Note: You cannot turn a parent type object into a subtype.

7. polymorphic Applications

The establishment of two categories: basic class students and senior class students, all have learning, sleep function. These two kinds of things are required to be extracted.

8, polymorphic appearance, the characteristics of the code-the use of multi-state considerations

1) Characteristics of multi-state non-static member functions

compile time: See if there is a calling method in the class to which the reference type variable belongs, and if so, the compilation fails. because: at compile time, the object has not been created, and the parent class and subclass have not yet established a relationship.

run time: See if there are methods called in the class to which the object belongs, and if so, perform the functions in the class to which the object belongs, otherwise, perform the functions in the parent class.

Summary: when a member function is called in a polymorphic state, the compiler looks to the left (reference type) and runs to the right (the class to which the object belongs).

2) Characteristics of member variables in polymorphic states

Whether compiling or running, refer to the left (the class to which the reference variable belongs.) If this member variable is not in the class to which the reference variable belongs, the compilation fails. The variables in the class to which the reference variable belongs are also called at run time.

Accessing a subclass variable method in polymorphic: when you need to access a member variable in a subclass, you can cast a reference of the parent type to a subtype with coercion type conversion: Zi x = (zi) y, where y represents the application variable that points to the parent type of the subtype

3) Characteristics of static member functions in polymorphic states

Whether compiling or running, refer to the left (the class to which the reference variable belongs).

Cause: The non-static method and static are stored in memory in different locations, there are two references in the non-static method area (this and super), and only the class name is referenced in the static method area.

Second lecture Object class

1. Concept

object is a direct or indirect parent class for all objects. What is defined in this class is definitely a feature that many objects have.

2. Object class Application

The object class has already provided the same comparison method for objects equals (), if the custom class also has the same functionality, there is no need to redefine, as long as the function in the parent class to build their own unique comparison-this is the overlay.

Third Speaking Inner class

1. Definition

2. Access rules

1) The inner class can access the members of the outer class directly, including the private one, because at this point, the inner class is in the member position, the task is a member of the outer class, and the inner class holds a reference to the outer class, in the format: outer.this.x--this represents an inner class object. This is why the inner class has direct access to the external class members.

2) to access the inner class, the external class must establish an internal object:

Inner Inner = new Inner ();

3) How other external classes access the inner class

When an inner class is defined as non-private, an inner class object can be accessed directly from outside other classes in the following format:

Outer.Inner Inner = new Outer (). New Inner ();

When an inner class is on a member's position, it can be decorated by a member Fu Yi

For example, private decoration: The inner class is encapsulated in the outer class, only external classes can access;

    static modifier : The inner class has the static property. At this point, the inner class can only access static members in the outer class, and access restrictions occur.

External other classes accessing the static decorated inner class non-static member methods are as follows:

———— new Outer.Inner (). function (); The--inner class is a static member of the Outer class and can be called directly with the class name.

The static member method of the external class directly accessing the static adornment is as follows:

———— Outer.Inner.function ();--static members can be called directly by the class name.

Note: when a static member is defined in an inner class, the inner class must be static, and the inner class must be static when a static method in the outer class accesses the inner class.

-- A big principle: static members can only be accessed statically.

When the inner class is on a local location:

cannot be modified by static;

You can access members in an external class because you also hold references in external classes. However, you cannot access a variable in its local area, only the local variables that are final decorated.

As follows: A must be final modified to be accessed by the inner class.

as follows, the inner class is defined in the local location. The system defaults to a reference to an external class for the inner class Outer.this

  Summary: external classes require an inner class object to access members of an inner class, regardless of whether the inner class definition is in a member location or a local location, and the inner class obtains an external class object (the system provides an external class reference Outer.this by default) to access the external class members.

3. Anonymous inner class

definition: An anonymous inner class is actually a shorthand format for an inner class.

the premise of defining an anonymous inner class: an inner class must inherit a class or implement an interface.

Fourth Lecture Exception

1. Definition

exception: An abnormal condition that occurs when the program is running.

The origin of the anomaly: The problem is also a concrete thing in real life, can also be described in the form of Java class, and encapsulated into objects, that is, the abnormal situation after the description of the object embodiment.

Exception Partitioning: serious and non-critical issues.

serious problem:Java is described by the error class, and the error generally does not write targeted code for processing.

Non-critical:Java is described by exception and can be handled using a targeted approach.

among them, The error and exception are the same throwable subclasses.

2. Exception handling

  Exception Handling Purpose: The exception is disposed of, so that subsequent statements outside the try to continue execution. If not processed, the program is stopped and no subsequent statements are processed.

Cause: If the virtual machine calls a program that does not have the ability to handle this exception, the exception is thrown to the virtual machine, and the default mechanism for the virtual machine to handle the exception is to simply stop the program.

The exception handling code is formatted as follows:

There are two ways of handling exceptions:

The first is: Throw an exception up (to the caller);

The second is to catch exceptions and handle them internally.

3. Multiple exception handling

1) As long as an exception occurs in the function, the function stops;

2) The called function declares a few exceptions, the caller corresponds to several catch blocks, if the exception in multiple catch blocks has an inheritance relationship, the parent exception catch block must be placed at the bottom;

3) If the program has an exception declared by the called function, it is best to throw this exception to the virtual machine and stop the program;

4) When it is recommended to catch processing, it is important to define a specific processing method in the catch, rather than a simple print exception. The exception information can be exported to the corresponding documents in the hard disk to facilitate the subsequent exception finding and processing;

4. Custom Exceptions

You can customize exceptions that are not defined in the Java mechanism.

Note: Java does not automatically recognize custom exceptions, so custom exception objects need to be created manually and thrown (exceptions defined by Java itself can be thrown by default)

1) Custom Exception information

Because the operation of the exception information has been completed in the parent class, the subclass simply passes the exception information through the Super statement to the parent class at the time of construction, and then can get the custom exception information directly through the GetMessage method.

2) Custom Exception features

Must inherit exception. Because: Exception system has one characteristic: Exception class and exception object are thrown, they all have the parabolic, this is unique characteristic in Throwable system. Only classes and objects in this system can be throws and throw operations.

5. The difference between throws and throw

1) throws used on function; throw is used within function;

2) throws followed by the exception class, can be multiple, separated by commas; throw followed by the exception object;

  Note: The throw below should not be followed by other statements, because after the exception is thrown, the following statement will no longer execute. Throw and return are identified as the end of the function. However, the system exits (System.exit (0)) without execution.

6, RuntimeException

definition: throws an exception inside a function, the function must declare a throw exception, otherwise the compilation does not pass, but RuntimeException does not need a function declaration to throw. If the exception is declared on a function, the caller can pass the compiler without exception handling.

runtimeexception The reason you do not need to declare on a function is that this exception does not require the caller to handle it. When such an exception occurs, you want the program to stop because there is a situation where the operation cannot continue while the program is running, and you want to fix the code after you stop the program.

7, the characteristics of abnormal coverage

The exception is reflected in the child parent class overlay:

1) When the subclass overrides the parent class, if the method of the parent class throws an exception, then the overridden class method of the child class can only throw the exception of the parent class or the subclass of the exception, or not throw an exception;

2) If the parent method throws more than one exception, the child class can only throw a subset of the parent exception when overwriting the method;

3) If no exception is thrown in the method of the parent class or interface, the subclass cannot throw an exception when overriding the method. If a subclass method has an exception, it must be done with a try and never be thrown.

Summary of Knowledge points

1. Inheriting classes and implementing interfaces

The parent class defines the common properties of a class of things, and the interface tends to define the extended functionality of a class of things.

2. Use of Throw and throws

1) The throw is defined within the function and is used to throw an exception object;

2) throws defined on the function, used to throw the exception class, can be thrown multiple, separated by commas;

3. There are two kinds of exceptions: compile-time exception and run-time exception

1) The exception that is detected at compile time: Inside the function is thrown, the function must be declared, and must be handled or thrown out in the caller;

2) Exceptions that are not detected at compile time (i.e., run-time exception, runtimeexception)

No declaration is required on the function, and the caller is not required to handle it.

4. Statement features in finally block in exception handling

1) The most common definition in finally is: close the resource code. Because the resource must be freed.

2) Finally, only one case will not be performed: When executed to System.exit (0), the statement in finally is not executed.

Dark Horse Programmer-java Basics-Object-oriented-polymorphic, object, inner class, exception

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.