*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract class and protected member behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract class and protected member behaviour" Watch "abstract class and protected member behaviour" New topic
Author

abstract class and protected member behaviour

M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
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

Joined: Oct 11, 2004
Posts: 3830
variables doesn't take part in the process of inheritance ...
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Rathi Ji,
If I comment line 2, the answer prints 9. Shouldn't that mean that it gets inherited?
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Mar 18, 2005
Posts: 143
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.


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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

Joined: Mar 18, 2005
Posts: 143
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

"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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
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

Joined: Mar 04, 2005
Posts: 91
Thanks for the answer guys. I need to read more on this topic to get a good idea.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract class and protected member behaviour
 
Similar Threads
illegal start of expression -- arrays
please explain the output
Confusing code.
accessing the super class variable
static method invocation problem