Newbie: instantiating of abstract methods - Calendar.

Z

Zalek Bloom

Hello,

Sun Java tutorial says:

If you attempt to instantiate an abstract class, the compiler displays
an error similar to the following and refuses to compile your program:
AbstractTest.java:6: class AbstractTest is an abstract class.
It can't be instantiated.
new AbstractTest();
^
1 error


But in one of the examples it is written:

Calendar cal = Calendar.getInstance();

Now the Calendar is an abstract class defined as:

public abstract class Calendar extends Object implements Serializable,
Cloneable

Now, isn't the command "cal = Calendar.getInstance()"
instantialization?
The result is the same as with "new" - creating an object and putting
a reference to "cal".

Zalek
 
J

Joona I Palaste

Zalek Bloom <[email protected]> scribbled the following
Sun Java tutorial says:
If you attempt to instantiate an abstract class, the compiler displays
an error similar to the following and refuses to compile your program:
AbstractTest.java:6: class AbstractTest is an abstract class.
It can't be instantiated.
new AbstractTest();
^
1 error

But in one of the examples it is written:
Calendar cal = Calendar.getInstance();
Now the Calendar is an abstract class defined as:
public abstract class Calendar extends Object implements Serializable,
Cloneable
Now, isn't the command "cal = Calendar.getInstance()"
instantialization?

Yes. But the actual class of the object that gets instantiated is not
Calendar - it's some concrete class that extends Calendar.
The result is the same as with "new" - creating an object and putting
a reference to "cal".

Yes. But what is happening in that getInstance() method is something
like:

return new com.foocorp.calendar.OurConcreteCalendar();

where the class is something like this:

package com.foocorp.calendar:
public class OurConcreteCalendar extends Calendar
{
/* ... */
}

This question, i.e. why is it possible for variables that refer to
abstract types to hold object references at all, comes up so
frequently I have to wonder why it's not a FAQ. Read up on basic
concepts in polymorphism.
 
V

VisionSet

Zalek Bloom said:
Hello,

Sun Java tutorial says:

If you attempt to instantiate an abstract class, the compiler displays
an error similar to the following and refuses to compile your program:
AbstractTest.java:6: class AbstractTest is an abstract class.
It can't be instantiated.
new AbstractTest();
^
1 error


But in one of the examples it is written:

Calendar cal = Calendar.getInstance();

Now the Calendar is an abstract class defined as:

public abstract class Calendar extends Object implements Serializable,
Cloneable

Now, isn't the command "cal = Calendar.getInstance()"
instantialization?

No, but that isn't really the confusion is it?
It will retrieve a Calendar, but then GregorianCalendar is a Calendar, so it
could be that, or any concrete subclass of Calendar. You can be sure it the
object is not of Class Calendar, as you observe it is abstract and cannot be
instantiated, but it can be of type Calendar
The result is the same as with "new" - creating an object and putting
a reference to "cal".

No, it may instantiate, or it may retrieve a previously instantiated
Calendar.
 
C

Christophe Vanfleteren

Zalek said:
Hello,

Sun Java tutorial says:

If you attempt to instantiate an abstract class, the compiler displays
an error similar to the following and refuses to compile your program:
AbstractTest.java:6: class AbstractTest is an abstract class.
It can't be instantiated.
new AbstractTest();
^
1 error


But in one of the examples it is written:

Calendar cal = Calendar.getInstance();

Now the Calendar is an abstract class defined as:

public abstract class Calendar extends Object implements Serializable,
Cloneable

Now, isn't the command "cal = Calendar.getInstance()"
instantialization?

Yes, but you are not creating an instance of the Calendar class. You are
using the static factory method (method that creates objects for you)
getInstance(), which returns an instance of a subclass of Calendar, namely
GregorianCalender. It is something that you don't see often, because in this
case, the superclass (Calendar) knows about sublasses (GregorianCalendar),
which you would try to avoid most of the times.
 
T

Tor Iver Wilhelmsen

Now, isn't the command "cal = Calendar.getInstance()"
instantialization?

No, but somewhere in the method an object is created. This object's
class is assignable to the type Calendar, ie. it's a subclass.

Print out cal.getClass().getName() to see.
The result is the same as with "new" - creating an object and putting
a reference to "cal".

If you check the source you will find it's not the same at all.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top