Conversion troubles

G

gajo

I have two packages, containing a class each. Notice that they both have
methods of the same name. These two classes are not related to each other in
any other way.

In my Main class, I have a field named X, of the class Object. This way I
can make X to be either Something or SomethingElse. Now, when I want to do a
few things with this X, I have to specify which class the user is presuming
X is of. If the user thinks it's the first class, then he will use the
DoFirst() method; if he thinks it's the second, he will use the DoSecond()
method.

Anyway, as you can see these two methods try to access the methods from
Something or SomethingElse, according to the method that the user called.
However, I get an error message. Read the bottom of this post!

// ------------------ ./First/Something.java
package First;

public class Something {
...

public void DoSomething() {
}
}

// ------------------ ./Second/SomethingElse.java
package Second;

public class SomethingElse {
...

public void DoSomething() {
}
}

// ------------------ Main.java
import First.*;
import Second.*;

class Main {
private Object X;

public void DoFirst() {
(Something)X.DoSomething();
}

public void DoSecond() {
(SomethingElse)X.DoSomething();
}
}


----------------------------------------
When I write
(Something)X.DoSomething();
Then I get the following error message:

not a statement
(Something)X.DoSomething();
^
----------------------------------------

When I write
X.DoSomething();
I obviously get the error that the Object class does not have this method:

cannot resolve symbol
symbol : method DoSomething ()
location: class java.lang.Object
X.DoSomething();
^
 
N

Noel

----------------------------------------
When I write
(Something)X.DoSomething();
Then I get the following error message:

not a statement
(Something)X.DoSomething();
^

You are trying to cast the return type of X.DoSomething() to Something,
which is void, hence the complier complains.
Try

Something s = (Something)X;
s.DoSomething();
----------------------------------------

When I write
X.DoSomething();
I obviously get the error that the Object class does not have this method:

cannot resolve symbol
symbol : method DoSomething ()
location: class java.lang.Object
X.DoSomething();
^
----------------------------------------

You have declared X as an Object, which does not have a DoSomething()
method, hence the complaining complier again. By adding my amended line
above, this should get rid of that problem.
So what am I doing wrong?
Gajo

Noel

PS Java convention says that methodnames should start with a lower case
letter, as should instance variables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top