Override (Override)
Like what
Defining the Father Class
1: Name, meal method, eat can窝窝头pickles.
2: Define son class, inherit father
The 1:son class does not define any members, the subclass creates the object, and the method of eating can still be called.
2: Father's way of eating, son does not want to eat. Son himself defined the method of eating.
1: There is a way to eat in the parent class, there are 2 ways to eat in the subclass, exactly the same, but the method body is not the same.
2: Two functions in a class are identical and are not allowed.
1: Compile run, execute the method of subclass.
2: Using the parent class method, in the subclass method, use Super. The parent class method name.
-
Class Father {
-
String name;
-
-
void Eat () {
-
System.out.println ("Eat litters");
-
}
-
}
-
-
Class Son extends Father {
-
-
public void Eat () {///inheritance enables subclasses to enhance the parent class's methods
-
System.out.println ("Two side dishes");
-
System.out.println ("two cups");
-
System.out.println ("Hot drink");
-
System.out.println ("Come One");
-
}
-
}
-
-
Class Demo8 {
-
-
public static void Main (string[] args) {
-
Son s = new Son ();
-
Methods for executing subclasses
-
S.eat ();
-
-
}
-
}
3, the phenomenon is called rewriting (overwrite override)
1, in inheritance, a subclass can define the same name as the parent class and a function that has a consistent argument list, which is called a function rewrite
4, the premise
1, there must be an inheritance relationship
5, features
1, when the subclass overrides the function of the parent class, then the object of the child class, if called, must call the overridden function, which can be called by the Super keyword for the overriding function of the parent class.
2, inheritance can make subclasses enhance methods of parent class
6, details
1, the function name must be the same
2, parameter list must be the same
3, when a subclass overrides a function of the parent class, the function must have access permissions greater than or equal to the access rights of the parent class's function or it will compile an error
4, when a subclass overrides a function of the parent class, the return value type must be the return value type of the parent class or the subclass of the return value type, and cannot return a larger data type than the parent class, such as the return value type of the subclass function is Object
1: Define a B class C B extends a
A geta () is defined in the 2:father class;
Rewrite Geta () in the 3:son class; method, attempting to modify the return value to B,c, Object
1:b compiled by
2:C compilation failed with no inheritance relationship
3:object compilation failed, larger than the return value type of the parent class
-
Class A {
-
-
}
-
-
Class B extends A {
-
-
}
-
-
Class C {
-
-
}
-
Class Father {
-
String name;
-
-
void Eat () {
-
System.out.println ("Eat litters");
-
}
-
-
Define a function to get an object of Class A,
-
A Geta () {
-
return new A ();
-
}
-
-
}
-
-
Class Son extends Father {
-
-
public void Eat () {///inheritance enables subclasses to enhance the parent class's methods
-
System.out.println ("two cups");
-
System.out.println ("Two side dishes");
-
Super.eat ();
-
System.out.println ("Come One");
-
}
-
-
Class B is a subclass of Class A
-
B Geta () {
-
return new B ();
-
}
-
}
-
-
Class Demo8 {
-
-
public static void Main (string[] args) {
-
Son s = new Son ();
-
S.eat ();
-
-
}
-
}
7, sub-class object Lookup property or method order
1, principle, proximity principle
If the object of the subclass call method, the default is to use this to find, if the current object does not find a property or method, the current object to maintain the Super keyword point to the object, if you have not found the mutation error, find the direct call,
8, overloads and overrides are different
1, heavy Duty (overload)
1, assuming that all overloaded functions must be in the same class
2, features
Function name is the same, parameter list is different, not related to other (access control, return value type)
3, different
The number is different, the order is different, the type is different
2, overriding (override)
1, premise, inheritance
2, features
Function names must be the same, parameter lists must be the same
The return value type of the subclass is equal to or less than the return value of the parent class
What is rewriting in Java?