• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Abstract Parent - Dynamic child classes

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following general program. I am trying to
get the child classes to be dynamicallyd determined on
a switch statement.
The BuildScores method expects a BaseCharacterClass which is the parent of class1, class2, etc.. I thought that since parent is abstract, the class type would trickle down the hierarchy until it gets to the correct class.
For example, if case 0: , then it would make a class1 object. Then BuildScores method is called - sending a class1 object. Now in this method - the compiler would say Ok, I have a BaseCharacter abstract parent, now let me find the class1 and execute its methods.
Without this set-up I would have to call a different method for each class - which considering the amount of calculations that I am doing would be cumbersome. [Note the example below is only a small chunk of the actual code]
How does one solve this?
** code begins **
abstract BaseClass
class 1 extends BaseClass
class 2 extends BaseClass
etc...
//AnotherClass.java
public AnotherClass class
{
public static int[] GetScores(int i)
{
switch(i)
{
case 0:
class1 aClass = new class1();
return AbilityScores= BuildScores(aClass);
case 1:
class2 bClass =
new class2();
return abilityScores = BuildScores(bClass);

}
}
private static int[] BuildScores(BaseCharacterClass childClass)
{
int[] scores = {0,0}; //hold some ints
//currently it throwing run-time null errors
scores[0] = childClass.GetAInt();
scores[1] = childClass.GetAnotherInt();
}
}

 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you have shown seems OK. Are you sure the problem is not in your implementation of "GetAInt" in class1 or class2 ?
 
Bruce Quesenberry
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were right - the concept is correct. I messed up
the class array with the local array names.
reply
    Bookmark Topic Watch Topic
  • New Topic