• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

overriding in java

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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 ?
 
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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 ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic