• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hiding member !

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
I am confusing below case:


I recognized that the "name1" is hiding other than inherited to class B. However, I am not clearly understand what's the meaning of the method setName1(String _name1).
Base on my knowledge, method B.setName1() is inherited from class A (since I didn't override it) AS IF "class B redefine a method setName()" , i.e.

Therefore, "I THINK" that calling b.setName1 will set the B.name1 (the one with private) but not the A.name1 (the one with public)...
But this is not the case, does anyone explain it ?
Firstly, is the setName1() I call in fact set the super class A.name1 ?
Secondly, what should I get when I call the b.getName1() method ? The A.name1 or B.name1 and why ?
Thanks
Edited by Corey McGlone: Added CODE Tags
[ February 26, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The idea is simple, for data members you have static binding for member functions (except static ones) you have dynamic binding.
So all variables are looked up in the class where the method you called is executing

Should print out 5 when doing

Edited by Corey McGlone: Added CODE Tags
[ February 26, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by imre leber:
The idea is simple, for data members you have static binding for member functions (except static ones) you have dynamic binding.


Hi,
Can you be more clear(Please explain clearly) on the above sentence.
Thanks.
[ February 26, 2004: Message edited by: Narasimha Rao B. ]
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vince,
I think the thing in question here is the so called "encapsulation" - everything that you have inherited in a subclass forms a "capsule" inside the new class.
I have modified your original code to be like this:


This will output:
a.name1
b.name1
new name
b.name1

Now, as you can see - b.getName1() is a method in the superclass so it will use the name1 from the superclass (hence the first line is "a.name1").
The second method invoked is b.test() and it belongs to the subclass so it will use the name1 from the subclass and we get the output "b.name1".
After changing the name1 with

we're actually setting it in the base class. So next time we try to read it with b.getName1() we're reading the new value from the base class.
And finally b.test() is a method in the subclass and it's showing us that private String name1 in the subclass hasn't changed.
Now, first of all I wouldn't know why would anyone set a member variable to public (use public getters&setters for that and make your field private) and after that try to hide it in the subclass. Better set it to protected right away, no?
HTH,
Bojan
[ February 26, 2004: Message edited by: Bojan Knezovic ]
 
Vince Hon
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bojan, thanks, very clear explanation !
I encouter this because I am confused how the java bean design pattern can be applied to inheritance, since the attributes are always "private" in a bean.
And I would like to inherite those common attributes from the super class , so that I do not need to write the "duplicate" setter / getter methods.
Here is my case:
I have a web application (jsp, servlet) that allow user
to insert, modify, delete a record in database with table
PRODUCT.
The table PRODUCT will present different type of products, video, audio and
book
[PRODUCT]
----------
id
name
----------
I will write VideoProduct.java, AudioProduct.java, BookProduct.java
to handle the insert, modify, delete business logic......

In the above case, I have to write the "SAME" getter and setter methods in all *.java.
 
Bojan Knezovic
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vince Hon:

The table PRODUCT will present different type of products, video, audio and
book
[PRODUCT]
----------
id
name
----------
I will write VideoProduct.java, AudioProduct.java, BookProduct.java
to handle the insert, modify, delete business logic......
In the above case, I have to write the "SAME" getter and setter methods in all *.java.



Why wouldn't you implement the Product class that holds the ID and Name plus getters and setters? Then subclass it for Video, Audio and Book and add whatever new functionality you need.
[ February 26, 2004: Message edited by: Bojan Knezovic ]
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic