Click here to Skip to main content
15,032,063 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Java
public class A
{
  public static void main(String []args)
  {
    X(1);
  }
  public ????? X(int i)
  {
    if(i==0)
      return new B();
    else
      return new C();
  }
}
public class B
{
}
public class C
{
}


what should be the return type of method X in place of ?????
Posted
Updated 19-May-15 1:29am
v2
Comments
Tomas Takac 19-May-15 7:42am
   
Object. But this is bad design! B and C should have something common and this commonality should be expressed by an interface or a base class.
CPallini 19-May-15 8:00am
   
My virtual 5.
   
Will you post it as the formal answer, perhaps with a bit of detail?
—SA
Tomas Takac 19-May-15 8:35am
   
Done. Thanks.

1 solution

To make the code compile you can declare X's return type as Object. This will work as everything is an object. However it is bad design!

You factory (method X) is creating objects of both types because they have something in common. If they are not related at all then why they need to be created in the same method?

In OOP you express this commonality by a base class or an interface and let the factory return then common type.
Java
public abstract class Account {}

public class SavingsAccount extends Account {}

public class TransactionAccount extends Account {}

public Account Create(int type)
{
    if(type == 0) 
        return new SavingsAccount();
    else
        return new TransactionAccount(); 
}
   
v2
Comments
   
I voted 4. Will you please fix the typo in first line, or better yet, check it all on a compiler?
Besides, it's good to show at least one member of Account, to demonstrate how the caller of the method Create could use it (but none of the members added by derived classes).

Also note that the method Create should be static. Such methods can be called "factory methods".

—SA
Tomas Takac 19-May-15 9:35am
   
Thank you, I fixed the typo. I'm a bit hesitant to add more details though because I don't think it will help to make it more clear.

I would disagree that the method should be static. What if I want to have it injected? And I avoided naming it "factory method" on purpose. When you say "factory method" the design pattern comes to my mind first. While this is a factory and it is a method it's not implementation of that pattern.
Sergey Alexandrovich Kryukov 19-May-15 10:19am
   
About "static", the rule is quite simple: if you don't use "this" anywhere in its code, it should be static. Making a method non-static (instance) without a purpose is a violation of the good style and is a performance leak detected as such by some analysis tools.

"Factory method" is only my own hint to the inquirer; this is not something you would need to mention. Yes, essentially it is a factory method.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900