[java] Java Question

Started by
6 comments, last by _nomad_ 20 years, 2 months ago
i am creating an api in java, and do not know how to do this: my api has a running loop that calls certain methods/function calls all the time. let''s say one of the methods i always call is method A(), method A() does nothing and is intended for users to define what it does. how do i let users define what A() does? if they inherit the class, then the running loop won''t see the user''s implementation of A(). thanks.
Advertisement
Your program will, let me explain

yourClass
method: doLoop() (this method calls a())
a() (empty)

someone says:

someone class extends yourClass
void a()
I type some stuff here what a() should do

Now I call doLoop() and this will use the overriden a() method. I hope I'm clear, caus I'm feeling kinda sleepy

Wesley

[edited by - wicked_wesley on February 7, 2004 4:31:22 PM]
Have a nice dayWesley
The a() method should be declared abstract like this :

abstract Type a();


After what, Each sub class that inherit from class where a() is declared (supposed class C) will have to define method a().

Exemple :

class B extends C{
Type a(){
.
.
.
}
}

manumoi
I see. I get it now. Thanks!!!!
hi again,

i have class A doing the loop that keeps on calling method B(). method B() is what the user should define for himself.

i have placed method B() in an abstract class of its own, but i get error messages when I call method B() inside the running loop of class A.

please help. thanks.
here''s an example of my problem:

public class A {  public void doLoop() {    B.userDefinedMethod();  }}public abstract class B {  abstract boolean userDefinedMethod();} 


The users will have to override class B with whatever he/she wants to happen inside B.userDefinedMethod().

How to fix this? thanks.
Yes, that doesn''t work. You might want to do something like this:
public class A  {  C.method();  }public class C extends B  {  override the method  } 


Ofcourse, the class C must exist already. Isn''t it possible to include the class B into the class A? That would be the easiest solution I think.

Wesley
Have a nice dayWesley
quote:
public class A {  public void doLoop() {    B.userDefinedMethod();  }}public abstract class B {  abstract boolean userDefinedMethod();}       

This doesn't work because class A is making a static call to the userDefinedMethod in class B. It's calling a method directly on the class rather than an on instance (an object) of the class.

You could do something like this:
public class A {  B userObj;    // Provides class A with an instance of class B  public void setUserObject(B b) {     userObj = b;  }  public void doLoop() {     // Now you're calling the method on an object, rather than a class     userObj.userDefinedMethod();   }  public static void main(String[] args) {     // Start the program     A a = new A(); // Create an object of class A, can't call methods directly on A unless those methods are declared static     B b = new C(); // Instantiate user-defined class, casting it class B just to show that it can be done     b.initialize(a); // initialize the user-defined class instance     a.doLoop(); // start loop  }}public abstract class B {  public void initialize(A a) {    a.setUserObject(this);  }  abstract boolean userDefinedMethod();}     

public class C extends B {
boolean userDefinedMethod() {
// do something
}
}

[edited by - HenryAPe on February 8, 2004 10:51:32 AM]

This topic is closed to new replies.

Advertisement