Java Object-oriented inheritance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.