Results 1 to 8 of 8
  1. #1
    moaxjlou is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default Calling methods from superclass

    Hi All
    I am stuck with calling methods from a superclass.
    Basically, in the subclass, i need to get an array of values, which is generated from the methods of the superclass.

    Java Code:
    public class A {
    ...
    
    public ArrayList<String> generateNames(){
     //this method returns an array of names
    }
    
    
    }
    the output of this method is something like below:
    Java Code:
    [Adam, Smith, Jones, Norah]
    Java Code:
    public class B extends A{
    ...
    //now I want to use the values from generateNames() of class A
    public void generateUI(){
      ArrayList<String> names = super.generateNames();
      System.out.println(names);
    }
    
    }
    However, when instatiate a B object, and calls generateUI() as below:

    Java Code:
    B b= new B();
    b.generateUI();
    The output is
    Java Code:
    []
    Do you know how do i get the output
    Java Code:
    [Adam, Smith, Jones, Norah]
    by calling b.generateUI();?

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    you should be able to just do this.super().generateUI(); or super(this).generateUI();

    something along those lines

  3. #3
    moaxjlou is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default

    you should be able to just do this.super().generateUI(); or super(this).generateUI();
    Pardon my ignorance, but I don't think this is the correct way of using super. I even tried this, and it gives compile errors as expected.

    Do anyone has any ideas on how to solve this?

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    what errors are you getting? I just did
    System.out.println(super.getName()) just fine.

  5. #5
    moaxjlou is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default

    I just did
    System.out.println(super.getName()) just fine.
    Did you do that in class B? I tried that and it still returns empty array


    Referring to my class B as the above post, my aim is storing an array of values (which is generated from generateNames of class A) into a variable in class B (the subclass)

    I'm not sure if you understand my question correct?

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Hmm. Honestly I don't see anything wrong with what your doing and why it won't work. I did(in my own code)

    Server[] siblings = super.getChildren();

    and that worked fine for me. Maybe someone else can pitch in some advise?

  7. #7
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default

    Your code works as expected here. No problems at all.

    1) It seems that your problem is in the generateNames() method. Could you post the body of your A.generateNames() method? It appears to be broken...

    I am using:

    Java Code:
    public ArrayList<String> generateNames() {
    	ArrayList<String> result = new ArrayList<String>();
    	result.add("Adam");
    	result.add("Smith");
    	result.add("Jones");
    	result.add("Norah");
    	return result;
    }
    2) As long as you are not overriding your generateNames() method, you might as well call it from class B (there is no need to call it from super, as B inherits the generateNames() method as it is defined in A)

  8. #8
    moaxjlou is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default

    Hi
    Java Code:
    It seems that your problem is in the generateNames() method
    Atually the generateNames() method does work. This is because I also have another method that makes use of this method (generateNames()) in the same class (class A) and it does return the array with the names ([Adam, Smith, Jones, Norah])

Similar Threads

  1. Calling Java methods form Python
    By mew in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 02:30 PM
  2. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM
  3. Problems with readLine() and calling methods
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 07:44 AM
  4. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 AM
  5. Invoking superclass methods... how?
    By rhobincu in forum New To Java
    Replies: 7
    Last Post: 08-09-2007, 03:10 PM

Posting Permissions

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