Java basic syntax-object-oriented (2)

Source: Internet
Author: User

a Inheritance

in the program, inheritance describes the relationship between things, through inheritance can make a relationship between the various things system. in Java , class inheritance refers to building a new class on the basis of an existing class, the new class is called a subclass, the existing class is called the parent class, and the subclass automatically owns all inheritable properties and methods of the parent class.

1.1 inherited formats & uses

in a program, if you want to declare a class to inherit from another class, you need to use the extends keyword.

Format:

Class Subclass extends parent class {}

Application of inheritance in Java

/** Define Employee Class employee*/classEmployee {String name;//define the Name property//Define how employees work     Public voidWork () {System.out.println ("Work wholeheartedly"); }}/** Define research and Development department employee class developer inherit employee Class employee*/classDeveloperextendsEmployee {//define a method for printing a name     Public voidPrintname () {System.out.println ("Name=" +name); }}/** Define test classes*/ Public classEXAMPLE01 { Public Static voidMain (string[] args) {Developer d=NewDeveloper ();//Create a research and Development department employee class objectD.name = "Xiao Ming";//assign a value to the Name property of the employee classD.printname ();//call the employee's Printname () methodD.work ();//Call the work () method inherited by the developer class    }}

Java basic syntax-object-oriented (2)

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.