Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object creation- new v/s getInstance()

 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can't instantiate abstract classes because of undefined abstract method. Then why it is allowed to create object via factory method getInstance()? What is the difference between both?
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In Factory design pattern, getInstance method is supposed to create and object (or return the desired/random object from pool, depending on context).

Abstract class cannot be instantiated and hence, putting getInstance method in Abstract class is pointless(except you want to make all descendants of that class to follow getInstance() contract). getInstance of that class will return instance of a subclass (depending on conditions).

Can you be more specific (even better, provide some code where you are facing doubts)?
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example Calendar is abstract and Calendar.getInstance() gives you some subclass of Calendar. So Calendar can decide which subclass to return.

John
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anayonkar and John
John Stark wrote:For example Calendar is abstract and Calendar.getInstance() gives you some subclass of Calendar. So Calendar can decide which subclass to return.

Is this a random decision made by Calendar?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on the locale the application is running in (or the calendar is asked for). The idea is that it delivers an appropriate calendar for the context.

For example, looking at the source code for Java 1.6, Calendar.getInstance() can return one of three specific types. BuddhistCalendar if you're in Thailand, JapaneseImperialCalendar if you're in Japan, or GregorianCalendar otherwise. I haven't looked at the Java 7 code to see if they've added any more.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:It depends on the locale the application is running in (or the calendar is asked for). The idea is that it delivers an appropriate calendar for the context.

For example, looking at the source code for Java 1.6, Calendar.getInstance() can return one of three specific types. BuddhistCalendar if you're in Thailand, JapaneseImperialCalendar if you're in Japan, or GregorianCalendar otherwise. I haven't looked at the Java 7 code to see if they've added any more.

Is it like this-

and


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic