Inherited syntax:
Modifier Subclass extends Superclass {
class Definition Section
}
Inheritance (inheritance) is implemented by the extends keyword, where subclass is called a subclass, Superclass is called a parent class, a base class, or a superclass. Modifier if it is public, the class is visible throughout the project, and if there is no public modifier, the class is visible only in the current package; and protected modified classes.
Java Inheritance is one of the most prominent features of object-oriented. Inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities .
Java Inheritance is the technique of building a new class using the definition of an existing class, and the definition of a new class can either add new data or new functionality, or use the functionality of the parent class, but not selectively inherit the parent class. This technique makes it easy to reuse the previous code, greatly shortening the development cycle and reducing development costs. For example, you can separate Mr. Foo to define a class called a car, the car has the following properties: Body size, color, steering wheel, tires, and by car this class derives two classes of cars and trucks, adds a small trunk to the car, and adds a large container for the truck.
The inheritance relationship between classes and classes can be expressed in UML notation, where the parent class is also called a superclass or base class , and a subclass is also called a derived class . The parent class is the generalization of the subclass, which is the specialization (materialization )of the parent class.
Java does not support multiple inheritance , single inheritance makes JAVA inheritance simple, a class can have only one parent class, easy to manage the program, and a class can implement multiple interfaces, thereby overcoming the disadvantages of single inheritance.
The application of the principle of inheritance in object-oriented programming is in the general - Special structure formed by general class and special class. The properties and operations common to the object instances of the generic class and all the object instances of all special classes are defined explicitly in a generic class at a time, and in a particular class no longer repeats the definition of what is already defined in the generic class, but semantically, the special class automatically and implicitly owns its generic class ( and all the higher-level generic classes ) are defined in the properties and operations. Objects of a particular class have all or part of the properties and methods of their generic classes, called special classes that inherit from generic classes.
Inheritance is the expression of an intersection between an object class, which enables a class of objects to inherit from another type of object.data membersand Member methods. If the classBInheriting ClassesA, it belongs toBobject has the classAall or part of the nature of(Data Properties)and Function(Operation), we call the inherited classAto bebase class, parent class, orSuper Class, which is called inheriting classBto beAof theDerived Classesor sub-class.
Then an example
Existing a parent class pet
/*** Pet, Dog and Penguin parent class *@authorPKU Jade Bird*/ Public classPet {PrivateString name = "Anonymous";//Nickname Private intHealth = 100;//Health Value Private intLove = 0;//Degree of intimacy /*** Non-parametric construction method*/ PublicPet () { This. Health = 95; System.out.println ("Non-parametric construction method for pet implementation"); } /*** Method of construction of a parameter *@paramName Nickname*/ PublicPet (String name) { This. Name =name; } PublicString GetName () {returnname; } Public intGethealth () {returnHealth ; } Public intGetlove () {returnLove ; } /*** Output Pet information*/ Public voidprint () {System.out.println ("The Confession of the pet: \ n My name is" + This. Name + ", my health value is" + This. Health+ ", my intimacy with the master is" + This. Love + ". "); }}
There's another subclass of Dog.
/*** Dog class, pet sub-class *@authorPKU Jade Bird*/ Public classDogextendsPet {PrivateString strain;//Variety /*** Method of construction of a parameter *@paramName Nickname *@paramStrain Varieties*/ PublicDog (string name, string strain) {Super(name);//This.name=name cannot be used here; This. Strain =strain; } PublicString Getstrain () {returnstrain; }}
Subclass inherits to the parent class, so the method of the parent class can be called in the Main method with the subclass
/** * Test class, Test class inheritance @param beida Jade Bird */public staticvoid main (string[] args) { // 1. Create pet Object PET and output information New Pet ("Beibei"); Pet.print (); // 2. Create a dog object with the output information Dog dog = new Dog ("Europe and Europe", "Snow schnauzer"); Dog.print (); }}
Inheritance in Java