Hiding of member variables and overriding methods in Java

Source: Internet
Author: User

First, let's look at a very simple code:

Public class Clazztest {Public static voidMain (string[] args) {Son S1 =new Son ();System. out. println ("S1.name:" + S1.name);System. out. println ("S1.say:" + S1.say ());Father s2 =new Son ();System. out. println ("S2.name:" + s2.name);System. out. println ("S2.say:" + S2.say ()); }}class Father {Stringname ="Jack";Stringsay () {return"I am Father"; }}class Sonextends Father {Stringname ="Jack ' s son.";Stringsay () { return   "I am Son"   }}  

output:
s1.name:jack's son
 s1.say:i am son
 s2.name : Jack
 s2.say:i am son

Then why is this happening? It's about overloading (overload) and rewriting (override) in Java.

In the case of a subclass of Java with two names in the parent class and a method with the same argument list.

Because they have the same method signature, the new method in the subclass overrides the methods that are in the parent class.

It is because Java has a method rewrite in its inheritance, so it also embodies the dynamic polymorphism of Java.

So the question is, can the member variable be rewritten in Java?

in java in the rewriting of the introduction of the explicit, rewrite, refers to the method. The member variable is not mentioned.

Through the above example, we can also find that the member variables are not rewritten.

  So, in Java, member variables are not overridden. So here's another word: hide

java member variables are hidden:

within A class, a field that have the same name as a field in the superclass hides the superclass ' s fi Eld,

even if their types is different. Within the subclass, the field in the superclass cannot is referenced by its simple name.

instead, the field must be accessed through super.

generally speaking, we don ' t recommend hiding fields as it makes code difficult to read.

(powerful Youdao translator appearances, he said:)

In a class, a member variable in a subclass has the same name as a member variable in the parent class, even if they are of the same type.

Instead, you must obtain a member variable from the parent class that is hidden from the parent class, in general, we do not recommend hiding member variables because it makes the code difficult to read.

actually translated it into a short sentence, that is: subclasses do not override member variables that override the parent class, so access to member variables cannot be accessed as a method using polymorphism.

So again, how do we access the hidden member variables?

is to use a reference to the parent class to access the member variable:

Son s1 = new Son (); System. out. println ("S1.name:" + ((Father) s1). Name

Output:

S1.name:Jack

Hiding of member variables and overriding methods in Java

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.