aspose file tools*
The moose likes Java in General and the fly likes overriding in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "overriding in java" Watch "overriding in java" New topic
Author

overriding in java

Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 215
<code>

I am solving mock questions


class super
{
public float getNum()
{ return 3.0f;

}

}

public class P2 extends super
{

}

Q1) which method causes compilation error?

A)public void getNum() -- Why this option is wrong?
B) public void getNum(double d) -- why this option works ?
C)public float getNum(){return 4.0f}
D)public double getNum(){return 4.0d}

Q2) I am getting confused of overloading and overrding ?
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by amolpalekar kadolkar:

I am solving mock questions



Q1) which method causes compilation error?

A)public void getNum() -- Why this option is wrong?
B) public void getNum(double d) -- why this option works ?
C)public float getNum(){return 4.0f}
D)public double getNum(){return 4.0d}

Q2) I am getting confused of overloading and overrding ?


Mr Palekar,

You need to be more clear about your question. Where you are going to write these all methods in Code.

Anyways, If you are defining the same method in same class then it's Overloading.

And if you are defining the method in another class then that is called as Overriding.


And also whenever you post a code in forum it would be great if you use CODE tags..
[ November 01, 2006: Message edited by: A D Sharma ]

The Best way to predict your future is to create it - Every great individual common man
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
"A" has a signature that differs from one in the super class only by return type. That's not overloading or overriding ... it's just illegal. The following method should work whether you call it with an instance of super or an instance of p2. Think about why the compiler won't allow the signature in p2.

"B" has a signature that differs by argument type. That's overloading whether in the same class or another. The compiler and runtime can clearly tell what code to call and what should be returned.

Did that help?

BTW: Get in the habit of starting classnames with a capital letter, like Super and P2. Other folks who read the code will immediately spot the caps and realize you mean a class.
[ November 01, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
A D Sharma posted November 01, 2006 05:50 AM
Anyways, If you are defining the same method in same class then it's Overloading.

And if you are defining the method in another class then that is called as Overriding.




This is wrong!


It is true, that methods can be overridden only in a subclass. But you can overload methods in the same or in subclasses. And the difference has something to do with return types and parameter list.

Here's an example of an overload in a subclass:




Overriding: in a subclass
method must have the same name and the same parameter list.
method must have the same return type (or see covariant returns for Java5)
+ some additionals regarding visibility, exception throwing...

Overloading: in the same class or a subclass
method must have the same name.
method must have a different parameter list.
method may have a different return type



The difference between overriding and overloading is extremely important. For example different behaviour in late binding. You cannot get it by posting to or reading in a newsgroup - not even the Javaranch.

Study the Sun Tutorials. Or perhaps have a first idea with wikipedia.


Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding in java