Results 1 to 11 of 11
- 11-21-2010, 07:05 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
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 appreciatedLast edited by stackptr89; 11-22-2010 at 01:11 AM.
- 11-21-2010, 07:12 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
- 11-21-2010, 07:17 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
Yes they do.
- 11-21-2010, 07:19 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
- 11-21-2010, 07:30 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
sorry I missed that last bracket when I copied/pasted
-
- 11-21-2010, 08:53 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
my recent code is on there. tried to make it as easy as possible to read. thanks
-
- 11-21-2010, 10:44 PM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 11-22-2010, 07:29 PM #10
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
JoshAh, Fubarable I thought I fixed it but thanks for looking.
m00nchile thanks!!! :D
- 11-22-2010, 07:40 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
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:
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.Java Code:super.icards(one, two, three);
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Using superclass fields in subclass method
By lonegreyride in forum New To JavaReplies: 12Last Post: 11-17-2010, 01:21 PM -
How2assign all instance vars of a subclass var to be same as a superclass var?
By gymshoe in forum New To JavaReplies: 6Last Post: 02-22-2009, 06:04 AM -
superclass and subclass
By mr idiot in forum New To JavaReplies: 19Last Post: 01-03-2009, 07:29 AM -
Calling methods from superclass
By moaxjlou in forum New To JavaReplies: 7Last Post: 12-11-2008, 12:07 AM -
which class is superclass and subclass?
By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-11-2007, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks