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)