This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes method overwriting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method overwriting" Watch "method overwriting" New topic
Author

method overwriting

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 357
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
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2970
    
    9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method overwriting
 
Similar Threads
Doubt on access verifers
method overloading issues
interface problem
need help!!
difference