Javase Getting Started learning 15:java Object-oriented J rewrite (override) and overloading (overload)

Source: Internet
Author: User

one override (override)

If the subclass is not satisfied with the method that inherits the parent class, it is possible to override the method inherited by the parent class. Overrides are subclasses of the implementation of methods that allow access to the parent class

process to be rewritten. Neither the return value nor the formal parameter can be changed. That is, the shell is unchanged, the core rewrite. The method of the subclass is called first when the method is called.

Syntax rules:

A return value type

B Method Name

C parameter Type and number

Is the same as the method inherited by the parent class, which is called the override of the method.

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. In the face

To the object principle, overriding means that any existing method can be rewritten.

Instance:

Test.java Source file Code:

Class animal{   //animal class method public   void Move () {      System.out.println ("Animal can Move");}   } Class Dog extends animal{   //Override the Move () method of the parent class Animal class public   void Move () {      System.out.println ("Dog can run and walk");   }   }public class test{public   static void Main (String args[]) {      Animal a = new Animal ();//Animal Object      Ani Mal B = new Dog (); Dog Object      //method of executing Animal class      a.move ();      The method of executing the Dog class      b.move ();   }   }

The results of the above example compilation run as follows:


As you can see in the example above, although B belongs to the animal type, it runs the Move () method of the dog class. This is because during the compilation phase,

only is the reference type of the check parameter. At run time, however, the Java Virtual machine (JVM) specifies the type of object and the method that runs the object. So in the above

Example in the animal class, it is possible to compile successfully because the move () method exists in the , however, the run- time operation is a method of a particular object.

Consider the following example:

Test.java Source file Code:

Class animal{   //animal class method public   void Move () {      System.out.println ("Animal can Move");}   } Class Dog extends animal{   //Override the Move () method of the parent class Animal class public   void Move () {      System.out.println ("Dog can run and walk");   }      Dog's own unique bark method public   void Bark () {      System.out.println ("Dog can Bark");}   }   public class test{public   static void Main (String args[]) {      Animal a = new Animal ();//Animal object      Animal b = New Dog (); Dog Object      //method of executing Animal class      a.move ();      The method of executing the Dog class      b.move ();      B.bark ();   }   }

The results of the above example compilation run as follows:


The program throws a compilation error because the reference type of B animal does not have a bark () method.

the overriding considerations for methods are:

A parameter list must be exactly the same as the overridden method;

b The return type must be exactly the same as the return type of the overridden method;

C Access permissions cannot be higher than the overridden methods in the parent class. For example, if a method of the parent class is declared public, the subclass

cannot be declared as protected if overridden in this method.

The member method of the D parent class can only be overridden by its subclasses.

The method declared as final by E cannot be overridden.

F the method declared as static cannot be overridden, but can be declared again.

If the G subclass and the parent class are in the same package, the subclass can override all methods of the parent class, except for the method declared private and final.

H 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.

I override the method to 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 broader mandatory exception than the overridden method declaration, is otherwise possible.

The J constructor method cannot be overridden.

K If you cannot inherit a method, you cannot override this method.

use of the Super keyword

Use the Super keyword when you need to call the overridden method of a parent class in a subclass. Used inside an object, you can represent the parent class object.

Access the properties of the parent class: Super.age;

Methods to access the parent class: Super.eat ();

use of super:

The constructor method of a subclass must be called during the construction of its parent class.

b if the constructor method of the calling parent class is not displayed in the constructor of the subclass, the system defaults to calling the parent class without a reference to the constructor method.

c If you explicitly call the constructor method, you must construct the method in the first row of the child class,

D If the construction method in the subclass does not explicitly call the constructor of the parent class, and the parent class does not have a parameterless constructor, the edit error occurs.

Instance:

Class animal{public   void Move () {      System.out.println ("Animal can Move");   }   }   Class Dog extends animal{public   void Move () {      super.move ();//Apply Super class method      System.out.println ("Dog can run and walk");   }   }public class test{public   static void Main (String args[]) {      Animal b = new Dog ();//Dog Object      b.move (); Method of executing the Dog class   }   }

The results of the above example compilation run as follows:


two 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. Per-overloaded party

Method must have a unique list of parameter types. Overloading a constructor method is only a special case of an overloaded method.

Overloading Rules:

A overloaded method must change the parameter list;

b The overloaded method can change the return type;

C Overloaded method can change the access modifier;

d The overloaded method can declare a new or broader check exception;

The E method can be overloaded in the same class or in a subclass.

To construct a method overload rule:

A construction method is the same as the class name

B Construction method does not return a value

C constructor Method parameter list number is different

D constructor method parameter list order is different

The e-construction method automatically selects the corresponding method according to the different parameters when it is called.   

Common method Overloaded Instances:

Overloading.java Source file Code:

public class Overloading {public int test () {System.out.println ("test1"), return 1;} public void Test (int a) {SYSTEM.OUT.PR Intln ("Test2");} The following two parameter types have different order of public string test (int a,string s) {System.out.println ("test3"), return "RETURNTEST3";} public string Test (String s,int a) {System.out.println ("test4"); return "Returntest4";} public static void Main (string[] args) {Overloading o = new overloading (); System.out.println (O.test ()); O.test (1); System.out.println (O.test (1, "test3")); System.out.println (O.test ("Test4", 1));}}
Operation Result:

To construct a method overload instance:

Test.java Source file Code:

public class Test {public         int a;public float b;public Test () {System.out.println ("I am a non-parametric constructor method");} Public Test (int a,float b) {this.a=a;this.b=b; System.out.println ("I am a constructor with parameters");} public static void Main (string[] args) {test t1=new test (); Test t2=new test (1,2F);}}

Operation Result:


Three summarizes the difference between overrides and overloads:



Javase Getting Started learning 15:java Object-oriented J rewrite (override) and overloading (overload)

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.