The difference between overloading and overriding in Java

Source: Internet
Author: User
Tags modifier throw exception

First, let's say: 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.


The following is an example of overloading:
Package c04.answer;//This is the pack name.
This is the first programming method of this program, first creating an instance of the dog class in the main method, and then invoking the different bark methods with the This keyword in the method of constructing the dog class.

The different overloaded methods bark are differentiated according to their parameter types.

Note: In addition to the constructor, the compiler prohibits invoking the constructor anywhere else.
Package c04.answer;

public class Dog {
Dog ()
{
This.bark ();
}
The Void Bark ()//bark () method is an overloaded method
{
System.out.println (\ "No barking!\");
This.bark (\ "Female\", 3.4);
}
void Bark (String m,double L)//Note: The return value of the overloaded method is the same,
{
System.out.println (\ "A barking dog!\");
This.bark (5, \ "China\");
}
void bark (int a,string N)//cannot differentiate overloaded methods with return values, but only with "parameter type" and "Class name"
{
System.out.println (\ "A Howling dog\");
}

public static void Main (string[] args)
{
Dog dog = new Dog ();
Dog.bark (); [Page]
Dog.bark (\ "male\", \ "yellow\");
Dog.bark (5, \ "China\");


And then we'll talk about rewriting (overriding) .


(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;
The following is an example of rewriting:


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

Insider of dynamic bindings :

1. The compiler checks the type and method name of the object declaration to obtain all candidate methods. Try to comment out the test of the base class above, and then compile it.

2. Overload resolution: The compiler checks the parameter type of the method invocation, and selects the only one from the above candidate (implicit type conversions are in between).

If the compiler finds more than one or does not find it, the compiler will make an error. Try to comment out the test (Byte b) of the previous example base class, and the result is 1 1.

3, if the method type is priavte static final, Java with static compilation, the compiler will know exactly what the call
A method.

4. When a program runs and uses dynamic binding to invoke a method, the virtual machine must invoke a method version that matches the actual type of the object.

In the example, the actual type that B points to is testoverriding, so b.test (0) calls the subclass's test.

However, the subclass does not rewrite test (byte b), so B.test ((byte) 0) calls the parent class's Test (Byte b).

If the parent class (Byte B) is commented out, the second step implicitly converts the type to int, and the final call is the subclass of test (int i).

Learning Summary :

Polymorphism is a feature of object-oriented programming, which is independent of the method,
Simply put, the same method can be used to make different processing according to the input data, that is, the method
Overloads--There are different parameter lists (static polymorphism)

And when the subclass inherits from the same method as the parent class, enter the same data, but to make a response different from the parent class, you overwrite the parent class method,

That is, overriding the method in the subclass-the same parameter, different implementations (dynamic polymorphism)

three major features of OOP: inheritance, polymorphism, encapsulation.

public class Base
{
void Test (int i)
{
System.out.print (i);
}
void Test (Byte b)
{
System.out.print (b);
}
}
public class Testoverriding extends Base
{
void Test (int i)
{
i++;
System.out.println (i);
}
public static void Main (STRING[]AGRS)
{
Base b=new testoverriding ();
B.test (0)
B.test ((byte) 0)
}
}


At this point the output is 1 0, which is the result of dynamic binding at run time.

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

public class father{

public void Speak () {

System.out.println (Father);

}

}

public class Son extends father{

public void 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{

Final public void speak () {

System.out.println ("Father");

}

}

public class Son extends father{

public void Speak () {

System.out.println ("Son");

}

}//compiler will 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.

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 (public>protected>default>private) of the overridden method

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 states that 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.

Instead of overloading the rules:

1, must have a different list of parameters;

2, can have not scold return type, as long as the parameter list is different;

3, can have different access modifiers;

4, can throw different anomalies;

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.

The difference between overloading and overriding in Java

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.