Three main features of object-oriented: encapsulation , inheritance , polymorphism
Java implements inheritance through the extends keyword, and it is a single inheritance, a subclass can have only a direct parent class, but the parent class can also have a parent class ...
Java.long.Object is the parent class of all classes, and if a class does not declare who inherits from it, the default is to inherit from the Java.long.Object class
By inheritance, subclasses can obtain all the properties and methods of the parent class, while expanding.
code example:
1 PackageExtendss;2 3 /**4 * Class inheritance Demo code5 * This is a parent class train train class6 * Inherited from the object class by default7 * 8 * */9 Public classTrain {Ten One //Properties A PrivateString Trainsnum;//Train Number - - PrivateString Trainscount;//Number of trains the - PrivateString Trainstype;//Compartment Type - - //Method + //because I am a little fascinated in the encapsulation ... - //so I wrote two ways to solve a little doubt. + //call your own property within your own method A //do not need to pass the Get method ... at Public voidSpeakInfo1 () { -System.out.println ( This. Gettrainsnum () + "Train is by" + This. Gettrainscount () + "section" -+ This. Gettrainstype () + "type compartment"); - } - - Public voidSpeakInfo2 () { inSystem.out.println (Trainsnum + "train is made by" + Trainscount + "section" -+ trainstype + "type compartment"); to } + - the /**----------Get/set Method------------**/ * $ PublicString Gettrainsnum () {Panax Notoginseng returnTrainsnum; - } the + Public voidsettrainsnum (String trainsnum) { A This. Trainsnum =Trainsnum; the } + - PublicString Gettrainscount () { $ returnTrainscount; $ } - - Public voidSettrainscount (String trainscount) { the This. Trainscount =Trainscount; - }Wuyi the PublicString Gettrainstype () { - returnTrainstype; Wu } - About Public voidSettrainstype (String trainstype) { $ This. Trainstype =Trainstype; - } - -}
PackageExtendss;/*** This is the subclass passenger train class * inherits from the train class **/ Public classPassengertrainextendsTrain {//temporarily an empty method Public Static voidMain (string[] args) {//because the train class is inherited, the methods in the parent class can be used even if the method is emptyPassengertrain pt =NewPassengertrain (); //Assigning a value to a propertyPt.settrainscount ("18"); Pt.settrainsnum ("K8359/k8360"); Pt.settrainstype ("25T"); //Calling MethodsPt.speakinfo1 (); The k8359/k8360 train is a Pt.speakinfo2 () consisting of 18-section 25T carriages; The k8359/k8360 train is made up of 18-section 25T carriages}
Even if the subclass is an empty class, you can get properties and methods by inheriting the parent class ~ ~
subclasses overriding methods of the parent class
Subclasses can also override methods that are not applicable in the parent class while extending the properties and methods of the parent class, but must be the same as the parent method method name and the same parameter list
Subclasses override the methods of the parent class, and then want to use the methods in the parent class, you can call through the keyword super ...
For a static decorated class method, it can be called directly using the parent class name.
code example
PackageExtendss;/*** This is the subclass passenger train class * inherits from the train class **/ Public classPassengertrainextendsTrain {//Extended Properties PrivateString trainload;//passenger capacity//overriding methods in a parent class Public voidSpeakInfo1 () {System.out.println ( This. Gettrainsnum () + "Train is by" + This. Gettrainscount () + "knots" + This. Gettrainstype () + "type compartment, total can ride" + trainload + "passengers"); } //Super calls a method in the parent class Public voidSpeakinfofu () {Super. SpeakInfo1 (); } Public Static voidMain (string[] args) {//because the train class is inherited, the methods in the parent class can be used even if the method is emptyPassengertrain pt =NewPassengertrain (); //Assigning a value to a propertyPt.settrainscount ("18"); Pt.settrainsnum ("K8359/k8360"); Pt.settrainstype ("25T"); //The calling method is annotated here first.//Pt.speakinfo1 (); //Pt.speakinfo2 (); /*------------------------------*/ //assigning values to extended attributesPt. TrainLoad = "2056"; //calling a subclass override methodpt.speakinfo1 ();//The k8359/k8360 train is made up of 18-section 25T carriages and can be used by 2,056 passengers .
// calling the Parent class original method Pt.speakinfofu (); The//k8359/k8360 train is made up of 18-section 25T carriages }}
The train is really ... The model is also true ... The number of passengers is calculated according to the capacity of 118 dining car 50. Purely Entertaining ~ ~
Subclasses use super to invoke the constructor of the parent class
The constructor of the parent class can be called by super in the constructor of the subclass, but using super to call the parent class constructor must appear in the first row of the subclass constructor.
code example:
PackageSuperss;/*** Parent Object * **/ Public classFu {//constructor Function PublicFu () {System.out.println ("No parameter constructor in parent object"); } PublicFu (String A1) { This(); This. A1 =A1; System.out.println ("Constructor with one parameter in the parent object"); } PublicFu (String a1,string A2) { This(A1); This. A2 =A2; System.out.println ("Constructor with a parameter in the parent object"); } //Defining Properties PublicString A1; PublicString A2;}
PackageSuperss; Public classZiextendsFu {//Defining Constructors PublicZi (String a1,string a2,string A3) {//calling the parent class constructor Super(A1, A2); This. A3 =A3; System.out.println ("Constructors in subclasses"); } //Defining Properties PublicString A3; //Defining Methods Public voidinfo () {System.out.println ("Methods in Subclasses"); } Public Static voidMain (string[] args) {System.out.println ("----------"); Zi Zi=NewZi ("A1", "A2", "A3"); System.out.println ("----------"); Zi.info (); } //Operation Result://---------- //non-parametric constructors in parent objects//a constructor with a parameter in the parent object//a constructor with a parameter in the parent object//Constructors in subclasses//---------- //Methods in subclasses }
The creation object is always executed from the top-level constructor of the inheriting tree where the class is located, then executes down, then executes the class constructor, and a parent class invokes the other constructors in the same class through this, and then executes multiple constructors of that parent in turn.
-------inheritance of three basic features of "Java Foundation" object-oriented