jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hiding/Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hiding/Overriding" Watch "hiding/Overriding" New topic
Author

hiding/Overriding

sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
Can anyone explain to me the diff between hiding and overriding of methods/fields with some examples..
-Sanjana
Sripada Phani
Greenhorn

Joined: Nov 28, 2003
Posts: 7
When you extend any base class, all the members of base class will get part of the derived class. Based on their access modifiers these memebers get accessed from the new class.
While defining the derived class if you try to change the behaviour of a particular method, with which the signature is already inherited from base class, we call it as overriding. Similarly, if you define any member variable with the same name in derived class we call it as hiding.
In both the cases we are redefining the inherited members. The only difference between these is when you access them. JVM decides which overridden method to call based on the object referrece that curently pointing to. Incase of member fields it's the Class type of the reference it's pointing. Try to run the below program ...
import java.io.*;
class A
{
String str="This String is defined in A";
public void printMsg()
{
System.out.println("This method is defined in Class A");
}
}
public class B extends A
{
String str="This String is defined in B";
public void printMsg()
{
System.out.println("This method is defined in Class B");
}
public static void main(String[] a)
{
B testB=new B();
A testA=testB;
System.out.println(testB.str);//This prints B String
testB.printMsg(); //This prints B Message
System.out.println(testA.str);//But, this prints A String
testA.printMsg(); //This prints B Message
}
}
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330

While defining the derived class if you try to change the behaviour of a particular method, with which the signature is already inherited from base class, we call it as overriding.

This is not applicable to static methods. Static methods cannot be overridden.
[ December 07, 2003: Message edited by: dennis zined ]

SCJP 1.4<br />SCWCD 1.4
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Suppose class A has a static method show and class B which extends A has redefined the same method then we say that show in B has hidden show in A.


Groovy
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330

Suppose class A has a static method show and class B which extends A has redefined the same method then we say that show in B has hidden show in A.

Exactly. Its important to understand that hiding is different from overridding. Let say for example you assign a reference variable with its subclass type:
<Superclass> A = new <Subclass>;
and a "static" method show() in the Superclass has been hidden in its subclass so that both the Superclass and subclass now has the static method show(). When you access the static method show() via the reference variable: A.show() the question is which show() method is accessed? The answer is the static method show() that is defined in the Superclass. But if the show() method was not declared as static but rather as an instance method, the show() method called is the one that is defined in the subclass.
So Sripada's statement:

JVM decides which overridden method to call based on the object referrece that curently pointing to.

is only applicable to instance methods and not static methods and variables.
sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
thank u guys..

-Sanjana
 
jQuery in Action, 2nd edition
 
subject: hiding/Overriding