• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

abstract class and protected member behaviour

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the code





The inherited member x is int. However, I am redeclaring it as String while subclassing. Is this allowed?

Also, if I make line 1 as public, I am able to change line 2 as private. Compiler doesn't complain that the member has become less accessible. Is that ok as well?
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
variables doesn't take part in the process of inheritance ...
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rathi Ji,
If I comment line 2, the answer prints 9. Shouldn't that mean that it gets inherited?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables are not inherited, they are hidden.
It means the there are two varaibles in sub class, one is variable x present in Class A and other is x in Class B.
So here as u r accessing x, the JVM will first search if that variable is present in ur class, if the varaible is defined in ur class, if not it searches its super class and finally comes up with the result.
I hope thats ok with u

Thanks
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Members are not overriding. If you declared the member in subclass i.e,. available in superclass that memeber will be shadowing. This is also called as instance memeber hiding.
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i had two questions

1)Though it is shadowing can i say it is possible to access the superclass variable using the Fully qualified Name.
(i.e)SuperClassName.variablename

is there anyother way to access it.

2) The protected variable of the super class which resides in different package will become a private member in the subclass.
is it okay..
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paramesh,


1)Though it is shadowing can i say it is possible to access the superclass variable using the Fully qualified Name.
(i.e)SuperClassName.variablename



If it is static memebers it is possible. just walk through the below code. If any doubt post again.

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"M"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,
I guess you are refering to my name here. The naming policy in javaranch does say that I could use initial for first name.
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answer guys. I need to read more on this topic to get a good idea.
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic