Java Inheritance and Interfaces

Source: Internet
Author: User

First, inheritance

1, the concept of inheritance

Inheritance is the cornerstone of object-oriented programming technology and is a mechanism for creating new classes from existing classes.

The inheritance of a class is the ability of a subclass (subclass) to automatically inherit all its properties and methods from the parent class. And any instance methods declared in the subclass can call them.

Java does not support multiple inheritance, and a subclass has only one parent class.

An inheritance relationship, also known as a generalization relationship, describes the relationship between a parent class and a subclass, represented by a straight line with a hollow triangle in UML.

2, sub-class

Create subclass: Extends the superclass statement indicates the parent class to inherit.

It should be clear that the object class in Java is the parent class of all classes, and its methods can be called by all classes.

Subclasses automatically inherit from the parent class public, protected decorated properties and methods, and cannot access the properties and methods of the private adornment.

For the default access permission (no access modifier), if the subclass parent is in the same package, the subclass can inherit the parent class default member;

3. What the subclass can do

    • Subclasses can use inherited member variables directly, just as they do with their own defined variables;
    • Declares a new member variable, which, if the name is the same as the member variable of the parent class, hides the member variable of the parent class;
    • Inherited methods can also be used directly
    • A method that implements the same signature as a parent method in a subclass overrides the method of the parent class;
    • The construction method of a subclass can use super to call the constructor of the parent class;

4. Construction method and sub-class memory structure

The constructor method of a class cannot have modifiers, return types, and exception declaration clauses.

When the constructor method of a subclass is called, the constructor of the parent class is called first, then the initialization block of the instance variable and the static variable is executed, and the subclass's own construction method is finally executed.

5. Invocation of the parent class construction method

The compiler automatically adds a default super () method call to a child class if its constructor does not show the constructor that called the parent class. However, if the parent does not have a default parameterless construction method, the compiler will make an error and the compiler will make an error.

Also, the super () statement must be the first clause of a subclass construction method.

6. Instance variable initializer, {...} Statement block

In addition to constructing the method, initialize the properties of the object using this instance variable initializer!

It runs after the constructor of the class is invoked, after the constructor of the parent class, before the current class's construction method.

7. Class variable initializer static{...}

Initialize class variables, which are static blocks of code, can only be initialized once! (will be executed first)

8, sub-class memory allocation

Memory in Java is divided into heap (heap), stack (stack), global data area, and code area.

The new object is placed in the heap, and its use is invoked by reference.

The reference is pressed into the stack (because the reference is small and is suitable for entering and exiting in the stack).

In fact, from a memory perspective, a subclass has all the attributes of the parent class (that is, including private), but the object of the subclass does not directly access the private variables in the parent class, which is also the key to class encapsulation.

Second, the modifiers in Java

By modified object: Class modifier, property modifier, method modifier

by function:

1. Access Rights modifiers

Public, private, protected, default (previously introduced)

2. Final modifier

Final means immutable.

Modifier class: A class cannot be extended, that is, it cannot be inherited.

Modifier Property: The property value cannot be changed, meaning that the declaration is to be initialized manually.

Modification method: The method cannot be overridden, that is, the final version, the subclass cannot be modified.

3. Abstract modifier

Decorated class: The class is abstract and cannot be instantiated and must be expanded before the object can be generated.

Modification Method: Abstract method, must quilt class override (override).

4. Static modifier

Table Name properties and methods are subordinate to class ~ can be accessed through the class name.

5. Super modifier

Super is a reference to the parent class, which is the name of the parent class, which makes the masked parent class member variable or member method visible.

If the child class hides, overrides the properties and methods of the parent class, it can be called through super.

Use of Super:

      • To access the hidden properties of the parent class: Super.variable
      • Call the Parent class overridden method: Super.method ([paralist])
      • Call the constructor of the parent class: Super ([Paralist])

Java Inheritance and Interfaces

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.