java继承的范例
Inheritance in java is one of the core concepts of Object Oriented Programming. Java Inheritance is used when we have is-a relationship between objects. Inheritance in Java is implemented using extends
keyword.
Java中的继承是面向对象编程的核心概念之一。 Java的继承是在使用时我们已经是-一个对象之间的关系。 Java中的继承是使用extends
关键字实现的。
Inheritance in Java is the method to create a hierarchy between classes by inheriting from other classes.
Java中的继承是一种通过从其他类继承来在类之间创建层次结构的方法。
Java Inheritance is transitive – so if Sedan extends Car
and Car extends Vehicle
, then Sedan is also inherited from Vehicle class. The Vehicle becomes the superclass of both Car and Sedan.
Java继承是可传递的-因此,如果Sedan extends Car
和Car extends Vehicle
,则Sedan也将从Vehicle类继承。 车辆成为轿车和轿车的超类。
Inheritance is widely used in java applications, for example extending Exception class to create an application specific Exception class that contains more information like error codes. For example NullPointerException.
继承在Java应用程序中被广泛使用,例如,扩展Exception类以创建特定于应用程序的Exception类,该类包含更多信息,例如错误代码。 例如NullPointerException 。
Every class in java implicitly extends java.lang.Object
class. So Object class is at the top level of inheritance hierarchy in java.
java中的每个类都隐式扩展了java.lang.Object
类。 因此,Object类是Java中继承层次结构的顶层。
Let’s see how to implement inheritance in java with a simple example.
让我们来看一个简单的示例,如何在Java中实现继承。
Superclass: Animal
超类:动物
package com.journaldev.inheritance;
public class Animal {
private boolean vegetarian;
private String eats;
private int noOfLegs;
public Animal(){}
public Animal(boolean veg, String food, int legs){
this.vegetarian = veg;
this.eats = food;
this.noOfLegs = legs;
}
public boolean isVegetarian() {
return vegetarian;
}
public void setVegetarian(boolean vegetarian) {
this.vegetarian = vegetarian;
}
public String getEats() {
return eats;
}
public void setEats(String eats) {
this.eats = eats;
}
public int getNoOfLegs() {
return noOfLegs;
}
public void setNoOfLegs(int noOfLegs) {
this.noOfLegs = noOfLegs;
}
}
The Animal is the base class here. Let’s create a Cat class that inherits from Animal class.
动物是这里的基类。 让我们创建一个从Animal类继承的Cat类。
Subclass: Cat
子类别:猫
package com.journaldev.inheritance;
public class Cat extends Animal{
private String color;
public Cat(boolean veg, String food, int legs) {
super(veg, food, legs);
this.color="White";
}
public Cat(boolean veg, String food, int legs, String color){
super(veg, food, legs);
this.color=color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Notice that we are using extends
keyword to implement inheritance in java.
注意,我们使用extends
关键字在java中实现继承。
Java Inheritance Test Program
Java继承测试程序
Let’s write a simple test class to create Cat object and use some of its methods.
让我们编写一个简单的测试类来创建Cat对象并使用其某些方法。
package com.journaldev.inheritance;
public class AnimalInheritanceTest {
public static void main(String[] args) {
Cat cat = new Cat(false, "milk", 4, "black");
System.out.println("Cat is Vegetarian?" + cat.isVegetarian());
System.out.println("Cat eats " + cat.getEats());
System.out.println("Cat has " + cat.getNoOfLegs() + " legs.");
System.out.println("Cat color is " + cat.getColor());
}
}
Output of the above program is shown in below image.
下图显示了上述程序的输出。
Cat class doesn’t have getEats()
method but still it works because it’s inherited from Animal class.
Cat类没有getEats()
方法,但仍然有效,因为它是从Animal类继承的。
Cat c = new Cat(); //subclass instance
Animal a = c; //upcasting, it's fine since Cat is also an Animal
我们可以创建子类的实例,然后将其分配给超类变量,这称为upcasting 。 以下是上播的简单示例: Cat c = new Cat();
Animal a = c;
Cat c1 = (Cat) a; //explicit casting, works fine because "c" is actually of type Cat
Note that Compiler won’t complain even if we are doing it wrong, because of explicit casting. Below are some of the cases where it will throw ClassCastException
at runtime.
Cat c = new Cat();
Animal a = c;
Cat c1 = (Cat) a; //explicit casting, works fine because "c" is actually of type Cat
注意,由于显式转换,即使我们做错了,编译器也不会抱怨。 在某些情况下,它将在运行时引发ClassCastException
。
instanceof
instruction to check the inheritance between objects, let’s see this with below example.Cat c = new Cat();
Dog d = new Dog();
Animal an = c;
boolean flag = c instanceof Cat; //normal case, returns true
flag = c instanceof Animal; // returns true since c is-an Animal too
flag = an instanceof Cat; //returns true because a is of type Cat at runtime
flag = an instanceof Dog; //returns false for obvious reasons.
我们可以使用instanceof
指令检查对象之间的继承,下面的示例让我们来看一下。 I have recently published two videos on YouTube explaining Inheritance in detail with sample programs, you should watch them below.
我最近在YouTube上发布了两个视频,其中通过示例程序详细解释了继承,您应该在下面观看。
演示地址
演示地址
Reference: Oracle Documentation
参考: Oracle文档
翻译自: https://www.journaldev.com/644/inheritance-java-example
java继承的范例