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 Beginning Java and the fly likes explain the concept of data hiding 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 » Java » Beginning Java
Bookmark "explain the concept of data hiding" Watch "explain the concept of data hiding" New topic
Author

explain the concept of data hiding

itsHarsh sharma
Greenhorn

Joined: Feb 24, 2012
Posts: 2


class Parent
{
int x = 50;
void display()
{
System.out.println(x);
}

}

class Child extends Parent
{
int x = 100;

public static void main(String a[])
{
Child child = new Child();
child.display();
}

}
The output of this is 50 but not 100 why?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Because its Parent's display() method that gets called, and it refers to variable x. Member variables are not runtime polymorphic. In other words, it is determined at compile time, by the type of the reference, which class's variable will be used.
Rod Singh
Greenhorn

Joined: Feb 10, 2012
Posts: 16
The output of the program is correct. If you move the display() into Child's class, the output will be 100. The reason is when the Child's class instance is created it inherits both x-- from the Child Class and from the Parent class. That being said, since display() method is in the Parent class, it will access the x-- the one which is present in the Parent class--not from the Child's class.

With regards to data hiding(encapsulation), the concept is related to access modifiers--public, protected, default, private. In other words, if you assign a "private" access modifier to "int x", then the 'x' can not be accessed from any other class. Here one point is notable: when a class inherits any other class, the inherited class gets all the behaviors and properties of its parent class.
itsHarsh sharma
Greenhorn

Joined: Feb 24, 2012
Posts: 2
but when we compile it and again decompile using any decompiler we see that in the display function the variable is printed using "this.x". So when the display function is called on child class object it must pass the reference of child class to the function display which is received in "this" variable. then it must have printed the x defined in child class. but it does not. why?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

itsHarsh sharma wrote:but when we compile it and again decompile using any decompiler we see that in the display function the variable is printed using "this.x".


Yes, and that's resolved at compile time by the reference type of "this", which is the parent class.

If it was a method call, "this.x()", and if x() was non-static, non-private, and non-final, then, and only then would there be a runtime check to determine which class's version of x() to use.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

itsHarsh sharma wrote:it must pass the reference of child class to the function display which is received in "this" variable. then it must have printed the x defined in child class. but it does not. why?


Stating that it "must" do something that it clearly does not do is not a productive path to understanding. Since you can see that it clearly doesn't behave as you thought it did, you need to re-examine your assumptions to find out where they went wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: explain the concept of data hiding
 
Similar Threads
Confusion on access modifiers
can someone explain me this?
Child extends Parent, Belongs to same package, Cannot access public variable
an inheritance question
constructor