Dark Horse Programmer-java Basics-Object-oriented-inheritance, constructors, overrides, final, abstract classes, interfaces

Source: Internet
Author: User

First speaking inheritance

1. The role of inheritance

1) Improve code reusability;

2) make a relationship between classes and classes;

2. Java Inheritance Features

1) Java only supports single inheritance and does not support multiple inheritance

Because multiple inheritance is a security risk: when the same functionality is defined in more than one parent class, but the feature content is not the same, the subclass object is not sure which one to run.

2) Java supports multiple layers of inheritance: it can form an inheritance system

Use an inheritance system approach: Read the System parent class, understand the common function (the basic function of the system), in order to invoke these basic functions, you need to create the most subclass of the object, is the most sub-class:

One is: The parent class may not be able to create cashes (such as static classes or interfaces);

The second is: the creation of subclass objects can use more functions, including the parent class functions and subclass-specific functions;

3) Relationship of functions in child parent class

When a subclass has a function that is identical to the parent class, the subclass overrides the contents of the function in the parent class, and when the subclass object calls the function, only the contents of the subclass function are run, which is the overridden attribute.

overriding effect: when subclasses inherit the parent class, the function in the parent class is inherited. However, although the subclass has this function, but the content of the function and the parent class are inconsistent, it is not necessary to define the new function, but to preserve the function definition of the parent class, and rewrite the function content to

4) override (overwrite) precautions

    • Subclass overrides the parent class function, must ensure that the child class permission is greater than or equal to the parent class permission, otherwise the compilation fails;
    • Static can only be covered by static;

5) The difference between overloading and rewriting

    overloading: only look at the parameter list of a function with the same name--the return value type can be different.

    rewrite: the Child parent class method is identical--function name, return value, parameter list must be consistent.

3. Relationship of constructors in child parent class

1) When the child class object is initialized, the constructor of the parent class is also run, because the constructor of the subclass defaults to the first line with an implicit statement super ();

Run Result: When you create an object, the child class accesses the constructor in the parent class first.

Note: If a constructor that has parameters defined in the parent class is displayed, the constructor for calling the parent class must be displayed in the first row of the subclass constructor.

Why must subclasses access constructors in the parent class?

Because subclasses can get the data directly from the parent class, when the subclass object is established, it needs to see how the parent class initializes the data, so that when the object is initialized, the subclass accesses the constructor in the following parent class first. -Otherwise, the data in the parent class gets from the subclass may not be the result of the parent class's eventual initialization.

Second speaking final Key Words

1. Final Features

1) can modify classes, functions, variables

2) A class that is final modified cannot be inherited--this is to avoid the key functions in the subclass of the parent class;

Compile fails as follows

    

3) The final modified function cannot be replicated, and the following will fail to compile:

4) The final modified variable is a constant that can be assigned only once, either as a member variable or as a local variable. The compilation fails as follows:

5) When the inner class is defined at a local location, it can only access the locally final decorated local variables and cannot access the externally final modified variables. Compilation fails as follows:

2. Meaning of final modified variables

In order to enhance the reading, these values are given a name for easy reading.

Third Speaking Abstract class

1, abstract class characteristics

1) Abstract methods must be in the abstract class;

2) abstract methods and abstract classes must be modified by the abstract keyword;

3) An abstract class cannot create an object with new, because invoking an abstract method makes no sense;

4) Abstract methods in an abstract class are to be used and must be called by the subclass object after all of its abstract methods have been replicated by the subclass. If the subclass only makes a partial copy of the abstract method, then the subclass is an abstract class-the purpose is to force subclasses to do something.

A compilation error occurs as the subclass is not defined as an abstract class and must be overridden by all the abstract methods of the parent class. The--set () method was not rewritten.

2. The difference between abstract class and general class

Abstract classes have more abstract functions (abstract methods cannot be defined in a generic class), and abstract classes cannot be instantiated.

3. Special cases of abstract class

Abstract methods may not be defined in an abstract class, and the purpose of defining the class as an abstract method is to not allow the class to establish an object.

4. Abstract class Exercises

Model employees: Employees have 3 attributes (name, work number, salary), and managers are employees, and there is a bonus attribute in addition to the attributes of the employee. Use the idea of inheritance to design the employee class and the manager class, requiring the class to provide the necessary methods for property access.

The code is implemented as follows:

Description: An abstract method work () is declared in an abstract class employee, requiring its subclasses to override the method, and the child class must be an abstract class.

Fourth Lecture Interface

1. Definition

An interface is equivalent to a special abstract class.

When a method in an abstract class is an abstract method, the class can be represented in the form of an interface. Use interface to define the interface and class to define it.

Format features:

    • Constants: public static final--global static constants
    • Method: public static abstract
    • Remember: The members in the interface are public

2. Interface Features

1) A class can inherit only one class, but may implement multiple interfaces, or it can inherit a class and implement multiple interfaces;

2) The relationship between interface and interface is also called inheritance, with extends to represent the inheritance relationship, the interface can be multiple inheritance (because there is no method body within the interface, will not cause confusion);

3) The basic function is defined in the class, the extension function is defined in the interface; For example, learning is the basic function of students (must do), while smoking, drinking is the expansion of students ' function;

3, interface Use example--Interface for function expansion (for example, the basic function of students is to learn, all students have, and expand the function can be smoking, play, etc.)

Summary of Knowledge points

1. Constructor relationships in child parent classes

All constructors of a subclass have access to the constructor of the parent's hollow argument by default, because the first row within each constructor of a subclass has a single, implicit super (). When the constructor for a parent class does not have an empty argument, the subclass must manually specify the constructor in the parent class to access by using the Super statement.

2. Subclasses overriding parent class functions

Subclass overrides the parent class function, must ensure that the child class permission is greater than or equal to the parent class permission, otherwise the compilation fails;

Static can only be overridden by static.

3. Final keyword Features

When an inner class is defined at a local location, it can only access the locally final decorated local variable and cannot access the externally final decorated variable.

4, the subclass of the abstract class must rewrite all the abstract methods in the parent class, otherwise, the subclass is still an abstract class;

5. Interface

    • Classes can implement multiple interfaces at the same time;
    • The subclass of the interface must rewrite all the methods in the interface;
    • The constants in the interface are modified with public static final, not written, the system will be assigned by default;
    • Interface methods are static abstract, with public static abstract modification, not written, the system will be assigned by default;

Dark Horse Programmer-java Basics-Object-oriented-inheritance, constructors, overrides, final, abstract classes, 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.