The order in which the various methods of the subclass and parent classes are executed

Source: Internet
Author: User

First post code: Parent class Animal class, Subclass Cat Class

Package com.wzy.jpbm.test;

public class Animal {
	static{
		System.out.println ("Static method of Parent class");
	}
	{
		System.out.println ("construct code block for executing parent class");
	}
	Public Animal () {
		System.out.println ("construct method to execute parent class");
	public void A () {
		System.out.println ("A method of executing parent class");
	}


Package com.wzy.jpbm.test;

public class Cat extends animal{
	static{
		System.out.println ("static method for Subclasses");
	}
	{
		System.out.println ("Construction code block for executing subclasses");
	}
	Public Cat () {
		System.out.println ("construct method for executing subclasses");
	}
	@Override public
	Void A () {
		System.out.println ("a method of executing subclasses");
	}
	public static void Main (string[] args) {
		cat c = new Cat ();
		C.A ();
	}


Execution results:

static method of the static method subclass of the parent class executes the construction code block of the parent class executes the constructor of the parent class executes the constructor of the subclass execution subclass of
a method


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.