Java Inheritance Issues

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.