aspose file tools
The moose likes Beginning Java and the fly likes Why there are two Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why there are two "a" attributes ?" Watch "Why there are two "a" attributes ?" New topic

Why there are two "a" attributes ?

Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84

Hi... Could you please explain how there are two "a" attributes in a one object ? I think, I haven't understand this correctly.. Please help me to understand ... ?
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1576
The trick is in line# 19.

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Matthew Brown

Joined: Apr 06, 2010
Posts: 4519

Overriding only applies to methods, not instance variables. So if you declare a variable in a subclass that's got the same name as a variable in the superclass it's a separate variable. It "shadows" the original variable, which means that anywhere in your class B where you refer to a it means the a that's declared in B. But the other variable still exists, and any code in the A class will refer to that one.

If you're following good practices then this rarely causes any problems. If you make all your instance variables private then they aren't visible at all in the subclass.
Have you tried LearnNowOnline?
subject: Why there are two "a" attributes ?