Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Overloading and Overriding

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





1. What would the output be?
2. Will this compile?
3. Will this compile?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Srinivas Palam wrote: object type and reference type.


there is no object type, i guess it is reference in java. (as far i know ;) )
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ) { }

 
Bartender
Posts: 2124
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Srinivas Palam buddy,



 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srinivas Palam,

Please, UseOneThreadPerQuestion
 
reply
    Bookmark Topic Watch Topic
  • New Topic