继承是面向对象编程的一个重要支柱。它是java中允许一个类继承另一个类的特性(字段和方法)的机制。
重要术语:
- 超级班: 其特征被继承的类称为超类(或基类或父类)。
- 子类: 继承另一个类的类称为子类(或派生类、扩展类或子类)。除了超类字段和方法之外,子类还可以添加自己的字段和方法。
- 可重用性: 继承支持“可重用性”的概念,也就是说,当我们想要创建一个新类,并且已经有一个类包含我们想要的一些代码时,我们可以从现有的类派生出我们的新类。通过这样做,我们可以重用现有类的字段和方法。
如何在Java中使用继承
用于继承的关键字是 延伸 .
语法:
class derived-class extends base-class { //methods and fields }
例子: 在下面的继承示例中,类Bicycle是基类,类MountainBike是扩展Bicycle类的派生类,类Test是运行程序的驱动程序类。
JAVA
// Java program to illustrate the // concept of inheritance // base class class Bicycle { // the Bicycle class has two fields public int gear; public int speed; // the Bicycle class has one constructor public Bicycle( int gear, int speed) { this .gear = gear; this .speed = speed; } // the Bicycle class has three methods public void applyBrake( int decrement) { speed -= decrement; } public void speedUp( int increment) { speed += increment; } // toString() method to print info of Bicycle public String toString() { return ( "No of gears are " + gear + "" + "speed of bicycle is " + speed); } } // derived class class MountainBike extends Bicycle { // the MountainBike subclass adds one more field public int seatHeight; // the MountainBike subclass has one constructor public MountainBike( int gear, int speed, int startHeight) { // invoking base-class(Bicycle) constructor super (gear, speed); seatHeight = startHeight; } // the MountainBike subclass adds one more method public void setHeight( int newValue) { seatHeight = newValue; } // overriding toString() method // of Bicycle to print more info @Override public String toString() { return ( super .toString() + "seat height is " + seatHeight); } } // driver class public class Test { public static void main(String args[]) { MountainBike mb = new MountainBike( 3 , 100 , 25 ); System.out.println(mb.toString()); } } |
No of gears are 3speed of bicycle is 100seat height is 25
在上面的程序中,当创建MountainBike类的对象时,超类的所有方法和字段的副本将获取该对象中的内存。这就是为什么通过使用子类的对象,我们也可以访问超类的成员。
请注意,在继承过程中,只创建子类的对象,而不是超类。有关更多信息,请参阅 继承类的Java对象创建 .
程序的说明性图像:
在实践中,继承和 多态性 在java中一起使用,以实现代码的快速性能和可读性。
Java中的继承类型
下面是Java支持的不同类型的继承。
1.单一继承: 在单一继承中,子类继承一个超类的特征。在下图中,类A充当派生类B的基类。
JAVA
// Java program to illustrate the // concept of single inheritance import java.io.*; import java.lang.*; import java.util.*; class one { public void print_geek() { System.out.println( "Geeks" ); } } class two extends one { public void print_for() { System.out.println( "for" ); } } // Driver class public class Main { public static void main(String[] args) { two g = new two(); g.print_geek(); g.print_for(); g.print_geek(); } } |
GeeksforGeeks
2.多级继承: 在多级继承中,一个派生类将继承一个基类,并且该派生类也将充当其他类的基类。在下图中,类A充当派生类B的基类,而派生类B又充当派生类C的基类。在Java中,类不能直接访问 祖父母的成员 .
JAVA
// Java program to illustrate the // concept of Multilevel inheritance import java.io.*; import java.lang.*; import java.util.*; class one { public void print_geek() { System.out.println( "Geeks" ); } } class two extends one { public void print_for() { System.out.println( "for" ); } } class three extends two { public void print_geek() { System.out.println( "Geeks" ); } } // Drived class public class Main { public static void main(String[] args) { three g = new three(); g.print_geek(); g.print_for(); g.print_geek(); } } |
GeeksforGeeks
3.等级继承: 在层次继承中,一个类充当多个子类的超类(基类)。在下图中,类A充当派生类B、C和D的基类。
JAVA
// Java program to illustrate the // concept of Hierarchical inheritance class A { public void print_A() { System.out.println( "Class A" ); } } class B extends A { public void print_B() { System.out.println( "Class B" ); } } class C extends A { public void print_C() { System.out.println( "Class C" ); } } class D extends A { public void print_D() { System.out.println( "Class D" ); } } // Driver Class public class Test { public static void main(String[] args) { B obj_B = new B(); obj_B.print_A(); obj_B.print_B(); C obj_C = new C(); obj_C.print_A(); obj_C.print_C(); D obj_D = new D(); obj_D.print_A(); obj_D.print_D(); } } |
Class AClass BClass AClass CClass AClass D
![图片[4]-Java中的继承-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/geeks/20210311/geeks_Untitled-300x269.png)
等级继承
4. 多重继承 (通过接口): 在多个继承中,一个类可以有多个超类,并从所有父类继承特征。请注意,Java确实如此 不 支持 多重继承 上课。在java中,我们只能通过 接口 .在下图中,C类来自接口A和B。
JAVA
// Java program to illustrate the // concept of Multiple inheritance import java.io.*; import java.lang.*; import java.util.*; interface one { public void print_geek(); } interface two { public void print_for(); } interface three extends one, two { public void print_geek(); } class child implements three { @Override public void print_geek() { System.out.println( "Geeks" ); } public void print_for() { System.out.println( "for" ); } } // Drived class public class Main { public static void main(String[] args) { child c = new child(); c.print_geek(); c.print_for(); c.print_geek(); } } |
GeeksforGeeks
5.混合继承(通过接口): 它是上述两种或两种以上继承类型的混合。因为java不支持类的多继承,所以类也不支持混合继承。在java中,我们只能通过 接口 .
关于Java中继承的重要事实
- 默认超类 :除了 对象 类,它没有超类,每个类都有一个且只有一个直接超类(单继承)。在没有任何其他显式超类的情况下,每个类都隐式地是 对象 班
- 超类只能是一个: 一个超类可以有任意数量的子类。但子类只能有 一 超类。这是因为Java不支持 多重继承 上课。虽然有接口,但java支持多继承。
- 继承构造函数: 子类从其超类继承所有成员(字段、方法和嵌套类)。构造函数不是成员,因此它们不会被子类继承,但可以从子类调用超类的构造函数。
- 私有成员继承: 子类不会继承其父类的私有成员。但是,如果超类具有访问其私有字段的公共或受保护方法(如getter和setter),则子类也可以使用这些方法。
Java是一种关系。
IS-A是一种说法:这个对象就是那个对象的一种类型。让我们看看如何使用extends关键字来实现继承。
JAVA
public class SolarSystem { } public class Earth extends SolarSystem { } public class Mars extends SolarSystem { } public class Moon extends Earth { } |
现在,基于上述示例,用面向对象的术语来说,以下是正确的:-
- 太阳系是地球级的超类。
- 太阳系火星级的超类。
- 地球和火星是太阳系的子类。
- 月球是地球和太阳系的子类。
JAVA
class SolarSystem { } class Earth extends SolarSystem { } class Mars extends SolarSystem { } public class Moon extends Earth { public static void main(String args[]) { SolarSystem s = new SolarSystem(); Earth e = new Earth(); Mars m = new Mars(); System.out.println(s instanceof SolarSystem); System.out.println(e instanceof Earth); System.out.println(m instanceof SolarSystem); } } |
truetruetrue
在一个子类中可以做什么?
在子类中,我们可以按原样继承成员、替换成员、隐藏成员或用新成员补充成员:
- 继承的字段可以像其他字段一样直接使用。
- 我们可以在子类中声明不在超类中的新字段。
- 继承的方法可以直接使用。
- 我们可以写一个新的 例子 子类中的方法,该方法与超类中的方法具有相同的签名,因此 最重要的 它(如上面的例子所示, toString() 方法被重写)。
- 我们可以写一个新的 静止的 子类中的方法,该方法与超类中的方法具有相同的签名,因此 躲藏 信息技术
- 我们可以在子类中声明不在超类中的新方法。
- 我们可以编写一个子类构造函数,隐式地或通过使用关键字来调用超类的构造函数 超级的 .