• 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

method overwriting

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) What is method overwriting ? How is it different from overriding and overloading ?

2) In methoid overriding, we can change definition of method and in method overloading, we can change method parameters. Buit in neither case, we can change access modifier or return type. Now in case if wekeep the same method name and alter access modifier and return type , what is it called ? Is it some other operation like overriding or overloading ? Or will that be compilation error ?

Thanks
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirjari patel wrote:1) What is method overwriting ? How is it different from overriding and overloading ?


The term means nothing in Java. Wherever you read it, they probably meant overriding, and misspelled it.

nirjari patel wrote:2) In methoid overriding, we can change definition of method and in method overloading, we can change method parameters. Buit in neither case, we can change access modifier or return type.


That's not true. An overloaded method can have a completely different access modifier, and a completely different return type. An overriding method can have a different access modifier, if the new modifier is more public than the old one. Default can be overridden with protected or public, and protected can be overridden with public. And the overriding method can have a different return type, provided the new return type is a subtype of the old one. So a method that returns Object can be overridden with a method that returns String. But a method that returns String cannot be overridden with a method that returns Object. That's because String is a subtype of Object, and Object is not a subtype of String.

nirjari patel wrote:Now in case if wekeep the same method name and alter access modifier and return type , what is it called ? Is it some other operation like overriding or overloading ? Or will that be compilation error ?


It's overriding. But if you violate the rules of overriding that I just stated, you will get a compile error.
 
reply
    Bookmark Topic Watch Topic
  • New Topic