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