First of all, the inheritance: integration is a polymorphic one, so that inheritors also have some characteristics of the successor.
Let's take a look at an example: Let's create an animal class that represents the animal species.
public class Animal {
The weight of the animal
private int weight;
public int getweight () {
return weight;
}
public void setweight (int weight) {
This.weight = weight;
}
}
Then we create a subclass of the dog class, inheriting the animal class, and the dog class has the properties and methods of the parent class animal.
We added a method to describe dog barking in the dog class.
public class Dog extends Animal {
Bark is the way the dog barks.
public void Bark () {
System.out.println ("wang~~~~~~wang~~~");
}
}
Finally, let's test if the dog really has some of the methods and properties of the parent class animal.
public class Mydog {
public static void Main (string[] args) {
Dog Mydog=new Dog ();
Mydog.setweight (200);
System.out.println ("My Dog ' s Weight is" +mydog.getweight ());
Mydog.bark ();
}
}
Running the program, the console will get the following information, stating that the subclass dog perfectly inherits the properties and methods of the parent class animal.
Implementation of Java Inheritance