aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading and Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading and Overriding" Watch "Overloading and Overriding" New topic
Author

Overloading and Overriding

Srinivas Palam
Ranch Hand

Joined: Oct 20, 2005
Posts: 51
Can some one please explain with simple class example, the meaning of object type and reference type. I am little confused. I was reading scjp6 book and found following description.

To summarize, which overridden version of the method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type of the argument passed at compile time. If you invoke a method passing it an Animal reference to a Horse object, the compiler knows only about the Animal, so it chooses the overloaded version of the method that takes an Animal. It does not matter that at runtime there's actually a Horse being passed.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7






1. What would the output be?
2. Will this compile?
3. Will this compile?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Srinivas Palam wrote: object type and reference type.

there is no object type, i guess it is reference in java. (as far i know ;) )
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Consider the expression

Animal a = new Horse();

here a is a reference variable of the type Animal. so i guess reference type means type of the reference

Object type means type of the object on the other end of a which is Horse. now new Horse() is a Horse, is a Animal(assume Horse subclass from Animal), is a Object.
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

just to add to gurupreet,



Here List is the reference type
ArrayList is the object type

FWIW,

when it comes to production ready code, the object type wont be so straight forwardly clear. It depends on the object passed at runtime. For ex.



where listOfAnimals is a list of some animals.

Hope that helps.

- Rakesh K.


Warm Regards,
Rakesh
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Seetharaman Venkatasamy wrote:
Srinivas Palam wrote: object type and reference type.

there is no object type, i guess it is reference in java. (as far i know ;) )


In addition to this, there is no object type, there is reference variable type and the actual object on the heap of a particular class referred by the reference variable at run-time.


OCPJP 6 86%
Srinivas Palam
Ranch Hand

Joined: Oct 20, 2005
Posts: 51
Thanks for your responses. Now I understood.

One more question, as we cannot override final and staic methods, Is it applicable to overloading methods also? My answer is Yes, but want to confirm.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Srinivas Palam wrote:Thanks for your responses. Now I understood.

One more question, as we cannot override final and static methods, Is it applicable to overloading methods also? My answer is Yes, but want to confirm.


do you want to say that we cannot overload final and/or static methods ?

the answer is no we can. we can overload final methods and/or static methods. overloading means same method name but with different number or type of arguments passed. for e.g

public final void SomeMethod(int a , int b) { }

public final String SomeMethod(int a , int b, int c) { }

public final static String SomeMethod(Animal a ) { }

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 801
    
    1
Hi, Srinivas Palam buddy,



Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi Srinivas Palam,

Please, UseOneThreadPerQuestion
 
wood burning stoves
 
subject: Overloading and Overriding