Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading and Overriding

 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • 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.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





1. What would the output be?
2. Will this compile?
3. Will this compile?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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 ;) )
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • 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.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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 ) { }

 
Himai Minh
Ranch Hand
Posts: 1296
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Srinivas Palam buddy,



 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivas Palam,

Please, UseOneThreadPerQuestion
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic