亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> java

Java 继承

Java 继承

在本教程中,我们将通过示例了解 Java 继承及其类型。

继承是 OOP 的关键特性之一,它允许我们从现有类创建新类。

创建的新类称为子类 (子类或派生类)和派生子类的现有类称为超类 (父类或基类)。

extends 关键字用于在 Java 中执行继承。例如,

class Animal {
  // methods and fields
}

// use of extends keyword
// to perform inheritance
class Dog extends Animal {

  // methods and fields of Animal
  // methods and fields of Dog
}

在上面的例子中,Dog 类是通过继承 Animal 的方法和字段来创建的 类。

在这里, 是子类和 Animal 是超类。


示例 1:Java 继承

class Animal {

  // field and method of the parent class
  String name;
  public void eat() {
    System.out.println("I can eat");
  }
}

// inherit from Animal
class Dog extends Animal {

  // new method in subclass
  public void display() {
    System.out.println("My name is " + name);
  }
}

class Main {
  public static void main(String[] args) {

    // create an object of the subclass
    Dog labrador = new Dog();

    // access field of superclass
    labrador.name = "Rohu";
    labrador.display();

    // call method of superclass
    // using object of subclass
    labrador.eat();

  }
}

输出

My name is Rohu
I can eat

在上面的例子中,我们派生了一个子类 Dog 来自超类 Animal .注意语句,

labrador.name = "Rohu";

labrador.eat();

在这里,拉布拉多Dog 的对象 .但是,名称eat()Animal 的成员 类。

自从 Animal 继承字段和方法 ,我们可以使用 Dog 的对象访问字段和方法 .

<图>

is-a 关系

在 Java 中,继承是一个 is-a 关系。也就是说,只有当两个类之间存在 is-a 关系时,我们才使用继承。例如,

在这里,汽车 可以从 Vehicle 继承 , 橙色 可以从 Fruit 继承 ,等等。


Java 继承中的方法覆盖

示例 1 ,我们看到子类的对象可以访问超类的方法。

但是,如果同一个方法同时存在于超类和子类中,会发生什么?

在这种情况下,子类中的方法会覆盖超类中的方法。这个概念在 Java 中被称为方法覆盖。

示例 2:Java 继承中的方法覆盖

class Animal {

  // method in the superclass
  public void eat() {
    System.out.println("I can eat");
  }
}

// Dog inherits Animal
class Dog extends Animal {

  // overriding the eat() method
  @Override
  public void eat() {
    System.out.println("I eat dog food");
  }

  // new method in subclass
  public void bark() {
    System.out.println("I can bark");
  }
}

class Main {
  public static void main(String[] args) {

    // create an object of the subclass
    Dog labrador = new Dog();

    // call the eat() method
    labrador.eat();
    labrador.bark();
  }
}

输出

I eat dog food
I can bark

在上面的例子中,eat() 方法存在于超类 Animal 中 和子类 Dog .

在这里,我们创建了一个对象 labrador .

现在当我们调用 eat() 使用对象 labrador , Dog里面的方法 叫做。这是因为派生类内部的方法覆盖了基类内部的方法。

这称为方法覆盖。要了解更多信息,请访问 Java 方法覆盖。

注意 :我们使用了 @Override 注解告诉编译器我们正在重写一个方法。但是,注释不是强制性的。要了解更多信息,请访问 Java 注释。


Java 继承中的 super 关键字

之前我们看到子类中的相同方法会覆盖超类中的方法。

在这种情况下,super 关键字用于从子类的方法中调用父类的方法。

示例 3:继承中的 super 关键字

class Animal {

  // method in the superclass
  public void eat() {
    System.out.println("I can eat");
  }
}

// Dog inherits Animal
class Dog extends Animal {

  // overriding the eat() method
  @Override
  public void eat() {

    // call method of superclass
    super.eat();
    System.out.println("I eat dog food");
  }

  // new method in subclass
  public void bark() {
    System.out.println("I can bark");
  }
}

class Main {
  public static void main(String[] args) {

    // create an object of the subclass
    Dog labrador = new Dog();

    // call the eat() method
    labrador.eat();
    labrador.bark();
  }
}

输出

I can eat
I eat dog food
I can bark 

在上面的例子中,eat() 方法存在于两个基类 Animal 和派生类 Dog .注意声明,

super.eat();

这里,super 关键字用于调用eat() 超类中存在的方法。

我们也可以使用 super 关键字从子类的构造函数调用超类的构造函数。要了解更多信息,请访问 Java 超级关键字


继承中的受保护成员

在 Java 中,如果一个类包含 protected 字段和方法,那么这些字段和方法可以从类的子类中访问。

示例 4:继承中的受保护成员

class Animal {
  protected String name;

  protected void display() {
    System.out.println("I am an animal.");
  }
}

class Dog extends Animal {

  public void getInfo() {
    System.out.println("My name is " + name);
  }
}

class Main {
  public static void main(String[] args) {

    // create an object of the subclass
    Dog labrador = new Dog();

    // access protected field and method
    // using the object of subclass
    labrador.name = "Rocky";
    labrador.display();

    labrador.getInfo();
  }
}

输出

I am an animal.
My name is Rocky

在上面的示例中,我们创建了一个名为 Animal 的类。该类包括一个受保护的字段:name 和一个方法:display() .

我们继承了 类继承 Animal .注意声明,

labrador.name = "Rocky";
labrador.display();

在这里,我们可以使用 labrador 访问超类的受保护字段和方法 子类的对象。


为什么要使用继承?


继承类型

继承有五种类型。

1。单一继承

在单一继承中,单个子类从单个超类扩展而来。例如,

<图>

2。多级继承

在多级继承中,子类从超类扩展,然后同一个子类充当另一个类的超类。例如,

<图>

3。分层继承

在分层继承中,多个子类从单个超类扩展而来。例如,

<图>

4。多重继承

在多重继承中,单个子类从多个超类扩展而来。例如,

<图>

注意 :Java 不支持多重继承。但是,我们可以使用接口来实现多重继承。了解更多,请访问Java实现多重继承。


5。混合继承

混合继承是两种或多种继承类型的组合。例如,

<图>

在这里,我们将分层继承和多重继承结合起来,形成了混合继承。


java

  1. C# 继承
  2. C++ 继承
  3. Java final 关键字
  4. Java instanceof 运算符
  5. Java 嵌套静态类
  6. Java 匿名类
  7. Java 单例类
  8. Java 反射
  9. Java ObjectOutputStream 类
  10. Java 泛型
  11. Java 文件类
  12. C# - 继承