Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Inheritance in BlueJ, parent class has two parameters but child class has one...

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Inheritance in BlueJ, parent class has two parameters but child class has one...

    I've been given an assignment that involves inheritance on BlueJ.
    The superclass/ parent class has the following line of code:
    "
    abstract void changeSize(int newHeight, int newWidth);
    "
    The classes I am dealing with represent different shapes, where the changeSize methods are defined by two parameters for the size, whereas some are only defined by one parameter.
    My task is to correct this problem, but I have no idea where to start.
    Any help is greatly appreciated


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Inheritance in BlueJ, parent class has two parameters but child class has one...

    whereas some are only defined by one parameter.
    Sounds like you need to give a default value for one the argument you pass to the super's method.

    Can you make a small program to show the problem.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance in BlueJ, parent class has two parameters but child class has one...

    Yeah sure

    (Superclass)
    public abstract class Shapes
    {
    abstract void changeSize(int newHeight, int newWidth);
    }

    (Sub-class 1)
    public class Triangle extends Shapes
    {
    private int height;
    private int width;

    public void changeSize(int newHeight, int newWidth)
    {
    erase();
    height = newHeight;
    width = newWidth;
    draw();
    }
    }

    (Sub-class 2)
    public class Square extends Shapes
    {
    private int size;
    public void changeSize(int newSize)
    {
    erase();
    size = newSize;
    draw();
    }
    }


    So as you can see, in sub-class 1 where it represent a triangle, there are two values which are assigned to 'newHeight' and 'newWidth' from the superclass. In sub-class 2, however, it only requires one value.
    Hope this is what you asked for, thanks again

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Inheritance in BlueJ, parent class has two parameters but child class has one...

    Given that definition of Shape and that Square must extend Shape, you'll have to implement the Shape changeSize(..) method that takes 2 parameters in Square. In that method, check that both parameters are the same (i.e. height equals width), then call the single argument changeSize method passing either the height or the width. If the two parameters are not the same, an exception should be thrown. If you haven't covered exceptions yet, just decide which argument will specify the square size, pass it to the single argument method, and put a comment on the method explaining this (e.g. explain that argument 2 is not used).

    [It's worth considering whether it really makes sense to have a changeSize(..) method in Shape that takes 2 arguments, because you'll have the same problem with Circle... Often with this kind of thing there's no 'correct' answer. The best answer will generally depend on exactly how you're going to use the objects].
    Last edited by dlorde; July 19th, 2011 at 06:30 PM.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Inheritance in BlueJ, parent class has two parameters but child class has one...

    Quote Originally Posted by dlorde View Post
    you'll have to implement the Shape changeSize(..) method that takes 2 parameters in Square. In that method, check that both parameters are the same (i.e. height equals width), then call the single argument changeSize method passing either the height or the width.
    Personally I would do it the other way around. The single parameter method calls the 2 parameter method.
    Improving the world one idiot at a time!

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Inheritance in BlueJ, parent class has two parameters but child class has one...

    Quote Originally Posted by Junky View Post
    Personally I would do it the other way around. The single parameter method calls the 2 parameter method.
    Fair enough; I'm not sure it matters - it's not really like cascading constructors; when dealing with a list of Shapes, the 2 parameter method will be called, and I prefer to see the more general method calling the more specific method. I guess I think of it in terms of an existing Square class that implements the Shape interface, so the interface implementation delegates to the Square-specific method...

    Also because, for a Circle implementation, it would seem odd to have the single argument method call the 2 argument method which would then ignore one argument; and it makes sense to keep a consistent strategy - the Shape interface as a kind of Adapter pattern.

    But it's way past my bedtime, and I may be missing something really obvious
    Last edited by dlorde; July 19th, 2011 at 07:46 PM.

  7. The Following User Says Thank You to dlorde For This Useful Post:

    rph (July 20th, 2011)

  8. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance in BlueJ, parent class has two parameters but child class has one...

    Thank you for the help, got the assignment done thanks to you, and good to see you're from Faversham dlorde, as I'm only in Whitstable

  9. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Inheritance in BlueJ, parent class has two parameters but child class has one...

    Quote Originally Posted by rph View Post
    Thank you for the help, got the assignment done thanks to you, and good to see you're from Faversham dlorde, as I'm only in Whitstable
    Howdy neighbor! Glad you got it done

Similar Threads

  1. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  2. Access and set variable in parent class through child
    By java_newbie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 19th, 2011, 11:44 PM
  3. saving the data of the child jsp into parent jsp
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 15th, 2011, 11:05 AM
  4. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  5. GUI does not pop up in child process before parent is killed
    By Antipeko2 in forum Java Theory & Questions
    Replies: 0
    Last Post: February 26th, 2010, 06:32 AM

Tags for this Thread