This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes shadowing, overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "shadowing, overriding" Watch "shadowing, overriding" New topic
Author

shadowing, overriding

Anand Shivadas
Greenhorn

Joined: Oct 17, 2000
Posts: 22
Hi,
What is the difference between "overriding" and "shadowing"?
static methods can be "shadowed" in sub class, right?
Thanks,
Anand
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
Anand,
U R Right,
Static methods are not ovveriden, but they are HIDDEN(SHADOWED).
instance methods can be overriden.... meaning u can provide a different behaviour in the overriding(Child) method, than in the overridden(Parent) method....
HTH,
Aruna


[This message has been edited by Aru Ven (edited October 17, 2000).]
Anand Shivadas
Greenhorn

Joined: Oct 17, 2000
Posts: 22
Aruna,
Actually what is the difference between overriding and shadowing? Shadowing the static method in sub class, we can get the overriding functionality, then what is the difference between them?
Thanks,
Anand
Anand
Originally posted by Aru Ven:
Anand,
U R Right,
Static methods are not ovveriden, but they are HIDDEN(SHADOWED).
instance methods can be overriden.... meaning u can provide a different behaviour in the overriding(Child) method, than in the overridden(Parent) method....
HTH,
Aruna

[This message has been edited by Aru Ven (edited October 17, 2000).]

Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
Anand,
Look at the following details given by MA in one of the posts... This should clear all ur doubt's
Aruna
Overriding a method means what? When you create an object from the subclass which does the overriding, the re-defined (new) behaviour must be kept intact when the method is called on this object right? In other words ,there is a base class and it has a method say, void m1() which prints "I like Java". And there is a subclass which is derived from this parent, and it re-defines the beahviour of this inherited method from base class and say, it prints "No I don't like Java". Also lets assume that this method is instance method. So when you create a physical object using Base b = new Base(); and call b.method(), what it will say. Obviously it is real base class object and you would expect it to say "I like Java" Right? Simillarly when you create like this Subclass s = new SubClass(); and call s.method(), it will behave itself and says "I don't like java".
Now lets say we do like this Base b = new Subclass(); Now what do u expect? Since this method is instance method, which is overriden by the subclass, eventhough you see through a mirror and see it's another face of Base class, in case of overriding a method, the true face of the overriden method only will be seen. This is oop. Now also it will say "No I don't like Java';
In order to analyze the static concept, let us take the same case as described above. Let's assume Both Base class and subclass has their own version of static void method(); This means what? Each class as a independent class of its own wants to give a COMMON behaviour for all objects created from them. This means when the
subclass redefines the same static void method1(), it DOES NOT OVERRIDE. It hides. In the sense, If you apply the same 2 tests as shown above Base b = new Base(); b.method() WILL call Base class version. Subclass s = new Subclass(); s.method() WILL call subclass version. And you know waht? Base b = new Subclass() WILL NOT CALL SUBCLASS VERSION. IT WILL CALL THE BASE CLASS VERSION of the static method. Seeing a subclass object through a base class mirror and when we see a static method, the MIRROR type's static only will be seen. So the mirror beahves differently.
If the static method WOULD HAVE BEEN OVERRIDEN we MUST see the subclass version right? Then why we see the static base class method? So static method are NOT OVERRIDEN.
For the compiler part, it does say anything about the 'hiding' IT throw s the same error for both instance and static methods. It DOES NOT DISTINGUISH between hiding and overring when it throws the error But it does its work fine.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Sun has a Tech Tip on shadowing that also explains it quite well.
http://developer.java.sun.com/developer/TechTips/2000/tt1010.html#tip2


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
shanks iyer
Ranch Hand

Joined: Oct 16, 2000
Posts: 47
Thanks Aruna your explanation was too good it cleared my doubts also about overrididng and shadowing.
Regards,
Shankar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: shadowing, overriding
 
Similar Threads
Static methods and variables
overriding and covarient return types in java
Static and Instance fields
private method...
Shadowing in Java