File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding Vs Shadowing

 
ganga prabhu
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I have one doubt while preparing for SCJP

What is differences between Overiding and Shadowing

Variables and Static methods are shadowing and instances method are overridden..??? how???

can some one explain little bit clearly
 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Overriding means the instance methods in a parent class ...are redefined in the child class...


During the execution..which method should be executed depends upon the object...used in the call...


For static methods even if the child class has a method as the parent class ...
You cannot call it as overridden...since the static methods are class level methods...

Static methods cannot be overridden..

If you consider the child class...We can say that the method in child hides the method in parent...


If you try to call these method...which one should be executed depends upon the type of reference than the object at runtime..

Have a look here..
http://www.coderanch.com/t/251038/java-programmer-SCJP/certification/polymorphism


[ October 01, 2005: Message edited by: A Kumar ]
 
ganga prabhu
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why static method looks on references and instance method looks on objects??
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be the method static or not, but if the call is made in non-static context then I guess it is the reference that counts not the object for resolving which method to call.
 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Akhil...


Can you elaborate your point further...??


[ October 03, 2005: Message edited by: A Kumar ]
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean forget about the method type, whether it is static or non-static, but when you call a method using an instance variable (though static methods can be called using classes directly) it is always the reference that matters not the object. That is , you can have a child object in a parent reference. Using such a reference to access the method, would search called method in parent class only.

Something similar in this region...

http://www.coderanch.com/t/251041/java-programmer-SCJP/certification/Method-calling
[ October 03, 2005: Message edited by: Akhil Trivedi ]
 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes you are have a good point...there

Irrespective of an instance method or a static method reference is

considered..




Here //1 will flag an error beacuse m1() is not defined in parent class
even though the object instantiated is child..

 
Marcus Green
arch rival
Rancher
Posts: 2813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sample chapter from the book JavaRules is called
Scope and shadowing, which may help explain some of the concepts.

http://www.javarules.com/book/sample.pdf
 
Ashok George
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akil !!!
u have very good point out there
good!! and thanx
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic