The current phenomenon is that subclasses inherit the parent class. Then a method of the parent class is overridden, and this method of the parent class is called in the constructor of the parent class, and the subclass override method is called when the subclass instantiates the parent class instantiation.
Code:
Parent class:
public class Father {
Public Father () {
System.out.println ("base class constructor {");
Show ();
New A ();
System.out.println ("}");
}
public void Show () {
System.out.println ("base class----show");
}
public Class A {
Public A () {
System.out.println ("Inner class constructor");
}
}
}
Sub-class:
public class Child extends father{
public static void Main (string[] arf) {
New Father (). Show ();
System.out.println ("--------------------I am the dividing line-------------------------");
New Child (). Show ();
System.out.println ("--------------------I am the dividing line-------------------------");
New Father (). Show ();
}
Public Child () {
System.out.println ("subclass constructor");
}
public void Show () {
SYSTEM.OUT.PRINTLN ("Extend base class-----show");
}
}
Operation Result:
--------------------I'm a split line-------------------------
Base class Constructor {
Base class----Show
Inner class constructor
}
Base class----Show
--------------------I'm a split line-------------------------
Base class Constructor {
Extend base class-----Show
Inner class constructor
}
Sub-class constructors
Extend base class-----Show
--------------------I'm a split line-------------------------
Base class Constructor {
Base class----Show
Inner class constructor
}
Base class----Show
--------------------I'm a split line-------------------------
You can see the order of the runs from the running results.
Specifically, the run result in the middle part of the constructor, called Show () is a subclass of the overridden method, that is, a subclass of methods.
Java Inheritance Issues