1

I want to create an object of child class

more than 100 class extend MasterClass

MasterClass is

 public class MasterClass{
  int key;
  String value;
  String displayValue;
  boolean status;
 } 
 public class User extends MasterClass{
 public User(){ }
 }
 public class Customer extends MasterClass{
 String productName;
 public Customer (){ }
 }

etc...

i will get a MasterClass object from client, i wanted to type cast that object to respective one

if(masterClass instanceof User) {  
  User a_user = (User) a_ masterClass;
  …
} else if(masterClass instanceof Customer) {
  Customer a_customer = (Customer) a_ masterClass;
  …
}

if i do this i will end up with 100s of else if.

Please let me know how i can achieve this without else if?

Thanks in advance.

Sivakumar M
  • 1,567
  • 4
  • 18
  • 22
  • Just advice : read http://stackoverflow.com/help to put readable questions – Ahmed Nabil Dec 02 '13 at 08:08
  • Please really think about your design if you have a type hierarchy where `User` and `Customer` extend a `MasterClass`. What is it that `MasterClass` gives you which you could not solve more elegant by composition? – Matthias Dec 02 '13 at 08:09
  • What do you intend to do with each casted reference (`a_user`, etc)? Without knowing much more, my hunch is that polymorphism can do it by just having a method on `MasterClass`. – yshavit Dec 02 '13 at 08:12
  • @Matthias As your replay i have change the code please check it. do the help need full – Sivakumar M Dec 02 '13 at 10:06

3 Answers3

3

Use polymorphism and generics, as Java intended.

Polymorphism lets you call a method on your object that behaves differently for every type. The easiest way to achieve this is to provide an abstract method in the base class MasterClass and then override it with different functionality in every extended class. You are probably looking for something like this:

class MasterClass {
    int age;
    // ...
    public abstract void doWork();
    public int getAge() { return age; }
    // .. more methods
}

class User extends MasterClass {
    // customize User here
    @Override
    public void doWork() {  /* User does work in some way */ }
}

class Customer extends MasterClass {
    // customize Customer here
    @Override
    public void doWork() {  /* Customer does work in some other way */ }
}

// ...

If you are not too familiar with OOP, here is a good introductory tutorial.

If you are not allowed to alter your classes, you can populate a look-up table like HashMap<Class, MyFunctor> where you can assign a different functor for every type of person you have.

Also, you might want to use generics. Generics allow you to capture and restrict the type of objects passed to your methods.

Community
  • 1
  • 1
Domi
  • 22,151
  • 15
  • 92
  • 122
1

Maybe you can use generics with the constraint T extends MasterClass?

user1186155
  • 238
  • 2
  • 10
0

Using basic concepts of Design Pattern you can create a constructor like this in the object where you try to initialize

MasterClass masterClass;

public MyCreatorOject(MasterClass masterClass)
{
    this.masterClass = masterClass;
}

later when you create the object it can be

new MyCreatorObject(new User());

or

new MyCreatorObject(new Customer());
AurA
  • 12,135
  • 7
  • 46
  • 63