Results 1 to 15 of 15
- 11-21-2011, 10:12 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
extends class, why does it need another constructor?
I have a question. I am writing a program and I already have 4 classes that are working well together. Now I wanted to write another class that extends a class I have already written. I am confused, however, because I thought that when you "extend" a class, everything in that class (methods, constructors, variables, etc.) are now part of the new class. So I start writing the new class, put one simple method in it, and now it tells me it can't find a constructor.
public class Robot extends UrRobot{
public boolean frontIsClear(){
boolean clear=true;
if(w.hitWall(street, avenue, direction)){
clear=false;
}
return clear;
}
}
I thought everything in UrRobot class would also be in Robot class, but it tells me it can't find a constructor:
cannot find symbol
symbol : constructor UrRobot()
location: class UrRobot
public class Robot extends UrRobot{
^
1 error
I thought it would just use the UrRobot constructor? Can someone clarify this or how the extends thing works?
OH hehe, I suppose it would help if I include the UrRobot constructor, here it is:
public UrRobot(int street, int avenue, int direction, int beepers){
this.street=street;
this.avenue=avenue;
this.direction=direction;
this.beepers=beepers;
this.status=status;
w.robotList.add(this);
}Last edited by kyle_maddisson; 11-21-2011 at 10:29 AM.
- 11-21-2011, 10:29 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: extends class, why does it need another constructor?
Constructors aren't inherited; even more, how would you create a Robot when the Robot class doesn't have a constructor? The compiler tries to help you: if your class doesn't have a constructor, it generates one for you: it doesn't take parameters. There is more trickery going on: each constructor should either call another constructor in the same class or it should call a constructor from the super class; if you don't do that, the compiler does it for you: it makes a call to the no argument super constructor; so in your case the following is generated by the compiler:
next the compiler starts to complain about its own generated code: the super class (UrRobot) doesn't have a constructor that doesn't take arguments. Better build a constructor yourself in the Robot class so that you can create Robot objects.Java Code:public Robot() { super(); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-21-2011, 10:32 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: extends class, why does it need another constructor?
but it would just be the same as the UrRobot constructor, wouldn't it?
- 11-21-2011, 10:55 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: extends class, why does it need another constructor?
OK so what is the syntax of calling another classes constructor? I can't figure it out?
- 11-21-2011, 11:12 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: extends class, why does it need another constructor?
A constructor in the same class has to be called on the first line of a constructor with the syntax this( ... ) with the appropriate parameters; alternatively you can call a constructor from the super class with the syntax super( ... ), also with the appropriate parameters and also on the first line of the body of a constructor. You can only call one of them and if you don't do either, the compiler inserts a call to super().
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-21-2011, 11:15 AM #6
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: extends class, why does it need another constructor?
So it would be like this then?
public class Robot{
public Robot{
super(int street, int avenue, int direction, int beepers);
}
}
it doesn't work
- 11-21-2011, 11:25 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: extends class, why does it need another constructor?
Of course not because that code snippet is full of syntax errors: a constructor definition has a method definition like syntax, i.e. a formal parameters list; a call to a constructor is like a method call, i.e. you pass actual parameters and don't mention their type; I'm sure it's all in your textbook; it doesn't hurt to read it.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-21-2011, 11:28 AM #8
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: extends class, why does it need another constructor?
Well if I have to rewrite everything why extend a class in the first place? That don't make much sense.
- 11-21-2011, 11:39 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: extends class, why does it need another constructor?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-21-2011, 11:56 AM #10
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: extends class, why does it need another constructor?
Well I guess I don't understand what you are telling me. Here is the first part of the UrRobot class:
import java.util.ArrayList;
public class UrRobot{
/*********** declare variables for UrRobot class **************/
static MyWorld w = new MyWorld();
int street;
int avenue;
int direction;
int beepers;
String status=("on");
public String compass[]={"not used","North", "South", "East", "West"};
So I tried:A constructor in the same class has to be called on the first line of a constructor with the syntax this( ... ) with the appropriate parameters; alternatively you can call a constructor from the super class with the syntax super( ... ), also with the appropriate parameters and also on the first line of the body of a constructor. You can only call one of them and if you don't do either, the compiler inserts a call to super().
kind regards,
Jos
public class Robot extends UrRobot{
public Robot{
super(street, avenue, direction, beepers);
this.street=street;
this.avenue=avenue;
this.direction=direction;
this.beepers=beepers;
this.status=status;
w.robotList.add(this);
}
}
because after reading your post, this is what I interpreted you saying.
But that don't work either. And I don't understand why you have to define variables that are already defined in the class you are extending. Why are the variables in this class not the same as the parent class? Also, coding is like learning another language. Particularly, writing in another language. I come here for help learning how to "write" in another language. If you were from another country and were trying to learn how to write in english, but all you got was people telling you what verbs and nouns were, prepositions and predicates were, adjectives and adverbs were, would it help you learn how to 'write' english. I don't think so. Thanks for nothing.Of course not because that code snippet is full of syntax errors: a constructor definition has a method definition like syntax, i.e. a formal parameters list; a call to a constructor is like a method call, i.e. you pass actual parameters and don't mention their type
- 11-21-2011, 12:09 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 11-22-2011, 12:59 AM #12
Re: extends class, why does it need another constructor?
Wow.
Another person blaming their own stupidity on others.
- 11-24-2011, 05:22 AM #13
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: extends class, why does it need another constructor?
How was I blaming it on others? And I'm not stupid, I'm learning.
- 11-24-2011, 06:00 AM #14
- 11-24-2011, 06:36 AM #15
Member
- Join Date
- Nov 2011
- Posts
- 47
- Rep Power
- 0
Re: extends class, why does it need another constructor?
Well first of all, I would like to apologize for my comments. I had been working on this all day, and my mind was probably in three different places in this code at the same time. It's one of the trickiest I have attempted so far due to the fact that I have to get 4 separate class files working together. I really due appreciate everyone's help. To take the time to peruse the forums and read other people's questions about their code and try and help them with their problems is truly commendable. I was just at my wits end with this, and I tried a bunch of different things and I couldn't get it right. I eventually did figure it out and then realized how easy it was. Felt pretty dumb afterward. My frustration was only compounded by the fact the similarly to the book it talks about how to do it, but it never really shows you a useful example. I understand that people don't want to write my code for me, however, explanations without examples sometimes just further confuse me. Also, I'm sure I am not the only one that feels like they get the concepts, but need help with the syntax, and when you miss one semicolon or comma and you get a page long list of errors, it's overwhelming for a newb.
Again I apologize for my disrespect and lack of gratitude.
Similar Threads
-
Creating a class that extends Activity and implements OnClickListener
By sehudson in forum AndroidReplies: 4Last Post: 01-17-2012, 12:39 AM -
Class that extends JFrame help
By javaman1 in forum New To JavaReplies: 5Last Post: 11-10-2010, 02:29 AM -
[SOLVED] how to print a class that extends another class
By alpdog14 in forum New To JavaReplies: 3Last Post: 03-19-2009, 05:00 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks