File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the Difference between method overriding and redefining Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the Difference between method overriding and redefining" Watch "what is the Difference between method overriding and redefining" New topic
Author

what is the Difference between method overriding and redefining

Sneha Bharti
Greenhorn

Joined: May 25, 2010
Posts: 12
can anyone tell me the difference between method overriding and redefining?
Am not getting it clearly..
And pl go through this code:

class Alpha{
public void foo(String... args)
{System.out.print("Alpha:foo");}
public void bar(String a)
{System.out.print("Alpha:bar");}
}

public class Beta extends alpha{
public void foo(String a)
{System.out.print("Beta:foo");}
public void bar(String a)
{system.out.print("Beta:bar");}
public static void main(String[] argv){
Alpha a=new Beta();
Beta b=(Beta)a;
a.foo("test"); b.foo("test");
a.bar("test"); b.bar("test");
}
}

OUTPUT:
Alpha:foo Alpha:bar Beta:foo Beta:bar

Now pl tell me the reason for this output...
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1451
    
    6

http://www.coderanch.com/t/411202/java/java/difference-between-redefining-overriding-method


Swastik
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Sneha Please use code tags while posting code.


Life is easy because we write the source code.....
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
I get output: Alpha:fooBeta:fooBeta:barBeta:bar Only the method public void bar(String a) in Beta actualy overrides the one in Apha. You then invoke two different foo methods.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Sneha Bharti
Greenhorn

Joined: May 25, 2010
Posts: 12
Oh m really sorry...
Actually the output is given Alpha:foo Beta:bar Beta:foo Beta:bar
But I m getting the output: Alpha:foo Beta:foo Beta:bar Beta:bar

This is the question of SCJP Dump5 from the topic OOPs, question1..
Sneha Bharti
Greenhorn

Joined: May 25, 2010
Posts: 12
Thanks a lot Swastik I read the link and got it nw...
Thanks Martin, I got the reason for the output...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Sneha please Use Code Tags when you post a source code. Also please Use Real Words instead of words like "pl" instead of "please"...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the Difference between method overriding and redefining