• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

abstract class and protected member behaviour  RSS feed

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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: 3830
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks for the answer guys. I need to read more on this topic to get a good idea.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!