Hiding and overwriting of Java subclasses and parent class methods

Source: Internet
Author: User

class base{

???? intx = 1;

???? staticinty = 2;

???? String name () {

???????? return"mother";

????}

???? static String Staticname () {

???????? return"static mother";

????}

}

class Subclass extends base{

???? intx = 4;

???? inty = 5;

???? String name () {

???????? return"Baby";

????}

???? static String Staticname () {

???????? return"static Baby";

????}

}

Public class test02{

???? Public staticvoid main (string[] args) {

???????? Subclass s = new subclass ();

???????? System. out. println (S.x+""+s.y+ ""+s.name () + ""+s. Staticname ());

???????? Base S1 = s;

???????? System. out. println (S1. X+""+s1. y + ""+s1.name () +""+s1. Staticname());

????}

}

?

/*

Results:

?

4 5 Baby static Baby

1 2 baby static mother

Conclusion:

?

instance method of the parent class overridden by an instance method of the same name as the quilt class

static method of parent class The quilt class with the same name static method hidden

instance variables and class variables of the parent class can be hidden by instance variables and class variables of the class

?

just " Base S1 = S " For example : the s Assigns a value to s1 s (subclass) method name name static name

assign a value to S1 after the S1 of the name the method has become "Baby", and Staticname method is only hidden and not overwritten, and now it has been restored "Static Mother"

*/

Hiding and overwriting of Java subclasses and parent class methods

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.