Internship Training--java Foundation (3)
1 Java Inheritance1.1 Super and this keyword
Super Keyword: We can use the Super keyword to implement access to a parent class member to refer to the parent class of the current object.
This keyword: point to your own reference.
PackageYqq.study; Public classSuperdemo { Public Static voidMain (string[] args) {Newsubclass (). ShowMessage (); }}classSuperclass {inti = 50;}classSubclassextendsSuperclass {inti = 100; Public voidShowMessage () {System.out.printf ("Super.i=%d,this.i=%d\n",SuperI This. i); }}
1.2 Constructors
A subclass cannot inherit the constructor (constructor or constructor) of the parent class, but the constructor of the parent class has parameters, and the constructor of the parent class must be explicitly called through the Super keyword in the constructor of the child class with the appropriate argument list.
If the parent class has a parameterless constructor, it is not necessary to call the parent class constructor with super in the constructor of the subclass, and if the Super keyword is not used, the system automatically calls the parent class's parameterless constructor.
PackageYqq.study;classSuperclass {Private intN; Superclass () {System.out.println ("Superclass ()"); } Superclass (intN) {System.out.println ("Superclass (int n)"); This. N =N; }}classSubclassextendsSuperclass {Private intN; Subclass () {Super(300); System.out.println ("Subclass"); } PublicSubclass (intN) {System.out.println ("Subclass (int N):" +N); This. N =N; }} Public classTestsupersub { Public Static voidMain (String args[]) {Subclass SC=Newsubclass (); Subclass SC2=NewSubclass (200); }}
The output is:
Superclass (int N) subclasssuperclass () subclass (int N): 200
Why is there such a result? You can break points in eclipse to debug.
2 Java override (override) and overload (overload)
2.1 rewrite (override)
Overrides are subclasses that rewrite the implementation of the method that allows access to the parent class, and the return value and formal parameters cannot be changed. that is, the shell does not change, the core rewrite!
The benefit of overriding is that subclasses can define their own behavior as needed. That is, subclasses can implement methods of the parent class as needed.
The overriding method cannot throw a new check exception or a broader exception than the overridden method declaration. For example: A method of the parent class declares a check exception ioexception, but cannot throw Exception exception when overriding this method, because Exception is the parent of IOException, only the subclass exception of IOException can be thrown.
In object-oriented principles, rewriting means that any existing method can be rewritten. Examples are as follows:
classAnimal { Public voidYouyong () {System.out.println ("Can Swim"); } Public voidMove () {System.out.println ("Animals can move."); }}classDogextendsAnimal { Public voidMove () {System.out.println ("Dogs can run and walk."); } Public voidbark () {System.out.println ("Dogs can be called."); }} Public classTestdog { Public Static voidMain (string[] args) {Animal a=NewAnimal (); Animal b=NewDog (); Dog C=NewDog (); A.move (); A.youyong (); A.bark (); //compilation does not passB.move (); B.youyong (); B.bark (); //compilation does not passC.bark (); C.youyong (); C.bark (); }}
overriding rules for methods
- The argument list must be exactly the same as the overridden method;
- The return type must be exactly the same as the return type of the overridden method;
- Access permissions cannot be lower than the overridden methods in the parent class. For example, if a method of a parent class is declared public, overriding the method in a subclass cannot be declared as protected.
- A member method of a parent class can only be overridden by its subclasses.
- A method that is declared final cannot be overridden.
- A method declared as static cannot be overridden, but can be declared again.
- Subclasses and parent classes in the same package, subclasses can override all methods of the parent class, except for the methods declared private and final.
- Subclasses and parent classes are not in the same package, the subclass can only override non-final methods that are declared public and protected by the parent class.
- The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than the overridden method declaration, or vice versa.
- The construction method cannot be overridden.
- If you cannot inherit a method, you cannot override this method.
2.2 Heavy Duty (overload)
Overloading (overloading) is a class in which the method name is the same and the parameters are different. The return type can be the same or different.
Each overloaded method (or constructor) must have a unique list of parameter types.
The most common place is the overload of the constructor.
Overloading rules
- The overloaded method must change the parameter list (the number of parameters or the type or order is different);
- The overloaded method can change the return type;
- Overloaded methods can change the access modifier;
- Overloaded methods can declare new or broader check exceptions;
- Methods can be overloaded in the same class or in a subclass.
- The return value type cannot be used as a distinguishing criterion for overloaded functions.
Instance
Public classOverloading { Public intTest () {System.out.println ("Test1"); return1; } Public voidTestinta) {System.out.println ("Test2"); } //The following two parameter types are in different order PublicString Test (inta,string s) {System.out.println ("Test3"); return"Returntest3"; } PublicString Test (String s,inta) {System.out.println ("Test4"); return"Returntest4"; } Public Static voidMain (string[] args) {overloading O=Newoverloading (); System.out.println (O.test ()); O.test (1); System.out.println (O.test (1, "Test3")); System.out.println (O.test ("Test4", 1)); }}
the difference between overriding and overloading
Distinguishing points |
Overloaded Methods |
overriding Method |
Parameter list |
Must be modified |
Must not be modified |
return type |
can modify |
Must not be modified |
Abnormal |
can modify |
can be reduced or deleted, must not throw new or wider exceptions |
Access |
can modify |
Must not be more restrictive (can reduce the limit) |
Overrides of methods (overriding) and overloads (overloading) are different manifestations of Java polymorphism, which is a representation of polymorphism between a parent class and a subclass, and overloading can be understood as a concrete representation of polymorphism.
Beginner's Tutorial
Internship Training--java Foundation (3)