Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sub class access

 
Ashok Paulraj
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I access the subclasses' (B,C and D - subclasses of A) methods from an external class 'Temp' ?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you be more specific about these methods? What level of access do they have?
 
Ashok Paulraj
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

public class B extends A {

public void setObj (List l) {}
public void instantiateTemp ()
{
Temp t = new Temp ();
t.setParent (this);
}
}

public class C extends A {

public void setObj (List l) {}
public void instantiateTemp ()
{
Temp t = new Temp ();
t.setParent (this);
}
}

public class D extends A {

public void setObj (List l) {}
public void instantiateTemp ()
{
Temp t = new Temp ();
t.setParent (this);
}
}

public class Temp
{
private A _aObj;
Temp () {..}
void setParent (A aObj) {
_aObj = aObj;
}

void compute () {

// from here I need to access setObj(List l) on B and C....
}
}

Temp is a GUI class and hence compute () won't be called until a UI action takes place......

Appreciate your help !!
 
Srikanth Ramu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you want to access setObj of B and C



Or you could instantiate separate objects for respective classes.

Hope this helps.
[ April 04, 2007: Message edited by: srikanth ramu ]
 
Ashok Paulraj
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the above code sample defines _aObj which is already defined in setParent(...)

Also, I could do an instanceof to determine the subclass of A but is there a neat way to do this avoiding instanceof.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic