"Cicada Hall Study Notes" The Inheritance of Java basic knowledge

Source: Internet
Author: User
Tags define abstract modifier

Benefits of Inheritance
    1. Improve the reusability of your code.
    2. Having a relationship between classes and classes provides a precondition for a third feature polymorphism. (No inheritance, no polymorphism)

Java supports single inheritance and does not directly support multiple inheritance, but improves the multi-inheritance mechanism in C + + .

Single inheritance: A subclass can have only one parent class.

multiple inheritance: A subclass can have multiple direct parent classes ( not allowed in Java, modified)

Not directly supported, because there are multiple parent classes with the same members, which can produce call uncertainties.

in Java, it is embodied by the "multi-implementation" approach.

Java supports multilayer (multiple) inheritance

When you want to use an inheritance system:

    1. Review the top-level classes in the system to understand the basic functionality of the system.
    2. Create the most sub-class object in the system and complete the use of the feature.

When do you define inheritance?

Inheritance is defined when there is a relationship between the class and the class.

X is one of C ,x extends C;

When the member and local variables of this class have the same name, this is distinguished by this.

When a member variable with the same name in a child parent class is distinguished by super.

This and Super are very similar in usage.

This: Represents a reference to an object of this class.

Super: Represents a space for a parent class.

In the child parent class, the member characteristics are manifested (subclasses cannot directly access private content in the parent class)

    1. Member variables (subclass has, not found in parent class)
    2. member functions (subclasses have, functions that run subclasses)

Function two properties:

    1. Overloads, in the same class.
    2. Overwrite, in subclasses, overrides also become overridden,override.

overriding Considerations
    1. When a subclass overrides a parent class method, the child class permission must be greater than the parent class's permission.
    2. Subclasses cannot overwrite when the parent class method is private .
    3. Static can only be overridden by static, or statically overwritten.

When do I use the overwrite operation?

When you extend a subclass to a class, the subclass needs to preserve the function declaration of the parent class, but when you want to define the unique content of the feature in the subclass, use the overwrite operation to complete.

Child Parent class constructor features

When you construct an object in a subclass, the parent class also runs when the child class constructor is discovered.

Why?

In the subclass constructor, the first row has a default implicit statement -----> Super ();// The constructor that calls the parent's hollow argument

Constructors for subclasses ( either with or without a reference ) , the default implicit statement for the first row is super ();

Why do you want to access the constructor in the parent class when the subclass is instantiated?

Subclasses inherit the parent class and get the contents (attributes) of the parent class, so before using the parent class content, you first see how the parent class initializes its own content, so the subclass must access the constructor in the parent class when it constructs the object.

to complete this necessary action, the super () statement is added to the constructor of the subclass .

if the null argument constructor is not defined in the parent class, then the constructor of the subclass must use Super to explicitly call which constructor in the parent class. At the same time the constructor of the subclass, if This class constructor is called with this,super is gone . Because both Super and This can only define the first row. So there's only one. However, it is guaranteed that other constructors in the subclass will have access to the constructor of the parent class.

Note:The Super statement must be defined in the first row of the subclass constructor. Because the initialization of the parent class is done first.

Subclass object initialization must access at least one parent class.

Note ---> without parent class: default initialization, constructor initialization, and then initialization,

However, in the case of a parent class: when the parent class content is initialized with super, the member variable of the subclass does not show initialization, but instead initializes 0 by default . after the super () parent class is initialized, the member variables of the subclass are displayed initialization 8.

An Object instantiation process:

Person p = new person ();

    1. The JVM reads the person.class file under the specified path , loads it into memory, and loads the parent class of the person first, if there is a direct parent class.
    2. Open space in heap memory, assigning addresses.
    3. And in object space, the properties in the object are initialized by default.
    4. invokes the corresponding constructor for initialization.
    5. In the constructor, the first row is first called by the constructor in the parent class to initialize.
    6. After the parent class is initialized, the properties of the child class are displayed for initialization.
    7. The specific initialization of the subclass constructor.
    8. When the initialization is complete, assign the address value to the reference variable.

Ps: Reference variables are not necessarily within the stack

Class demo{

Person p = new person ();// within the heap, member variable.

}

final keyword

Inheritance disadvantage: Break the encapsulation.

Final keyword (final):

    1. Final is a modifier that can be decorated with classes, methods, and variables.
    2. the Final decorated class cannot be inherited.
    3. the Final modified method cannot be overridden.
    4. the Final modified variable is a constant and can only be assigned once.
    5. Final Fixed is the display initialization value, the default initialization value is invalid data.

Why use final modifier variables?

in the program, if a data is fixed, the data can be used directly, but the reading is poor. So give the data a name. And the value of this variable name cannot be changed, so add final fixed.

Code of notation:

constant values are capitalized, and the words are underlined. MY_PI

the first word of the variable value is lowercase and the first letter of the word is capitalized. Getmax

Inheritance (bottom)

Abstract class:

Abstract: Not specific.

Characteristics:

    1. a method is an abstract method that needs to be modified only if the declaration is not implemented .

an abstract method must be defined in an abstract class, and the class must also be decorated with abstractions .

    1. abstract class can not be instantiated / why? because invoking an abstract method is meaningless.
    2. An abstract class must have its subclasses overwrite all of the abstract methods before the subclass can be instantiated. Otherwise, this subclass is an abstract class.

Abstract class Problems

    1. Are there constructors in abstract classes?

There, used to initialize the subclass object.

    1. Can abstract classes not define abstract methods?

yes, but rare, to keep the class from creating objects, the adapter object for AWT is this class.

Usually the method in this class has the method body, but lacks the content.

    1. What keywords can I not coexist with abstract class keywords?

Private-->eg:privateabstract void Show ();---> Proprietary methods cannot be overwritten by the Quilt class discovery.

Static-->eg:static abstract void Show ();---> class name . method. There is no method body in the method, there is no meaning.

Final-->eg:final abstract void Show ();--->final adornment cannot be overridden,abstract Need to be overwritten.

    1. What is the difference between an abstract class and a generic class?

The same point: abstract classes and general classes are used to describe things, and members are internally determined.

different points: 1. General classes have enough information to describe things, and abstract classes may not have enough information to describe things.

2. Abstract methods cannot be defined in generic classes, only non-abstract methods can be defined, abstract methods may be defined in abstract classes, and non-abstract methods can be defined.

3. generic classes can be instantiated, and abstract classes cannot be instantiated.

    1. Abstract class must be a parent class?

Yes, you can instantiate a child class only if it needs to overwrite its method.

"Cicada Hall Study Notes" The Inheritance of Java basic knowledge

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.