Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Vs Shadowing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding Vs Shadowing" Watch "Overriding Vs Shadowing" New topic
Author

Overriding Vs Shadowing

ganga prabhu
Greenhorn

Joined: Sep 29, 2005
Posts: 10
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

Joined: Jul 04, 2004
Posts: 979
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

Joined: Sep 29, 2005
Posts: 10
why static method looks on references and instance method looks on objects??
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
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.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi Akhil...


Can you elaborate your point further...??


[ October 03, 2005: Message edited by: A Kumar ]
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
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

Joined: Jul 04, 2004
Posts: 979
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

Joined: Sep 14, 1999
Posts: 2813
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


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Ashok George
Ranch Hand

Joined: Mar 25, 2005
Posts: 87
Akil !!!
u have very good point out there
good!! and thanx
 
Consider Paul's rocket mass heater.
 
subject: Overriding Vs Shadowing