File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the logic behind designing how Overloaded vs Overridden methods are invoked?

 
Sandra Bachan
Ranch Hand
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

I am studying for SCJP 6 Exam by reading

SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)
By: Kathy Sierra; Bert Bates
Print ISBN-10: 0-071-59106-0


I am on Chapter 2, Overriding / Overloading (Exam Objectives 1.5 and 5.4). From what I understand, OVERLOADED methods are invoked based on the information received by compiler at compile time, while OVERRIDDEN methods are invoked based on information received by JVM at runtime. What is the LOGIC behind this design, why isn't it vice versa, i.e. OVERLOADED methods invoked at runtime, OVERRIDEN methods invoked based on information received by compiler.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler does not check for everything at the compile time only
means the checking is superficial and compiler does not know what will happen at run time
At compile time , the type of the object is checked and at the run time the object is checked
Remember :
  • Which overloaded method to be called is decided at the compile time
  • Which overriden method to be called is decided at the run time

  • To make things clearer
    you can take a look at the following code

     
    Harpreet Singh janda
    Ranch Hand
    Posts: 317
    Eclipse IDE
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Logic is run time polymorphism.
     
    Prithvi Sehgal
    Ranch Hand
    Posts: 774
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello

    The choice of which overloaded method to call is made at compile time. Compiler looks at the reference type to decide which overloaded
    method to call.

    The choice of which overridden method to call is based on actual object type based at run-time. Polymorphism concept is applied here.

    Hope this helps,
     
    Sandra Bachan
    Ranch Hand
    Posts: 434
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I appreciate the clarifications, thank you all
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic