Java Foundation-One of the four elements polymorphism-rewrite (overriding, overwrite)-Reload (overloading)

Source: Internet
Author: User
Tags throw exception

Polymorphism:

Java's method overloading is that you can create multiple methods in a class that have the same name but have different parameters and different definitions. The methods used to determine which method to use in Java are overridden by the number of different arguments and parameter types passed to them when invoking the method, that is, the subclasses may have other special definitions of the methods in the parent class, and the contents of the methods in the parent class need to be overridden on the side of the calculation.  Method name, return type, method parameter must be the same case, that is, rewrite polymorphism is an object-oriented programming feature, and Method-independent, simply said, is the same method can be different from the input data, to make different processing, that is, the method of overloading-there are different parameter list (static polymorphism) And when the subclass inherits the same method from the parent class, enters the same data, but to make a response different from the parent class, you overwrite the parent class method, which is overriding the method in the subclass-the same argument, the different implementation (dynamic polymorphism)
means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object. (Sending a message is a function call)

The role of polymorphism:

  eliminates the coupling relationship between types .

Rewrite (overriding)

Meaning:

(1) The polymorphism between the parent class and the child class, redefining the function of the parent class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding).

In Java, subclasses can inherit methods from the parent class without having to rewrite the same method. But sometimes subclasses do not want to inherit the parent class's methods, but want to make some changes, which requires a method of rewriting.

Method overrides are also called method overrides.

(2) The method in the Kawai class has the same method name, return type, and parameter table as a method in the parent class, and the new method overwrites the original method.

If you need a method from the parent class, you can use the Super keyword, which references the parent class of the current class.

(3) The access adornment permission of the subclass function can not be less than the parent class;

Concept: The mechanism by which an object method is called.

The following code tests

Sub-class

 Packagecom;/*** Sub-class *@authorHuage **/ Public classTestextendstest1{ Public Static voidMain (string[] args) {NewTest ();//To test the subclass and parent overload relationships and call relationships    }         PublicTest () {System.out.println ("Test"); Test ("Test"); Super. Test ("test");//the methods in the parent class are overloaded, so a method in the parent class that must be super can be called to execute    }    /*** Reload the test method in the parent class*/     Public voidTest (String name) {System.out.println ("Who:" +name+ "; Test:test"); }    /*** Static method does not call does not execute*/     Public Static voidtest1 () {System.out.println ("Test static void"); }}

Parent class

 Packagecom;/*** Parent class *@authorHuage **/ Public classTest1 { PublicTest1 () {System.out.println ("1"); //When a quilt class is overridden, a method in a subclass is called when the subclass is instantiatedTest ("Test1");  This. Test ("Test1"); //test1 ();    }    /*** When instantiating an object as a subclass, there is a subclass in which this method is overridden, the method that is called by the Quilt class in the original (parent class), the subclass calls the method that is overridden by the subclass * such as a method in the subclass to call the parent class requires super to call the * itself in this class cannot invoke the /c2>*/     Public voidTest (String name) {System.out.println ("Who:" +name+ "; 1:1"); }    /*** The static method is not overridden/overwritten, but can have the same method in the subclass, and in the call itself this is called its own method*/     Public Static voidtest1 () {System.out.println ("Test1 Static 111"); }        /*** The final method cannot be overridden/overwritten, the subclass cannot have the same method (method name, return type, parameters in the same case)*/     Public Final voidtest2 () {System.out.println ("Test2 Final 11111"); }}

Execute output results

1who:test1;test:testwho:test1;test:testtestwho:test;test:testwho:test; 1:1

Overriding methods can only exist in an inheritance relationship, overriding methods can only override the parent class's non-private methods.

Method One but is final, no matter if the method is public,protected and modified by default, it cannot be overridden

rules for overriding methods :

1. The parameter list must be exactly the same as the overridden method, otherwise it cannot be called overridden instead of overloaded. 2, the returned type must always be the same as the return type of the overridden method, otherwise it cannot be called overriding but overloaded. 3. The access modifier must be greater than the access modifier of the overridden method (public>protected>default>private 4. The overriding method must not throw a new check exception or a more general type of check exception than the overridden method declares. For example: A method of the parent class declares a check exception ioexception, in overriding this method is not able to throw exception, can only throw IOException subclass exception, can throw non-check exception. 

The main advantage of rewriting is the ability to define characteristics specific to a subclass:

 Public class father{   publicvoid  speak () {       System.out.println (Father);    }}  Public class extends father{    publicvoid  speak () {        System.out.println ("Son"  );   }}

This is also called polymorphism, where overriding methods can only exist in an inheritance relationship, overriding methods that only override the parent class's non-private methods.

When the Father class speak () method in the example above is private, the son class cannot re-write the Father class speak () method, at which point the Son class speak () method is equivalent to a speak () method defined in the son class.

When the Father class speak () method is final, the son class cannot override the Father class speak () method at all, regardless of whether the method is public,protected and is modified by default.

The compiler will make an error when trying to compile the code. Cases:

 Public class father{   finalpublicvoid  speak () {       System.out.println (" Father ");}    }  Public class extends father{    publicvoid  speak () {       System.out.println ("Son"  );    }}        // The compiler will make an error;

When the Father class speak () method is decorated by default, it can be overridden only in the same package, by its subclasses, if it is not in the same package.

When the Father class speak () method is protoeted, it is overridden not only in the same package but by its subclasses, but also by subclasses of different packages.

Overloading (overloading)

    (1) method overloading is a means of allowing classes to handle different types of data in a uniform manner. Multiple functions with the same name exist at the same time, with different number/types of parameters. Overloaded overloading is a representation of polymorphism in a class.    (2) Java's method overloading is the ability to create multiple methods in a class that have the same name but have different parameters and different definitions. The method is called polymorphism by the number of different arguments passed to them and by the type of parameter to determine which method to use.    (3) When overloading, the method name is the same, but the parameter type and number are different, the return value type can be the same or different. The return type cannot be used as a distinguishing criterion for overloaded functions.

eg

    voidBark ()//the Bark () method is an overloaded method{System.out.println ("No barking!");  This. Bark ("female", 3.4); }    voidBark (String m,DoubleL//Note: The return value of the overloaded method is the same,{System.out.println ("A barking dog!");  This. Bark (5, "China"); }    voidBarkinta,string N)//overloaded methods cannot be distinguished by return values, but only by "parameter type" and "Class name"{System.out.println ("A Howling Dog"); }

Instead of overloading the rules:

1, must have a different parameter list, 2, can have a different return type, as long as the parameter list is different, 3, can have different access modifiers, 4, can throw different exceptions;

The difference between overrides and overloads is that:

Overriding polymorphism works, and calling overloaded methods can greatly reduce the amount of code input, and the same method name can have different functions or return values as long as it passes different parameters inside. With good rewriting and overloading, you can design a clear and concise class that can be said to be overridden and overloaded in a way that is very unusual in the process of writing code.

Java Foundation-One of the four elements polymorphism-rewrite (overriding, overwrite)-Reload (overloading)

Related Article

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.