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();
^
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();
^