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);
}
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:
Code:
public Robot() { super(); }
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.
kind regards,
Jos
Re: extends class, why does it need another constructor?
but it would just be the same as the UrRobot constructor, wouldn't it?
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?
Re: extends class, why does it need another constructor?
Quote:
Originally Posted by
kyle_maddisson
OK so what is the syntax of calling another classes constructor? I can't figure it out?
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
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
Re: extends class, why does it need another constructor?
Quote:
Originally Posted by
kyle_maddisson
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
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,
Jos
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.
Re: extends class, why does it need another constructor?
Quote:
Originally Posted by
kyle_maddisson
Well if I have to rewrite everything why extend a class in the first place? That don't make much sense.
It's up to you; you started this question because you wanted to extend a class of yours; extending a class does take a bit of code (you don't have to rewrite everything). I outlined how it should be done; you can use it or ignore it.
kind regards,
Jos
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"};
Quote:
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
So I tried:
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.
Quote:
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
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.
Re: extends class, why does it need another constructor?
Quote:
Originally Posted by
kyle_maddisson
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.
You're most welcome of course; goodbye.
Jos
Re: extends class, why does it need another constructor?
Wow.
Another person blaming their own stupidity on others.
Re: extends class, why does it need another constructor?
How was I blaming it on others? And I'm not stupid, I'm learning.
Re: extends class, why does it need another constructor?
Quote:
Originally Posted by
kyle_maddisson
Thanks for nothing.
Maybe it wasn't your intention but the above comment was interpreted by me as "All your post were uselss and it's your fault that I still don't understand".
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.