current position:Home>Polymorphism and inheritance chapter summary

Polymorphism and inheritance chapter summary

2022-09-23 10:02:46get awesome


一 多态

1、 认识多态

2、 向上转型

3、 向下转型

4、 instanceof运算符


6、Two main forms of application of polymorphism

二 Polymorphism and inheritance chapter summary


一 多态

1、 认识多态

(1)The usual meaning of the word polymorphism is the ability to take on many different forms or shapes.

(2)在程序设计的术语中,它意味着A variable of a particular type can refer to objects of different types,and automatically

Invoke a method of the referenced object,That is, according to the different object types that act on it,响应不同的操作.


(4)多态意味着在一次方法调用中根据包含的对象的实际类型(即实际的子类对象)to decide should

调用哪个方法,It is not determined by the type of the variable used to store the object reference. 

当调用一个方法时,为了实现多态的操作,This method is declared in the parent class,Must also be heavy in subclasses

written method.

2、 向上转型



<父类型> <引用变量> = new <子类型>();

Animal animal = new Dog();


-->此时Changed by parent class reference量调用的方法是子类覆盖或继承了父类的方法,不是父类的方法.

-->此时通过Subclass-specific methods cannot be called from parent class reference variables.

3、 向下转型

(1)向上转型中,Subclass-specific methods cannot be called from parent class reference variables,如果需要调用子类特有的方法,可以

It does this by converting the parent class to the child class.

(2)向下转型:将一个指向子类对象的父类引用赋给一个子类的引用,That is to convert the parent class type to the child class


(3)The syntax for downcasting is as follows:

<子类型> <引用变量名> = (<子类型>)<父类型的引用变量>;

(4)The operations described above for such downcasting are paired with interfaces and abstractions(普通)The same applies to parent classes.

Animal animal = new Dog();
Dog dog =(Dog)animal;
animal instanceof Dog
instanceof Determine whether it can be down-transformed

4、 instanceof运算符

(1)在向下转型的过程中,If not cast to true subclass type,会出现类型转换异常


(2)在Java中提供了instanceofThe operator class performs type judgment.

(3)使用instanceof时,对象的类型必须和instanceofThe class specified by the following parameter has an inheritance relationship,否则会



在向下转型的时候,There may be conversion errors,There is no conversion to the subclass it points to,这时候会报ClassCastExceptionExceptions we can use before castinginstanceofThe keyword is used to judge the parent

Which subclass object the class reference points to




Operation and operation of other features.实际上新加子类更容易获得多态功能.

-->接口性:Polymorphism is when the parent class provides a common interface to the child class,由子类来具体实现.

-->灵活性:Polymorphism embodies flexible and diverse operations in applications,提高了使用效率.



6、Two main forms of application of polymorphism

1、 使用父类作为方法的形参

2、 使用父类作为方法的返回值

二 Polymorphism and inheritance chapter summary

1、 继承是Java中实现代码重用的重要手段之一.Java中只支持单根继承,即一个类只能有一个直


2、 在子类中可以根据实际需要对从父类继承的方法进行重新编写,称为方法的重写或覆盖.

3、 子类中重写的方法和父类中被重写的方法必须具有相同的方法名、参数列表,返回值类型必须

Same as the return type of the overridden method.

4、 在实例化子类是,会首先执行其父类的构造方法,然后在执行子类的构造方法.

5、 通过super关键字可以访问父类的成员.

6、 The amount of code in a class can be reduced through polymorphism,可以提高代码的可扩展性和可维护性.继承是多态的基础,


7、 在多态的应用中,You can use the parent class as a formal parameter of the method,还可以作为方法的返回值.

8、 Converting a subclass to a superclass is called向上转型,The system automatically performs type conversion.把父类转换为子类,称为向下转


9、向上转型后,A method called through a parent class reference variable is a method that the child class overrides or inherits from the parent class,Changed by parent class reference

Quantities cannot call subclass-specific methods.

10、 Subclass-specific methods are accessible after downcasting.Downcasting must cast to the true subclass type pointed to by the superclass,否则

A type conversion exception will occurClassCastException.

11、 instanceofOperators are used to determine whether an object belongs to a class.

copyright notice
author[get awesome],Please bring the original link to reprint, thank you.

Random recommended