爪哇 中的继承
在这篇文章中,我们将看到Inheritance 在 爪哇 . It is one 的 the 面向对象 s principles apart from 抽象化, 封装 和 多态性.
介绍
The word 遗产
is quite familiar with everyone. In common terms, the word means the bequeathing 的 property 和 characteristics from generation to generation. For example, the property or characteristics 的 parents 是 handed down to their children 和 the forthcoming generations.
面向对象编程(通常 面向对象 ) concepts 是 based on real life examples, where every entity 在 existence can be represented as an 对象 ect. Thus, being one 的 the fundamental concepts 在 面向对象 , 遗产
is based on the example we discussed earlier – bequeathing 的 properties 和 characteristics from parents to their children.
从OOP的概念派生而来的Java继承定义为子类或对象(称为子类)从其前任或父类(称为“子类”)继承行为和属性(方法和变量)的过程。 超 类)。下面几节让我们更深入地研究Java中的继承概念。
基本语法
爪哇 中的继承 is implemented by the use 的 the keyword 延伸
. This special word makes the 爪哇 compiler understand that the current 类 is 在 heriting or extending another 类 . Let us look at the following snippet example to understand the basic syntax.
1 2 3 4 5 6 7 |
上市 类 B 延伸 A { //一些过程或变量 } |
The above snippet shows the use 的 the 延伸
keyword. In the language 的 爪哇 , the use 的 ‘extends’ 在 dicates that the 类 B is a child or a subclass 的 the 类 A, which is known as the 超 类 or parent. 遗产 represents an IS-A relationship
之间 类 es. Here, B is a child or subclass 的 A.
爪哇 中的继承示例
旁边给出的图片显示了Java继承的简单表示。
Here, the 类 父母 contains an 整型 eger variable a
和 is a 超 -class to 类 儿童 which contains an 整型 eger variable b
让我们通过一个代码示例来查看此图片的表示形式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
类 父母 { 整型 a = 5; } 类 儿童 延伸 父母 { 整型 b= 7; 上市 静态的 虚空 主要 ( 串 args []){ 儿童 c = 新 儿童 (); 系统 . 出 . 打印 (“ 值 的 A =”+c.a); 系统 . 出 . 打印 (“ 值 的 B =”+c.b); 系统 . 出 . 打印 (“ 和 =”+(c.a+c.b)); } } |
上面的代码片段在执行时提供了以下结果:
B的值= 7
和 =12
From the above sample we see that the child 类 B is able to access the variable a
的 类 父母 和 use it 在 its own 类 . Thus we see that using 在 heritance, sub-classes may utilize variables 和 方法 s from their 超 -classes.
爪哇 中的继承类型
在面向对象的编程中,有多种形式表示父子关系。 爪哇 在类的基础上支持三种类型的继承。本节将进一步讨论各种类型的继承以及如何使用Java实现它们。
单继承
这是Java中最简单的继承形式,并且是两个类之间的简单的一对一关系。上面一节已经讨论了单个继承的基本示例,其中单个Child类继承了其Parent类的属性。
旁边给出的图片表示类A(超类)和B(子类)之间的单个继承
让我们看一个小的代码片段作为Single 遗产的示例。
码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
类 MessageWriter { 上市 虚空 显示 (){ 系统 . 出 . 打印 (“ 您 是 观看 a 方法 的 A”); } } 类 消息显示器 延伸 MessageWriter { 上市 静态的 虚空 主要 ( 串 args []) { B 对象 = 新 B(); 对象 . 显示 (); } } |
上面的代码片段执行以产生以下输出:
多层次继承
多层次继承
顾名思义,它看起来更像是基于层的结构。旁边给出的图片表示一个多级继承结构。在多级继承中,每个类仅以多层或多层体系结构的形式扩展单个类。
例如,从旁边的图中,我们看到类C是类B的子类,而类B是类A的子类。因此,B将依次继承类A和C的属性和行为。 ,将从两者继承。
让我们看一个代码片段作为多级继承的示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
类 动物 { 虚空 吃 (){ 系统 . 出 . 打印 (“ 动物 吃 ”); } } 类 马 延伸 动物 { 虚空 呼叫 (){ 系统 . 出 . 打印 (“ 马匹 嘶 ”); } } 类 小马 延伸 马 { 虚空 描述 (){ 系统 . 出 . 打印 (“ 马驹 是 宝宝 马匹 ”); } } 类 TestMultiLevel { 上市 静态的 虚空 主要 ( 串 args []){ 小马 c = 新 小马 (); c. 描述 (); c. 呼叫 (); c. 吃 (); } } |
执行上述代码后,将获得以下输出:
马n
动物吃
层次继承
层次继承
是一种继承类型,其中许多子类扩展了单个超类。
旁边给出的图片表示层次继承的基本形式。这种继承形式基本上是多个单一继承,其中所有子类均从单个超类继承。在这里,类B和C是类A的子类,并继承其属性和行为。
让我们看一个代码片段作为层次继承的示例。
码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
类 动物 { 虚空 吃 (){ 系统 . 出 . 打印 (“ 动物 吃 ”); } } 类 狮子 延伸 动物 { 虚空 呼叫 (){ 系统 . 出 . 打印 (“ 雄狮 怒吼 ”); } } 类 狼 延伸 动物 { 虚空 呼叫 (){ 系统 . 出 . 打印 (“ 狼队 嗥 ”); } } 类 测试层次{ 上市 静态的 虚空 主要 ( 串 args []){ 狼 w = 新 狼 (); 狮子 l= 新 狮子 (); w. 呼叫 (); w. 吃 (); l. 呼叫 (); l. 吃 (); } } |
上面的代码在执行时产生以下输出:
动物吃
狮子吼
动物吃
多重继承
Multiple 遗产
是面向对象编程中的一种继承,其中单个子类扩展了多个超类。混合继承是混合的继承形式,包括多级继承和多级继承。多重继承不受支持 爪哇 .
上图显示了多重继承
这张图片描绘了混合继承。
为什么Java不支持多重继承?
为了降低复杂性和歧义性,Java不允许使用类进行多重继承。让我们用一个例子来解释歧义。
Say, there is a 类 A having a 方法 显示 ()
和 类 B having another 方法 显示 ()
. A third 类 C, 延伸 both A 和 B 和 as per the concept 的 在 heritance
, is able to access both the 显示 () 方法 s 的 A 和 B. Now, when creating an 对象 ect 的 C 和 呼叫 在 g the 显示 ()
方法 , the compiler will not be able to discern which 显示 方法 to 呼叫 as both 类 es A 和 B have a 方法 having the same name 和 是 extended by 类 C.
码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
类 A (){ 上市 虚空 显示 () { //一些代码 } } 类 B (){ 上市 虚空 显示 () { //一些代码 } } 类 C 延伸 A,B () ( 不 支持的 在 爪哇 ){ 上市 静态的 虚空 主要 (){ C 对象 = 新 C (); 对象 . 显示 (); ( 歧义性 之间 显示 () 的 类 A 和 显示 的 类 B) } } |
为了避免这种歧义,Java不允许类之间进行多重继承,并且在尝试进行多重继承时会引发编译时错误。
注意:但是,Java通过使用来模拟多重继承 介面.
结论
遗产
是Java的强大武器,有助于使其成为一种广泛接受的语言。它有助于减少代码重复并减少错误。使用在父类中编写的代码,您不再需要为具有相同属性的多个子类编写相同的代码。这样,java中的继承实现了代码可重用性,以确保更好的用户可访问性。
那’关于Java继承的全部内容。