• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] Java Question

This topic is 5189 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement