• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

override or overload?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Here is another question from me. If say i have a superclass method that looks like this

public <return_class> meth1 ( <signature_class> )

and the subclass has a method

public <return_class> meth1 ( <sub_class_of_signature_class> )

Am i overloading or am i overriding.

Thanks
venu
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried writing some code to test this out?

If the method's parameters are different, it's overloading.
[ May 19, 2008: Message edited by: Stevi Deter ]
 
venu surampudi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what i have done.

In the superclass

public int getNumber (Number i){

System.out.println("super class version of getNumber");

Integer k = (Integer)i;

return k;
}

In the subclass

public int getNumber (Integer i){

System.out.println("subclass version of getNumber");
return 1;
}

In the main method


superclass mn = new Main();

mn.getNumber(5);

I get

super class version of getNumber

which means it has not been overridden. SO is the following a valid statement?

"A method can only be considered overridden if the signatures match exactly, an event in which the the signatures pass an is a relationship with the parents method parameters, it is still considered an overload and not an override"
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your complete code? What version of Java are you using? The code you list doesn't compile as you've written it.

Meanwhile, here's some quick code I wrote to show how the overloading versus overriding works:
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To venu...


public int getNumber (Number i){

System.out.println("super class version of getNumber");

Integer k = (Integer)i;

return k;
}

In the subclass

public int getNumber (Integer i){

System.out.println("subclass version of getNumber");
return 1;
}

These are two overloaded methods and not overriden.
so whenever you call method like
supurclass ref=subclass object;
ref.getNumber(5);

you will get superclass version,always.


Tushar
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by venu surampudi:
[QB]

"A method can only be considered overridden if the signatures match exactly,/QB]



Regarding above , its true for method parameters, but you can change the return type to be more specific. That is a sub class of the actual return type can be given in the overridden method.
 
venu surampudi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you guys, i get the point now.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I understand it these are overloaded methods and sice you are passing an int value the it will be wrapped to Integer and it will invoke the Integer method. if no Integer argument exist it will widen to Number and invoke the superclass method.

Trust me I'm preparing for the SCJP and practiced this a few times.
reply
    Bookmark Topic Watch Topic
  • New Topic