Inheritance in Java uses the extends keyword, and after the subclass inherits the parent class, it gets all the properties and methods of the parent class (except the constructor of the parent class). If the definition of her parent class does not appear when the Java class is defined, this class will extend the Java.lang.Object class by default. So Java.lang.Object is the parent class for all classes, either the immediate parent class or the indirect parent class.
override of method (overridden Methods)
in class inheritance, subclasses can modify the behavior inherited from the parent class, which means that the subclass can create a method that has different functionality from the parent class method, but with the same name, return type, and argument list. If you define a method in a new class whose name, return type, and parameter table match exactly the name, return type, and parameters of the method in the parent class, the new method is called overwrite the old method.
Example: These methods are listed below in the Employee and Manager classes:
public class Employee {
String name;
int salary;
Public String getdetails () {
Return "Name:" + name + "\ n" + "Salary:" + Salary;
}
}
public class Manager extends Employee {
String Department;
Public String getdetails () {
Return "Name:" + name + "\ n" + "Manager of" +
Department
}
}
The Manager class has a defined Getdetails () method because it inherits from the Employee class. The basic method is to replace or overwrite the quilt class version.
which method to run?
This will bring us a problem, the parent-child class has the same method, then at run time which method is called exactly? Consider the following scenario:
Employee E = new Employee ();
Manager m = new manager ();
If E.getdetails () and M.getdetails () are requested, different behaviors are invoked. The Employee object will perform the Getdetails version associated with the employee, and the manager object will perform the Getdetails () version associated with the manager.
It is not obvious that the following is the example:
Employee e = new Manager ();
E.getdetails ();
or some similar effect, such as a generic method parameter or an item from a heterogeneous collection. In fact, you get the behavior related to the variable's run-time type (that is, the type of the object that the variable refers to), not the compile-time type of the variable. This is an important feature of object-oriented language. It is also a feature of polymorphism and is often referred to as virtual method invocation.
In the preceding scenario, the E.getdetails () method that is executed comes from the real type manager of the object. So the rule is that you look at the data type at compile time, and the runtime looks at the actual object type (which class is the constructor followed by the new operator). One word:New who will call the method of WHO.
Rules for overriding methods
The name of the method for the subclass and the order of the subclass method parameters must be the same as the names of the methods in the parent class and the order of the parameters so that the method overrides the parent class version. The following rules apply to the Overwrite method:
- The return type, method name, and parameter list of the overridden method must be the same as the method that it overrides.
- The override method cannot be less accessible than the method it overrides (that is, access permissions are not allowed to shrink).
- The override method cannot throw more exceptions than the method it overrides.
These rules originate from polymorphic attributes and the Java programming language must guarantee "type-safe" needs. Consider this invalid scenario:
public class Parent {
public void Method () {}
}
public class Child extends Parent {
private void Method () {//Compile error
}
}
public class Test {
public void Othermethod () {
Parent P1 = new parent ();
Parent P2 = new Child ();
P1.method ();
P2.method ();
}
}
The Java programming language semantics stipulate that P2.method () causes the child version of the method to be executed, but because the method is declared as PRIVATE,P2 (declared as Parent), it cannot be accessed. Thus, language semantics conflict.
method Overloads :
If the method name of two methods is the same, but the parameters are inconsistent, then one method can be said to be overloaded with another method.
* Method name is the same
* The parameter type of the method, the order of the number is at least one different
* The return type of the method can be different
* Method modifiers can be different
* The Main method can also be overloaded
For example:
public class Test
{
public static void Main (string[] args)
{
Human person = new Human ();
Person.breath (10);
}
}
Class Human
{
/**
* Breath () 1
*/
void Breath ()
{
System.out.println ("hu...hu ...");
}
/**
* Breath () 2
*/
void Breath (int rep)
{
int i;
for (i = 0; i < rep; i++) {
System.out.println ("Lu...lu ...");
}
}
int height;
}
Inheritance of Java methods, overriding and overloading