• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method calling question  RSS feed

 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why does the code above print "Bird"? Even the print method is not overriden, I thought the print method in Raptor class would get invoked because method invocation uses late binding(print is not static). Can someone please explain to me, Thanks a lot.
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know the answer,

Bird c = new Raptor();c.print(new Raptor());

What you said method is not overridden is true.

So check on which reference you are calling method (Bird class)
so compiler checks if method is there in Bird class answer is YES,
at runtime actual object refered by c is of Raptor but method is not overridden ALRIGHT hence Bird's print is called.

new Raptor() can be assigned to superclass reference.
 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friend,
in your code you have overloaded the print method rather than overriding.a call to overridden method is resolved at run time whereas a call to overloaded method is resolved at compile time.
the compiler checks the reference type and not the object type while calling the overloaded method.

Bird c = new Raptor();// In this line, c is of the type bird.
c.print(new Raptor());// thatswhy while calling print here, print method of Bird class is called.

hope this helps.
regards,
Raja
 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for answering.
 
Jyothi Bhogadi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
on what basis you decide whether the method is overloaded or overidden when the signature is exactly the same like in the above example....

Regards
J
 
Manoj Kumar Sikhakolli
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

A method is said to be overloaded if the signature of the method changes. The Signature of the method is method name, number & type of arguments.

So if u say a method is a overloaded version of other , then

1. The Arguments should change( either number or type or both) with the same method name.

2. The return-type may change.

But in the case of over riding, we say that a method is overridden only when the signature of the overriding method is the same as that of overridden method.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by B.Jyothi:
hi
on what basis you decide whether the method is overloaded or overidden when the signature is exactly the same like in the above example....

Regards
J


Hi,
I dont think signatures of both the methos are same:


This is just overloading.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

A method is overloaded if there exists another method to the actual method..

with different signature...(not considering the return type)

i.e. same name..different argument list/different order ..

Math.abs(int x)
Math.abs(float f)


void Test(int i,float y);
void Test(float y, int i);

Mind you ..if you have another method with same signature as above but with return type.. compiler flags an error for duplicate method...

int Test(float y, int i);

Overriding means..You are providing another implementation to an already existing method..

Ex: equals method in object is overridden in string class...

Same name and same parameter list...And remember overriding comes into picture...during inheritance ,polymorphism...

Hope you got it..

In a nutshell...overriding is redefining an implementation;
overloading is making use of a method name with different parameter list for multiple implementations..
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification.
But i have one more doubt.
I had read that a subClass reference can be used instead of superClass.
So when Raptor Class is a subclass of Bird Class, does
"public void print(Raptor b)"
not override
public void print(Bird b) ?
 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Amrutha,
a method is said to be overridden when the method in the subclass has a signature which is exactly same as the method in the super class, for instance when you override the method equals of Object class, you should do it as follows.
public boolean equals(Object o)
however an overriding method need not declare any exceptions that it would not throw by itself,it cannot throw broader exceptions,and it cannot have more restrictive access modifier than that of the overridden method.
hope this helps.
regards,
Raja
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok . That means we have to pass the same parameters.
Even if the parameter is a subclass reference, that would not mean overriding.
Thanks Rajaraman .
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Amritha,

The following is an overridden method.

class Bird {
public void print(Bird b){
System.out.println("Bird");
}
}

class Raptor extends Bird {
public void print(Bird b){
System.out.println("Raptor");
}
public static void main(String[] args) {
Bird c = new Raptor();
c.print(new Raptor());
}
}

it prints Raptor. As you said the Super class reference can be used in the Subclass, but not viceversa.

thanks
Sampath
 
Will Fleming
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a pretty good question. I definitely would have missed it on a first try.

Preparing for this exam is scary (but in a good way)~!


[ October 06, 2005: Message edited by: Will Fleming ]
 
Ashok George
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool thread..
thanx for ur sights guys!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!