Java Learning notes-deep analysis of the seventh chapter class

Source: Internet
Author: User

The deep analysis of the seventh chapter class

1. Inheritance

1.1 class inheritance method: in Java, the child class inherits from the parent class by using the extends keyword in the declaration of the class. The general format is: [Class modifier] class < subclass name > extends < parent class name >{class body contents}. A class can inherit directly from only one parent class, and a parent class may have multiple subclasses.

1.2 the inheritance and concealment of member variables: When you create a subclass based on a parent class, subclasses can inherit member variables and member methods of the parent class. However, if a variable with the same name is declared in both the parent and child classes, the two variables exist at the same time as the program runs. That is: the member variables of the parent class are used for the parent class method, and the member variables of the subclass are used for the subclass methods.

Inheritance and overwrite of the 1.3 method: subclasses can inherit all member methods that can be accessed by subclasses in the parent class, but if the subclass redefined a method inherited from the parent class, this method of the parent class will no longer exist in the subclass, at which point the subclass method overrides the method of the parent class, and Jane (override) of the method is called.

1.4 Inheritance of constructed methods: When an object is created from a subclass, the system first calls the parent class's parameterless construction method before the subclass's constructor is executed. You can use the Super keyword if you want to invoke the parent class's argument construction method.


2. Polymorphism : polymorphism means that different methods of the same name coexist in the program. That is, the same method defines several versions, and the program runs with different versions depending on the situation. "An external interface, a number of internal implementation methods"

How to implement polymorphism:

(1) method coverage for polymorphism

This is done by redefining the methods that inherit the parent class from subclasses.

(2) method overloading for polymorphism

By defining multiple different methods with the same name, the system distinguishes between different methods depending on the parameters (type, number, order).


3. Abstract class : Abstract classes usually contain only abstract methods (only the declaration of the method, no method body), and the concrete implementation of the method is derived from each subclass to complete, which makes the function of the program and the implementation of the function is separated. In addition, because an abstract class can derive multiple subclasses, an abstract method in an abstract class can have multiple implementations in multiple subclasses, which also implements the polymorphism of the class

(1) The definition format of an abstract class is usually as follows:

Public abstract class Plane {//declaring abstract classes

Private String shape; Declaring member variables

......

public abstract double area (); Declaring abstract methods, note: semicolons are necessary

......

}

(2) Abstract classes cannot be instantiated, that is, objects cannot be created based on abstract classes.

(3) Abstract classes can also contain normal member variables and member methods. However, abstract methods can only appear in abstract classes. That is, the class containing the abstract method must be an abstract class, and non-abstract classes cannot contain abstract methods.


4. Interface: an interface is a special class that consists of constants and abstract methods. The general format of the Declaration interface is as follows:

[public] Interface interface Name [extends parent interface List]{

Constant declaration

Abstract method declaration

}

The relationship between an interface and an abstract class:

(1) Only constants and abstract methods can be defined in an interface, whereas other member variables and member methods may be declared in an abstract class;

(2) A class can inherit only one abstract class at the same time, but it is possible to implement multiple interfaces at the same time;

(3) The inheritance of abstract class with extends, the implementation of the interface with implements.


Java Learning notes-deep analysis of the seventh chapter class

Related Article

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.