• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding Vs Shadowing

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
https://coderanch.com/t/251038/java-programmer-SCJP/certification/polymorphism


[ October 01, 2005: Message edited by: A Kumar ]
 
ganga prabhu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why static method looks on references and instance method looks on objects??
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

https://coderanch.com/t/251041/java-programmer-SCJP/certification/Method-calling
[ October 03, 2005: Message edited by: Akhil Trivedi ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..

 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akil !!!
u have very good point out there
good!! and thanx
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic