• 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

Protected Fields

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I need an assist. I've searched the archives and my materials and I'm still not getting it. The problem is:
I'm trying to access the 'time' field of the Calendar Class. It's not allowing me to because its protected and I'm not sure why. Basically, I've instantiated a class such as:
Calendar myCalendar = new GregorianCalendar();
Now according to what I thought I understood, I should just be able to call the 'time' field to print such as:
System.out.print( myCalendar.time );
Since GregorianCalendar inherits from Calendar I thought this would do it. So...could someone please explain to me what I'm doing wrong and what it would take to access this field?
Thanks.
Ronald
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ronald please send me coding of your program .
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am just a beginner with OOD, OOP and Java. based on my very limited understanding of this, and because this topic of conversation caught my eye, i am wondering why you would want to instantiate from the Calendar class when the GregorianCalendar will provide you with the same benefits, and more...why not just do this?:
GregorianCalendar myCalendar = new GregorianCalendar();
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Whalen:
i am wondering why you would want to instantiate from the Calendar class ... why not just do this?:

GregorianCalendar myCalendar = new GregorianCalendar();


You are not actually instantiating the Calendar class. You are instantiating the GregorianCalendar class and assigning it to a Calendar reference.which makes it more flexible for use in polymorphism.

However, only methods are involved in polymorphism, not fields. So in this case it is hard to point out the usefulness of this feature using only the code presented here.

Not only that, but it wouldn't solve the problem at hand.
[This message has been edited by Marilyn deQueiroz (edited November 25, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case your class is in a different package than Calendar and GregorianCalendar. Therefore it must inherit from GregorianCalendar because otherwise it is unable to inherit GregorianCalendar's protected field.


 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a slightly different tack, here is an example from the JLS.
Notice that the subclass can access the inherited (its own) field but cannot access the superclass's field.

The rules are here, if that helps.
 
Ronald Schindler
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for the explanations. By jove, I think I've got it now!
 
Ronald Schindler
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thanks for posting the reply in javaranch to my question about accessing the protected field 'time' from the Calendar class.
I looked at the solution that was sent:
import java.util.* ;
class TestProtected extends GregorianCalendar
{
public static void main( String[] args )
{
TestProtected myCalendar = new TestProtected();
System.out.println( myCalendar.time );
}
}
I didn't notice it right away but this class instantiates itself. I'm not sure I've seen this before. What exactly is this called and would you normally want to do something like this? And/Or is this the only way to access that protected member?
Thanks.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what it is called, but you will see a class instantiating itself a lot when it comes to GUI applications. For example:

It doesn't do much but make a relatively large button, but you get the idea.
Also, in regards to the protected part, since you are subclassing the GregorianCalendar class, you now have access to all the protected data members. Otherwise, they aren't available to you (unless you are in the same package).
Jason
 
Would you like to try a free sample? Today we are featuring tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic