Eighth Day of Java learning

Source: Internet
Author: User

Nineth Time Class

Goal

1. polymorphic

2. Abstract class

3. Interface

4. Internal class (understanding)

One, polymorphic polymorphism

The pursuit of "high cohesion and low coupling" through three major characteristics: encapsulation inheritance and polymorphism to achieve.

Polymorphic: Various forms, "go out to buy fruit?", polymorphic behavior that occurs at run time, compiles look at the parent type, and runs the Find child object.

1. Conditions

1), inherit 2), override (No Override, continuation method) 3), parent class reference = Subclass Object

2. New method

When polymorphism occurs, the new method is not visible.

Forced type conversion succeeds, can use new method

3. Two forms of expression

1), formal parameters using parent type

2), return type using parent type

4. Type conversion

1), auto | up (upcasting) | polymorphic

Parent class = Child class object

2), Force | Down (downcasting)

Subclass = (Subclass) The object that the parent class points to

Note: The correct occurrence of polymorphism to be able to convert success, otherwise error

The Instanceofà relational operator, which checks the type of the object to which the reference is pointing (to the right), whether this type | parent type, must be on the inheritance chain.

Common error: Conversion error java.lang.ClassCastException

Second, abstract class

A class that is decorated by abstract, called an abstract class, cannot be instantiated, as a subclass instance (polymorphic), typically used as a tool class, as a template (invariant + variable), called a partial specification.

1. Define the format

Public abstract class xxx{

}

2. Features

1), abstract modifier, cannot use final decoration, because subclasses must exist

2), can not be instantiated, to subclass instantiation; Existence constructor

3), there may be abstract methods, abstract classes can have no abstract method

3. Abstract methods

Method that is modified by the abstract without method body ({}), note; End.

1), abstract classes can have no abstract method

2), abstract methods must exist in the abstract class

Third, the interface

Interface interface, full specification, standard, like such as: USB interface, with the connection of computer, transmission data and other functions. Interfaces are only global constants and common abstract methods, and are considered special abstract classes.

1. Format

[Public]/*abstract*/interface interface Name {

Global constants

/* public static final*/type constant name = assigned value;

The public abstract method

Public abstract return value type |void method name ([formal parameter list]);

}

2. Class and interface

Implements Multi-Implementation interface

3, interface and interface:

Multiple Inheritance (understanding)

4. Null interface

Give the JVM a pass

1), java.lang.Cloneable

2), java.io.Serializable

Iv. Internal classes (understanding)

Inner classes: Classes become classes in classes, methods

1. Classes in class

Instantiation:

2, the method of the class

3. Anonymous inner class (emphasis)

V. Eclipse debugging

Objective: To understand the process of program execution and to eliminate errors.

Eighth Day of Java learning

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.