Conversion troubles

Discussion in 'Java' started by gajo, Dec 19, 2003.

  1. gajo

    gajo Guest

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

    So what am I doing wrong?
    Gajo
     
    gajo, Dec 19, 2003
    #1
    1. Advertising

  2. gajo

    Noel Guest

    > ----------------------------------------
    > 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.
     
    Noel, Dec 19, 2003
    #2
    1. Advertising

  3. gajo

    gajo Guest

    Yeah that helped! Thanks! :)
     
    gajo, Dec 20, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ober
    Replies:
    6
    Views:
    533
    Frank Mamone
    Sep 13, 2004
  2. Victor
    Replies:
    10
    Views:
    3,444
    Victor van Hillo
    Nov 21, 2004
  3. Owen

    cookie troubles

    Owen, Jan 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    314
    Exception
    Jan 7, 2005
  4. cerr
    Replies:
    4
    Views:
    340
    Jerry Coffin
    Jul 10, 2009
  5. , India
    Replies:
    2
    Views:
    478
    Fraser Ross
    Sep 15, 2009
Loading...

Share This Page