Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help understanding a question

Options
  • 20-02-2012 3:04pm
    #1
    Registered Users Posts: 3,500 ✭✭✭


    Hey could I have a bit of help understanding a question in a SCJP book im reading. Question is..

    13. Given the following class definitions
    1. class Parent {
    2. public void printResults(String... results) {
    3. System.out.println(“In Parent”);
    4. }
    5. }
    6.
    7. class Child extends Parent {
    8. public int printResults(int id) {
    9. System.out.println(“In Child”);
    10. return 0;
    11. }
    12.}
    


    what is the result of the following statement?
    new Child().printResults(0);
    A. In Parent
    B. In Child
    C. 0
    D. Line 2 generates a compiler error.
    E. Line 8 generates a compiler error.

    I said answer=E as the child method printResults is overridding the parent one and you cannot change the method signature but this is the answer the book has given..

    13. B. The code compiles fine, so D and E are incorrect. The printResults method in Child is overloading printResults in Parent, not overriding. In method overloading, the return type can be any data type, so printResults in Child returning an int is not a problem.
    Invoking printResults with an int argument calls the method on line 8, which displays In Child. Therefore, the answer is B.

    How can I tell that that is overloading and not overriding??

    Thanks for the help


Comments

  • Registered Users Posts: 2,019 ✭✭✭Colonel Panic


    You can have multiple methods with the same name but different signatures in Java. That's what overloading is.

    You're overloading overriding a method in a subclass if it's signature is the same as it's parent's.


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    As long as the signature in the arguments is different, overloading is occurring, overriding would be to change the method for the same signature.


  • Registered Users Posts: 589 ✭✭✭loctite


    You can have multiple methods with the same name but different signatures in Java. That's what overloading is.

    You're overloading a method in a subclass if it's signature is the same as it's parent's.


    I'm no expert but I don't think that is true.

    You are OverWriting a method in a subclass when the signature is the same.
    The signature may be the same but the method is different.

    class SuperClass{

    static void methodOne()
    {
    System.out.println("Printout");
    }

    }

    class SubClass extends SuperClass{

    static void methodOne() // Overwritten Method
    {
    System.out.println("Print This OUT");
    }

    static int methodOne(int a, int b) // Overloaded Method
    {
    your code here....;
    return your int;
    }
    }

    Above example... method has same signature but is OVERWRITTEN to do something else.

    An example of an OVERLOADED method is where the method parameters/signature is overloaded to take different arguments.

    http://www.java-samples.com/showtutorial.php?tutorialid=284


  • Registered Users Posts: 2,019 ✭✭✭Colonel Panic


    Well spotted! My first example should be overriding! It's a typo, not me being wrong. (I'm never wrong :P)


  • Registered Users Posts: 3,500 ✭✭✭Drexel


    Hey lads thanks for the replys. Think i get it. I understand overloading and overriding but i think the fact the overloaded method was in the subclass was throwing me off a little bit. It's the first time I've come across an overloaded method in a child class


  • Advertisement
Advertisement