In the Novice tutorial to learn the Java record under the study, good review.
In Java, the extends keyword can be used to declare that a class is inherited from another class in the following general form:
class class subclass extends parent class {}
Now let's look at how the example is written, and create a parent class as follows:
Public classAnimal {PrivateString name; Private intID; PublicAnimal (String MyName,intmyID) {Name=MyName; ID=myID; } Public voideat () {System. out. println (name+"are eating"); } Public voidsleep () {System. out. println (name+"are sleeping"); } Public voidIntroduction () {System. out. println ("Hello, everyone! I was"+ ID +"number"+ name +"."); }}
Then create a subclass to inherit the parent class:
Public class Penguin extends Animal { publicint myid) { super (myName, myID); }}
Then the main method calls this:
Public class Main { publicstaticvoid main (string[] args) { Animal a= New Penguin (" Xiao Hong ",1); A.eat (); A.sleep (); A.introduction (); }}
This simply implements a subclass method that invokes the parent class.
Java Object-oriented inheritance