Results 1 to 8 of 8
  1. #1
    nethz13 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default Got confused with extending classes.

    this is the original class slidingbody.
    Java Code:
    SlidingBody(float dx, float dy, float period, BodyShape shape)
    Grandmonster extends from slidingbody
    Java Code:
    public class GrandMonster extends SlidingBody implements CollisionListener
    {   
        
        private Game game;
        
    
        public GrandMonster(Game game)
        {
            super(135, -45, 4.5f, new BodyShape(0,0, 12,0, 12,13, 0,13));
            this.game = game;
            setFloating(true);
            setGhostly(true);
            setColor(java.awt.Color.BLACK);
            setBodyImage(new BodyImage("bomb.gif"));
        }
    and now I want to make a monster extends from Grandmonster
    how should I write the constructor?

    i try to use
    Java Code:
    super(135, -45, 4.5f, new BodyShape(0,0, 12,0, 12,13, 0,13));
    it returns a error saying can not find symbol - constructor GrandMonster
    Last edited by nethz13; 04-18-2010 at 11:06 PM.

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

  3. #3
    nethz13 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Super can only take a Game argument, that's it as it has to match one of the super's constructors exactly.
    Thanks again for quick reply. but I don't really understand what you trying to say, could you please explain a bit more?
    Does it mean I can only use super once or?

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

    Default

    If class B extends A, and A has one constructor that takes an int parameter, then B must call that super constructor in its constructor:

    Java Code:
    class A {
      public A(int foo) {
        
      }
    }
    
    class B extends A {
      public B() {
        super(3); // B has to call the super constructor passing some int argument
      }
    }

  5. #5
    nethz13 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    If class B extends A, and A has one constructor that takes an int parameter, then B must call that super constructor in its constructor:

    Java Code:
    class A {
      public A(int foo) {
        
      }
    }
    
    class B extends A {
      public B() {
        super(3); // B has to call the super constructor passing some int argument
      }
    }
    ok, i got some idea now, just trying to see if it works
    thanks.

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

    Default

    Note that if the A class has a no-parameters constructor along with the int constructor, then B can use either of those super constructor calls and in fact will call the non-parameter constructor by default even if you don't explicitly tell it to.

  7. #7
    nethz13 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Note that if the A class has a no-parameters constructor along with the int constructor, then B can use either of those super constructor calls and in fact will call the non-parameter constructor by default even if you don't explicitly tell it to.
    Thanks, now i got another question but different topic.
    should I open another thread?

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

Similar Threads

  1. Extending or changing DTD for validator
    By dbil in forum Web Frameworks
    Replies: 0
    Last Post: 10-09-2009, 10:54 AM
  2. OOP Question re. private variables and extending classes
    By ImplicitCharm in forum New To Java
    Replies: 7
    Last Post: 07-28-2009, 03:46 PM
  3. [SOLVED] Problem with extending classes...
    By Bizmark in forum New To Java
    Replies: 4
    Last Post: 04-07-2008, 11:21 PM
  4. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 09:49 AM
  5. EXTENDING the string class
    By ferno in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 09:41 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
  •