"Hands-on experiment One"
Construction method invocation under inheritance conditions
- Run the Testinherits.java sample, observe the output, notice that the call relationship between the parent class and the subclass constructs a method to modify the code of the parents constructor method, explicitly call the other constructor of grandparent, notice whether the calling code is the first sentence, the impact is significant!
Conclusion: By super calling the base class construction method, it must be the first statement in the subclass construction method.
Testinherits.java
Class Grandparent {
Public grandparent () {
System.out.println ("Grandparent Created.");
}
Public grandparent (String string) {
System.out.println ("Grandparent created.string:" + String);
}
}
Class Parent extends Grandparent {
Public Parent () {
Super ("hello.grandparent.");
System.out.println ("Parent Created");
Super ("hello.grandparent.");
}
}
Class Child extends Parent {
Public Child () {
System.out.println ("Child Created");
}
}
public class Testinherits {
public static void Main (String args[]) {
Child C = new Child ();
}
}
Experimental results:
"Hands on one's head"
Main points of the method override (override)
- Method overrides require that the subclass be the same as the method of the parent class, otherwise the method overload (overload)!
- Write your own code to test the following features (hands-on brain):
- In subclasses, to invoke methods overridden in a parent class, you can use the Super keyword.
1 , Experiment code: Testinherit.java
Write your own code to test the following features (hands-on brain): in subclasses, to invoke the overridden method in the parent class, you can use the Super keyword.
Zhaoxuan Li,november 10th,2016.
Class Father
{
public void multiplication (double a,double b)
{
System.out.println ("Function of the parent class:" +a+ "*" +b+ "=" +a*b);
}
}
Class Me extends Father
{
public void multiplication (double a,double b)
{
Super.multiplication (a+1, b+2);
System.out.println ("Function of the parent class:" +a+ "*" +b+ "=" +a*b);
}
}
public class Testinherit
{
public static void Main (string[] args)
{
Me a=new Me ();
A.multiplication (1,1.2);
}
}
Results:
Seventh talk about inheritance and interface hands-on experimentation and hands-on brain