aspose file tools*
The moose likes Beginning Java and the fly likes Protected Fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected Fields" Watch "Protected Fields" New topic
Author

Protected Fields

Ronald Schindler
Ranch Hand

Joined: Jul 11, 2000
Posts: 50
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

Joined: Oct 09, 2001
Posts: 29
Ronald please send me coding of your program .
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
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();


if you don't know, then ask. if you do know, then share. love is knowledge.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
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).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
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

Joined: Jul 22, 2000
Posts: 9046
    
  10
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

Joined: Jul 11, 2000
Posts: 50
Thanks to all for the explanations. By jove, I think I've got it now!
Ronald Schindler
Ranch Hand

Joined: Jul 11, 2000
Posts: 50
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

Joined: May 08, 2001
Posts: 1932
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
 
subject: Protected Fields