Results 1 to 15 of 15
Like Tree2Likes
  • 2 Post By Junky

Thread: extends class, why does it need another constructor?

  1. #1
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default 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.

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

    Default 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:

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: extends class, why does it need another constructor?

    but it would just be the same as the UrRobot constructor, wouldn't it?

  4. #4
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default 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?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: extends class, why does it need another constructor?

    Quote Originally Posted by kyle_maddisson View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default 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

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

    Default Re: extends class, why does it need another constructor?

    Quote Originally Posted by kyle_maddisson View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default 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.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: extends class, why does it need another constructor?

    Quote Originally Posted by kyle_maddisson View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default 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"};

    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.
    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.

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

    Default Re: extends class, why does it need another constructor?

    Quote Originally Posted by kyle_maddisson View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: extends class, why does it need another constructor?

    Wow.

    Another person blaming their own stupidity on others.
    Fubarable and JosAH like this.

  13. #13
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: extends class, why does it need another constructor?

    How was I blaming it on others? And I'm not stupid, I'm learning.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: extends class, why does it need another constructor?

    Quote Originally Posted by kyle_maddisson View Post
    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".

  15. #15
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default 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

  1. Replies: 4
    Last Post: 01-17-2012, 12:39 AM
  2. Class that extends JFrame help
    By javaman1 in forum New To Java
    Replies: 5
    Last Post: 11-10-2010, 02:29 AM
  3. Replies: 3
    Last Post: 03-19-2009, 05:00 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

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