-
Newbie question: Calling method in class
I have a class named X, and a class named B. These two don't extend each other
or any such thing.
In Class X we find: B b=new B();
In Class B we want to call a method in Class X, how can this be done? I dont
want to explicitly call
X, since Class X might as well be Class Y, Class Z etc. I hope this is clear
enough.
Thanks for help.
-
Re: Newbie question: Calling method in class
"Gary J." <test@hotmail.com> wrote:
>
>I have a class named X, and a class named B. These two don't extend each
other
>or any such thing.
>In Class X we find: B b=new B();
>In Class B we want to call a method in Class X, how can this be done? I
dont
>want to explicitly call
>X, since Class X might as well be Class Y, Class Z etc. I hope this is clear
>enough.
>Thanks for help.
i didnt fully understand ur question but whatever i get
according to me u wants to invoke any of the class method(x,y,z) from class
b.now to invoke any class method u need to have that particular class reference
so u need to pass class reference of class(x,y,z) to class b.implement one
interface common to all classes(x,y,z,...)(say com).
class B
{
com c;
B(com c)
{
this.c=c;
c.f()//f is function in class(x,y,z...)
}
& instantiate B in X by B b = new B(this);//instead of
//B b=new B();
if this answer provides u ur solution-->fine else let me know
at my email-id
-
Re: Newbie question: Calling method in class
If in class B you want to call a method in class X, you first need to create
an object of that class:
myX = new X();
Then if the method is called doSomething, you need to have that object call
the method by:
myX.doSomething();
I hope that helps, your remark that "Class X might as well be Class Y, Class
Z etc" was completely unclear to me.
Gary J. <test@hotmail.com> wrote in message news:39b67df5$1@news.devx.com...
>
> I have a class named X, and a class named B. These two don't extend each
other
> or any such thing.
> In Class X we find: B b=new B();
> In Class B we want to call a method in Class X, how can this be done? I
dont
> want to explicitly call
> X, since Class X might as well be Class Y, Class Z etc. I hope this is
clear
> enough.
> Thanks for help.
-
Re: Newbie question: Calling method in class
If I understand your problem correctly, You want to call a function foo which
could be in Class X,Y,Z ... from the class B. If this is the case, you have
to do something like this (Polymorphism using interface):
class B{
B(Common c)
{
c.foo();
}
}
interface Common{
abstract void foo();
}
class X implements Common{
void foo()
{
//rest of the code here.
B b=new B(this);
}
}
class Y and Z can also be defined in a similar manner. So you dont have to
explicitly name the class in Class B. Just use the name of the interface
and call the function. Remember to use the implements statement in all the
classes that have the foo function. This is called Polymorphism using interface
in the OOP terminology. You dont have to use the extends statement, that
is required for inheritance.
Hope this helps.
Thomas.
"Gary J." <test@hotmail.com> wrote:
>
>I have a class named X, and a class named B. These two don't extend each
other
>or any such thing.
>In Class X we find: B b=new B();
>In Class B we want to call a method in Class X, how can this be done? I
dont
>want to explicitly call
>X, since Class X might as well be Class Y, Class Z etc. I hope this is clear
>enough.
>Thanks for help.
-
Re: Newbie question: Calling method in class
I'm not entirely shure I understood your problem, but it seems to me that
what you want to happen is that when an instance of class B is created you
want it to be able to call a method in the instance of X that created it.
Right? In that case your class B constructor need to be passed a reference
to class X like this:
B b=new B(this);
where class Bs constructor looks something like this:
public B(X x) {...}
and can reference x's method like this:
x.methodToCall(...);
"Gary J." <test@hotmail.com> wrote:
>
>I have a class named X, and a class named B. These two don't extend each
other
>or any such thing.
>In Class X we find: B b=new B();
>In Class B we want to call a method in Class X, how can this be done? I
dont
>want to explicitly call
>X, since Class X might as well be Class Y, Class Z etc. I hope this is clear
>enough.
>Thanks for help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks