Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can i access protected methods?

 
satya kiran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getTimeInMillis() is a public method in 1.4.
if you are using 1.3 or earlier:
calendar.getTime().getTime()
 
satya kiran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic