Results 1 to 5 of 5
  1. #1
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Accessing a method in a different class

    So, I am creating a class and within that class I want to call upon a method from a previously written picture class which has a method called show(), which will show the picture that you have selected.

    So, if I want to call on this method from another method how will it look?

    my method is:

    [code]
    public Picture display()
    {
    this.show();
    }
    [code/]

    is this right?

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    As stated in the other thread, this won't work because the compiler expects it to return a Picture, but it returns nothing. Either change Picture to void, or have it return something.

    Why can't you just call Picture.show() from another class? Is show() private?

  3. #3
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Any class extended from the class with the show() method in it will in turn inherit the show() method, unless the class is defined private.

    Java Code:
    class Picture {
    
        public Picture() {
        }
    
        public void show() {
            // show image
        }
    }
    
    class OtherClass extends Picture {
    
        public OtherClass() {
        }
    }
    OtherClass oc = new OtherClass();
    oc.show();

  4. #4
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    the method has to be called display() and within that method you have to call the show() method from a different class. That is public.

  5. #5
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    You are not really providing enough information. What is a 'Picture'? Are you extending it, as in the example I gave?

    Showing your code would help.

Similar Threads

  1. Accessing a method in another class
    By denno in forum New To Java
    Replies: 14
    Last Post: 04-07-2011, 06:59 AM
  2. Help with accessing static method
    By jazzermonty in forum New To Java
    Replies: 9
    Last Post: 03-07-2011, 10:04 PM
  3. accessing a singleton class inside another class
    By katturv in forum New To Java
    Replies: 1
    Last Post: 12-08-2010, 07:23 PM
  4. Need help with accessing files in a method
    By t0nydanzuh in forum New To Java
    Replies: 6
    Last Post: 10-03-2010, 07:05 PM
  5. Replies: 8
    Last Post: 03-21-2009, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •