Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Add code to a constructor

    Hi
    As part of an assignment I have being asked to add code to a constructor .
    the constructor initializes a dog object to black and its position to 1 in the superclass.
    A subclass requires to initialize the dog to white and position to say 2 .
    I would be grateful for any pointers to how best achieve this .
    regards

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    How this is done depends entirely on the properties of the superclass. If the colour and position are both protected or private, than you can write directly to the properties of the superclass. If the superclass accepts a colour and position as a part of its constructor, you can chain the subclass to the superclass with super(colour, position);.
    Chances are if you are required to alter the code, than the superclass does not accept parameters (otherwise an overwrite is completely useless since the subclass can just use the superclass constructors), which will lead me to believe that the properties are likely protected. If they are not, you need mutator's in order to set the new colour and position. In either the these two events, you will be overridding the default superclass constructor.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder DELOCH's Avatar
    Join Date
    Apr 2006
    Location
    Canada
    Posts
    537
    Thanks
    4
    Thanked 2 Times in 2 Posts
    1. I do not believe we are allowed to help you with your assignments.

    2. It would be far more helpful to know
    a) the source code
    b) what you have tried to do so far

    If it's a problem with phrasing, ask your professor/teacher


  •  

    Posting Permissions

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