Overloading overrides for Java methods

Source: Internet
Author: User

Object-oriented has three main features: encapsulation, inheritance, polymorphism

Polymorphism is required to meet the three conditions 1 to have inheritance 2 to have rewrite 3 parent class reference child class object

Many people have a lot of questions about the overloaded override of a method, let me introduce the overloaded override of the method

Method overloading is the method name repetition, loading parameters are different

Conditions to meet:

I. The method name must be the same. Two. The parameter table of the method must be different, including the type or number of parameters, to distinguish between different method bodies. 1. If the number of parameters is different, regardless of its parameter type! 2. If the number of arguments is the same, then the type of the parameter must be different. Three. The method's return type, modifiers can be the same, or different.classMethodOverloading {    voidreceive(int i) {        System.out.println("Received one int data");        System.out.println("i="+i);    }        void receive(floatf) {        System.out.println("Received one float data");        System.out.println("f="+f);    }         voidreceive(String s) {        System.out.println("Received a String");        System.out.println("s="+s);    }         publicstaticvoidmain(String [] args){        MethodOverloading m = newMethodOverloading();        m.receive(3456);        m.receive(34.56f);        m.receive("方法重载");    }}So what is the rewrite of the method? The overriding method is also known as the overriding approach is that subclasses modify the logic of the parent class definition method

public class Wine {
public void Fun1 () {
System.out.println ("The fun of Wine ...");
Fun2 ();
}

public void fun2 () {
System.out.println ("Wine Fun2 ...");
}
}

public class JNC extends wine{
/**
* @desc Subclasses overloads the parent class method
* The method does not exist in the parent class, and the parent class cannot reference the method after it has been transformed.
* @param a
* @return void
*/
public void Fun1 (String a) {
System.out.println ("JNC Fun1 ...");
Fun2 ();
}

/**
* Subclasses overriding parent class methods
* When referring to the parent class of a subclass called Fun2, the method must be called
*/
public void fun2 () {
System.out.println ("JNC Fun2 ...");
}
public static void Main (string[] args) {
Wine a = new JNC ();
A.fun1 ();
}
}

Overloading overrides for Java methods

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.