File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected Fields

 
Ronald Schindler
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Sadaf Zaidi
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ronald please send me coding of your program .
 
Thomas Whalen
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • 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();
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • 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: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • 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: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic