java-"II" under the object-oriented chapter

Source: Internet
Author: User

1. Concept of inheritance

    • One of the object-oriented features, you can implement the reuse of the class. The following two ways can be implemented

    • A. Composition/aggregation relationship (contains other classes): Treats a class as a property of another class.

    • B. Derived classes: One class inherits another class, and the class that implements the inheritance is called the derived class, and the inherited class is called the parent class, the base class, and the superclass.

    • The member methods and member variables of the parent class inherit from the quilt class, but the parent class's construction method is not inherited.

    • Only single inheritance is supported in Java, and C + + multiple inheritance is not the same.

2.super keywords

1.super can access member variables of the parent class

2.super can call a member method of a parent class

3.super you can call the constructor of the parent class in the subclass construction method

Note: Super can only be used in member methods and construction methods, not in the static method (and this is the same),

Used in the construction method must be placed in the first row.

3.this and Super keyword comparisons (neither can be called in the static method)

4. Method overrides

    1. Method overrides exist only between subclasses and parent classes

    2. When a subclass overrides a method of a parent class, access permissions cannot be scaled down, can be enlarged, and other ways of writing

    3. Static methods of a parent class cannot be overridden by a quilt class as a non-static method

5. Access rights

Access modifiers for class

    1. Public: Classes in any package (that is, all classes) can access the class

    2. Default value: Classes in the same package can access the class

Access modifiers for Members

    1. Private: Only visible to this class, sub-class is not seen

    2. Default value: Visible to classes in the same package

    3. Protected: Visible to classes and all subclasses in the same package

    4. Public: Visible to all classes

Note: Access to members is a prerequisite for accessing the class where members are located

6. Overrides and overloads for comparison

7. The polymorphism of the object

Polymorphism refers to multiple patterns of reference types under different circumstances, or to calls to methods that are overridden in different subclasses by pointers to the parent class

    • A reference-type variable of a base class can "point to" the object of its child class.

    • A reference to a base class does not have access to the newly added members (properties and methods) in its child classes.

    • Instanceof: Object instanceof class Name/interface//Determine whether the object is the class/subclass of the class/implement the interface.

    • A subclass object can be used as a parent class object to use as an upward transformation, and vice versa, as a downward transformation.

8. Abstract classes and abstract methods

Abstract class: Any abstract class that is declared abstract cannot be instantiated unless its subclasses all overwrite its abstract method.

A class like this is not an abstract class, not necessarily an abstract method, but a class with an abstract method must be declared as an abstract class.

Abstract decoration: There are no member methods for the method body. You cannot use the abstract modifier to construct a method.

Subclass of abstract class: 1. Abstract the parent class has an abstract method:

A. Non-abstract subclasses: Subclass overrides overriding all the abstract methods of the parent class

B. Abstract subclasses: abstract methods in the parent class as long as there is a overwrite override and inheritance must be declared as the abstract class

2. There is no abstract method for the abstract parent class:

A. Non-abstract subclasses: Do not use abstract to modify subclasses and do not add abstractions (which can be instantiated at this time)

B. Abstract subclasses: You can use the abstract modifier, or add an abstraction method, and then declare it as an abstract class.

Note: Only two of the abstract methods that are decorated by the visual modifier are public, protected, except for the "default access modifier"

"Public", "protected" modifies the extraction method, the rest (private, static, final) can not be decorated.

9.abstract,final,static comparison

10. Interface (interface)

Interfaces: Abstract classes that are abstract than abstract classes, are collections of definitions of abstract methods and constant values. There are no implementations of variables and methods.

Interface constants: Default public static Final property (note: No other modifiers are accepted), type Name=value;

Interface method: Default public abstract Property (note: No other modifiers are accepted), ReturnType methodName (paramlist)

Interface implementation Class (implements): The class that implements the interface must override the methods of all interfaces, otherwise it becomes an abstract class. Multiple interfaces can be implemented

