Home  >  Article  >  Java  >  What keywords are used for Java class inheritance?

What keywords are used for Java class inheritance?

(*-*)浩
(*-*)浩Original
2019-11-29 15:27:209364browse

What keywords are used for Java class inheritance?

Inheritance of classes

Subclasses and their definitions

Inheritance of Java classes uses the extends keyword (Recommended learning: java course)

class SubClass extends SuperClass{
........
}

Subclasses can inherit the attributes and methods of the parent class

Subclasses cannot Inherit properties with private modifiers, methods

Subclasses cannot inherit construction methods

Single inheritance:

Java only supports single inheritance, that is, only It can be inherited from a class, and there can be only one class name after the extends keyword.

Advantages: Possible conflicts between multiple parent classes can be avoided.

Interface interface mechanism allows one class to implement multiple interfaces

super keyword

The super keyword points to the parent class of the class where the keyword is located

Parent class reference variables can point to subclass objects

super.someNethod([paramlist])//调用父类中的someMethod()方法

Creation of subclass objects

Steps:

Allocate all the memory space required by the object and initialize it to a value of 0

According to the inheritance relationship, explicit initialization from top to bottom

According to the inheritance relationship, call the constructor from top to bottom Method

Another expression of subclass object initialization:

Basic initialization, execute the construction method, first execute the parent class construction method,

execute Before the parent class constructor method, the explicit initialization statement of the parent class must be executed.

Rewriting of methods

(1) The return value type of the overridden method in the subclass must be the same as that of the overridden method in the parent class. The return value type of the written method is the same

(2) The access rights of the overridden method in the subclass cannot be reduced

(3) The subclass override cannot throw a new exception: the overriding of the method Writing is the basis for realizing object runtime polymorphism

Polymorphism: compile-time polymorphism and run-time polymorphism.

Compile-time polymorphism: such as overloading

Run-time polymorphism: such as overriding

Upstream modeling

Convert a Convert a reference to an object of one type into a reference to an object of another type

Downcasting (forced type conversion)

instanceof operator

aOblectVariable instanceof SomeClass

aOblectVariable is of type SomeClass, the value of this expression is true, otherwise it is false

Format

(SomeClass)aObjectVariable

(1) The target type of object variable conversion must be a subclass of the current object type

(2) The object type must also be checked at runtime

Object class

Every class in java is a direct or indirect subclass of the Object class.

equals class: compare the values ​​of two objects

Override the equals() method Purpose: define the value of the object

Java regulations: two objects with equal values ​​hashCode ( )The return value must be equal

So rewrite the equals() method, and also rewrite the hashCode() method

"==" compares the addresses of the two objects                            

toString() method

Returns the string representation of the object

getClass() method

Returns the class information of the object, This method returns an object of type Class

The above is the detailed content of What keywords are used for Java class inheritance?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn