• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the Difference between method overriding and redefining

 
Sneha Bharti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1604
5
Android Java Java ME
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sneha Please use code tags while posting code.
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sneha Bharti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Swastik I read the link and got it nw...
Thanks Martin, I got the reason for the output...
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sneha please Use Code Tags when you post a source code. Also please Use Real Words instead of words like "pl" instead of "please"...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic