Results 1 to 11 of 11
  1. #1
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default subclass calling superclass method

    I am having trouble calling superclass method. Here is recent code


    Java Code:
    import java.util.*;
     public class Mercedes
     
    {
    
      protected int [] ix = new int [50];
      protected int chamu;
      protected int icounter;
    
     //Class constructor
    
     public Mercedes ( int one, int two, int [] three)
      {
    
         icounter = one; 
         chamu = two;
         ix = three;
      }
      
       //method to take user input and load into array
    
     public void icards (int icounter, int chamu, int [] ix )
         {
     
        }
    
     //End of Method
    
    
    }
     //End of Class
    
    
     
    public class MercedesTest extends Mercedes
    {
      //Subclass Constructor
      public MercedesTest ( int one, int two, int [] three) 
        {
          //calls superclass constructor
           super ( one, two, three ); 
        }
          //This line(10) throws error! Attempts to calls superclass method
         super.icards( int one, int two, int [] three);
     }

    Here are the errors I get:

    MercedesTest.java:10:illegal start of type
    MercedesTest.java:10: ';' expected
    MercedesTest.java:10: invalid method declaration; return type required

    any suggestions greatly appreciated
    Last edited by stackptr89; 11-22-2010 at 01:11 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stackptr89 View Post
    I've searched around but could not find anything. I get "illegal start of type" and "invalid method declaration" errors. Suggestions greatly appreciated
    Count your curly brackets and see if they match up.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Yes they do.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stackptr89 View Post
    Yes they do.
    No they don't.

    kind regards,

    Jos (<-- loves to have an argument ;-)
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    sorry I missed that last bracket when I copied/pasted

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by stackptr89 View Post
    sorry I missed that last bracket when I copied/pasted
    If you're still having the error, then please post the most recent code, nicely formatted, the full error message, and indicate in the code with a comment, which line is causing the error.

    Luck!

  7. #7
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    my recent code is on there. tried to make it as easy as possible to read. thanks

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by stackptr89 View Post
    my recent code is on there. tried to make it as easy as possible to read. thanks
    The code you've posted has not yet corrected all curly brace problems and has not indicated by comment the line causing the error. I can wait.

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Your problem is actually caused by the fact that the method call is in "no mans land". That method call should occur inside another method or perhaps a constructor. If you don't want to modify the behavior of the method (eg. does the same thing if called by subclass or superclass) you don't need to define the method again in your subclass:
    Java Code:
    public class Parent {
      public void foo() {
        System.out.println("bar");
      }
    }
    
    public class Child extends Parent {
      //other methods and all that jazz
    }
    
    public class Test {
      public static void main(String[] args) {
        Child c = new Child();
        c.foo(); //will print out bar
      }
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    JoshAh, Fubarable I thought I fixed it but thanks for looking.
    m00nchile thanks!!! :D

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stackptr89 View Post
    Java Code:
          //This line(10) throws error! Attempts to calls superclass method
         super.icards( int one, int two, int [] three);
    When you call a method you don't mention the types of the arguments; those types are defined when the method was defined; you call it like this:

    Java Code:
         super.icards(one, two, three);
    When there is no method icards( ... ) defined in the current class the compiler will check whether or not it is defined in a superclass, so here you can delete the "super." prefix.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Using superclass fields in subclass method
    By lonegreyride in forum New To Java
    Replies: 12
    Last Post: 11-17-2010, 01:21 PM
  2. Replies: 6
    Last Post: 02-22-2009, 06:04 AM
  3. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM
  4. Calling methods from superclass
    By moaxjlou in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 12:07 AM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 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
  •