Benefits of the interface:

    • It solves the limitation of single inheritance of Java, and solves the problems caused by multiple inheritance of C + +.

    • The risk of multiple inheritance causes subclasses to inherit different implementations of the same method. And the methods in the interface are not implemented, so the problem of multiple inheritance.

    • The same behavior of unrelated classes is implemented through an interface without having to consider the relationships between these classes.

    • Interface to understand an object's interface, without needing to know the class that the object corresponds to

11. Comparison of abstract classes and interfaces

12. Polymorphism

Encapsulation is an operation to protect a property, and inheritance is to extend the functionality of a class

Polymorphism is another feature of the object-oriented programming language that is derived from encapsulation and inheritance.

Polymorphic manifestation: Overloading and rewriting of methods

1.1 Static polymorphism (compile-time polymorphism): implemented by method overloading (overload)

1.2 Dynamic polymorphism (run-time polymorphism): Subclass objects can be used as parent class objects, with method overrides (Overridder) implementations

13. The polymorphism of the object

There are two main types of polymorphism of objects:

1. Upward Transformation:

Parent class Parent Class object = subclass instance;//program auto-complete

Note the usage rules:

The parent class object is manipulated, so the newly added members (properties and methods) of the child class cannot be accessed.

Methods overridden in subclasses and member variables with the same name

A. member variables (including static variables) and static member methods: Compile and run look to the left (see parent class)

B. Non-static member method: Compile to see the left side, run to the right (the subclass overrides the method).

2. Downward transition:

Subclass Subclass Object = (subclass) Parent class object;//need to cast

14. Inner Class ( equivalent to a non-static member method that cannot declare a static variable )

Another class is defined inside the class, and this class is called an inner class.

The members of the outer class are known to the inner class, and the members of the inner class are not known externally, only the inner class can be instantiated to access its members

Pros: Easy access to the private properties of external classes (maximum benefits), reducing the size of compiled bytecode files

Disadvantage: The structure of the program is not clear.

14.1 member Inner class (i.e., instance inner class)

    • Static variable cannot be defined in a member inner class

    • member Inner class holds references to external classes

    • File management: After compiling, the "external class name $ internal class name." File is generated

    • The format of the externally instantiated class:

    • Outer out=new Outer (); Outer.Inner in=out.new Inner ();

14.2 Static inner class (external class)

An inner class declared with static, called a static inner class, is equivalent to an outer class. can be accessed through an external class. Inner class

    • Static inner classes do not hold references to external classes. You can create without creating an external class.

    • Static inner classes can access external static variables if the member variables to access the external class must be accessed through an instance of the

    • The format of the externally instantiated class

    • Outer.Inner in=new Outer.Inner ();

14.3 local inner class (Inner class defined in method)

    • You can access the members of the external class, and the local variable must be of the final type in the access method to access the

    • Local inner class cannot be added private, any modifier in protected,public

    • Cannot contain static members.

14.4 Anonymous Inner class

    • If an inner class is used only once throughout the operation, it can be defined as an anonymous inner class

    • An internal class without a name, which is a mechanism designed by Java to facilitate our programming. Sometimes there are internal classes

    • You just need to create an object, and you won't be able to use that class again.

    • Anonymous inner classes are developed on the basis of abstract classes and interfaces. It can either inherit a class or implement an interface

    • On file management: After compiling, the "external class name $ number. Class", numbered 1,2,3....N, represents the first few anonymous inner classes

Format:

New parent class or abstract parent class or interface name ()

{Implementation method}. Call the appropriate method;

Summarize:

Polymorphic: Non-static Method-look to the left at compile time and run to the right. Static methods and member variables-look left

(non-static) inner class: equivalent to a non-static method, which cannot define a static variable, and the inner class holds a reference to the external class by default.

(static) Inner class: equivalent to an external class that does not hold a reference to an external class.

Static code block, construction code block--construction method

java-"II" under the object-oriented chapter

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.