overloading and overriding in Java

Source: Internet
Author: User

In reading the Java book, see the overloaded and overwrite this paragraph, some do not understand, and then slowly understand. Now write down some of my ideas.

Overloading refers to a method in which a different method name can be used, but the method of execution differs depending on the number of values passed in or the class.

Package czfx;

public class czfx{
public static void Main (String args[]) {
Class_one p=new Class_one ();
P.talk ();
P.talk (2);
(P.talk);
}
}
Class Class_one
{
void Talk () {
SYSTEM.OUT.PRINTLN ("No incoming value!") ");
}
void talk (int i) {
System.out.println ("There is an incoming value" +i+ "!) ");
}
void talk (int i,int j) {
SYSTEM.OUT.PRINTLN ("There are two incoming values" +i+j+ "!) ");
}
}

Operation Result:

Overwriting is when the subclass inherits the parent class, and the method name of the child class and the parent class can be the same, at which point the method in the subclass overrides the method in the parent class. If the method in the parent class is private, the class will not be established.

Package czfx;

public class czfx{
public static void Main (String args[]) {
Class_one p1=new Class_one ();
Class_two p2=new class_two ();
P1.talk ();
P2.talk ();

}
}
Class Class_one
{
void Talk () {
System.out.println ("This is a test!");
}
}
Class Class_two extends Class_one
{
void Talk () {
System.out.println ("This is A test!");
}
}
Operation Result:

Finish

overloading and overriding in Java

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.