Creating instances of abstract class DateFormat?

K

KAR120C

I am trying to learn the requirements for the new Java Certification exam and
came across something that doesn't make sense to me. According to the JDK 1.
5 docs, java.text.DateFormat is an abstract class, yet it has a constructor,
DateFormat(). It also has a method, getDateInstance(int style), that returns
a static reference that can be used to call a non-static method as in theses
four lines from the same documentation:

To format a date for a different Locale, specify it in the call to
getDateInstance().

DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);

You can use a DateFormat to parse also.
myDate = df.parse(myString);

How can an abstract class have a constructor and how can a static reference
be used to call a non-static method?

Thanks
 
I

Ian Mills

KAR120C said:
I am trying to learn the requirements for the new Java Certification exam and
came across something that doesn't make sense to me. According to the JDK 1.
5 docs, java.text.DateFormat is an abstract class, yet it has a constructor,
DateFormat(). It also has a method, getDateInstance(int style), that returns
a static reference that can be used to call a non-static method as in theses
four lines from the same documentation:

To format a date for a different Locale, specify it in the call to
getDateInstance().

DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);

You can use a DateFormat to parse also.
myDate = df.parse(myString);

How can an abstract class have a constructor and how can a static reference
be used to call a non-static method?

Thanks

It is the method getDateInstance that is static and not the reference it
returns. Also why shouldn't it have a constructor. This would just be
the default constructor called by any class extending DateFormat (if you
look at Format, the supercalss of DateFormat, you will see that this
also has a constructor).
 
K

KAR120C

I see what your saying about the the abstract class, DateFormat, having a
constructor as does its superclass, Format, but I'm still fuzzy about how
this abstract class can be instantiated. Any more thoughts?

Thanks
 
S

Stefan Schulz

KAR120C said:
I see what your saying about the the abstract class, DateFormat, having a
constructor as does its superclass, Format, but I'm still fuzzy about how
this abstract class can be instantiated. Any more thoughts?

Well, since you want to prepare for certification, i am a bit surprised
this point came up, but...

abstract class A {
public abstract void foo();

public static A newInstance(){
return new B();
}
}

class B extends A {
public void foo(){
System.out.println("Foo called");
}
}

See the pattern?
 
K

KAR120C

In other words, the getDateInstance method is returning an unspecified
subtype of DateFormat. That makes sense. Thanks for your help.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ian Mills uitte de volgende tekst op 01/22/2006 08:14 PM:
It is the method getDateInstance that is static and not the reference it
returns. Also why shouldn't it have a constructor. This would just be
the default constructor called by any class extending DateFormat (if you
look at Format, the supercalss of DateFormat, you will see that this
also has a constructor).

I for my point would make that constructor protected. Absolutely no use
for a public one and just confusing, as illustrated by this thread.

H.

- --
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFD1ATzimZ/5+ZTkokRAumgAJ4janUQi5L73mRuWDgMY4LbEbX5ZwCeJewZ
mkBRFZnXaGVTMIQKWF7k01g=
=EVuG
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top