File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how can i access protected methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how can i access protected methods?" Watch "how can i access protected methods?" New topic
Author

how can i access protected methods?

satya kiran
Ranch Hand

Joined: Nov 07, 2000
Posts: 52
I want to access the protected methods mentioned in an abstract class how can i do that?
For example:
in java.util.Calendar there is a protected method getTimeInMills(). How can retrieve the value.
In my class a.java i want to call the calendar class as
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTimeInMills()) ----> this line is giving compilation error saying getTimeInMillis() is a protected method.
How can i access this??
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
hmm.. you are accessing the getTimeInMillis() method directly from the superclass, wich is not valid. Accessing a protected method is valid through inheritance only.
So try to create a class that inherits from Calendar (I don't know whether Calendar is final, I'll have to check that) and use your class to access getTimeInMillis. This is just the first thing that comes to my mind, but the point here is that protected methods must be accessed by inheritance only.
let me know how it goes.
[ July 23, 2003: Message edited by: Andres Gonzalez ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
getTimeInMillis() is a public method in 1.4.
if you are using 1.3 or earlier:
calendar.getTime().getTime()


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
satya kiran
Ranch Hand

Joined: Nov 07, 2000
Posts: 52
Thank you.
Here my restriction(in project) is to use jdk1.3.1. So i will use getTime of java.util.Date.
One more question .
If i want to represent current system date and time like
yyyymmddhhmmss ie., 20030722142434 How i can achieve conveniently.
Right now i am doing like this:
Stringbuffer sb = new StringBuffer();
sb.append(calendar.get(calendar.YEAR));
sb.append(calendar.get(calendar.MONTH));
......
.
...
...
and finally using sb.toString() to get the value. Is there any better way??
Thanks in advance for your help
kiran
Ross Goldberg
Ranch Hand

Joined: Jul 09, 2003
Posts: 63
Here is what I came up with to print (without saving to a String) the current date/time.
You need to import java.util.Date (to get the current date) and java.text.SimpleDateFormat (to define the display format).
System.out.println(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
Hope that helps.
Ross
Ross Goldberg
Ranch Hand

Joined: Jul 09, 2003
Posts: 63
I should add that what I wrote is from jdk 1.4, but from what I recall reading, it was defined in 1.3.1, I believe....not certain of that though.
Ross
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i access protected methods?