wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Hiding 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 "Hiding" Watch "Hiding" New topic
Author

Hiding

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What does hiding a variable or method means?how does it affect the concept of inheritance and overriding?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi ... Think I know what hiding a variable means. (Hopefully a Java wise one will point out any mistakes in my explanation )
A variable can be hidden by a parameter. For example:
<pre>
public class TestHideVar {
int i = 5; // member variable
TestHideVar() {
System.out.println("Value of i = " + i);
}
TestHideVar(int i) { // parameter hides original i
System.out.println("Value of i = " + i);
}
public static void main(String args[]){
new TestHideVar();
new TestHideVar(10);
}
}
</pre>
Output from the above is:
<pre>
Value of i = 5
Value of i = 10
</pre>
The constructor parameter in TestHideVar(int i) hides the member variable i.
If you change the constructor to:
<pre>
TestHideVar(int y) {
System.out.println("Value of i = " + i );
System.out.println("Value of y = " + y );
}
</pre>
The output changes to:
<pre>
Value of i = 5;
Value of i = 5;
Value of y = 10;
</pre>
The original i is not hidden by the parameter y
I believe a method is hidden by creating a method with the same signature in a subclass.
<pre>
public class TestHideMethod {
static class One {
void display() {
System.out.println("This is class One");
}
}
static class Two extends One {
void display() {
System.out.println("This is class Two");
}
}
static class Three extends One {
}

public static void main(String args[]){
One a = new One();
Two b = new Two();
Three c = new Three();
a.display();
b.display();
c.display();
}
}
</pre>
The output from the above is:
<pre>
This is class One
This is class Two
This is class One
</pre>
The output from c.display() is 'This is class One' as class Three does not override the display() method in class One.
The output from b.display() overrides the class One display() method. ie it hides the original method.
Hope that's of some help.

[This message has been edited by Jane Griscti (edited September 03, 2000).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hiding