File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class and protected member behaviour

 
M Rama
Ranch Hand
Posts: 91
  • 0
  • 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?
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • 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
  • 0
  • 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?
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • 0
  • 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
 
Raghu Shree
Ranch Hand
Posts: 143
  • 0
  • 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.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • 0
  • 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
  • 0
  • 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.

 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic