Hi friends
I want to know what is difference between Data Hiding and Data shadowing in java. please explain it with example.
Thank you.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Apr 9, 2012, 10:32 PM
Data Hiding
Data Hiding is a concept in Java. We got the luxary of hiding data using e different access levels - private, protected, public using Java Programming Language.
Private members and functions can only be accessed by class members of the class. You can think the data abstraction is also using data hiding techniques. Protected members can be accessed only by the class and its derived classes.
In this sample code, the class CStudent members variables such as name, age, addr1, etc can not be accessed directly. You need to go through the interfaces functions to access these members.
Source Code
import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.*;
class CStudent
{
private String name;
private int age;
private String addr1;
private String addr2;
private String city;
private String zipcode;
public CStudent() { }
public String GetName() { return name; }
public String GetAddr1() { return addr1; }
public String GetAddr2() { return addr2; }
public String GetCity() { return city; }
public String GetZipCode() { return zipcode; }
public int GetAge() { return age; }
public void SetName(String s) { name = s; }
public void SetAddr1(String s) { addr1 = s; }
public void SetAddr2(String s) { addr2 = s; }
public void SetCity(String s) { city = s; }
public void SetZipCode(String s) { zipcode = s; }
public void GetAge(int v) { age = v; }
};
class DataHiding
{
static public void main(String[] args)
{
CStudent s = new CStudent();
// s.name = "softwareandfinance.com"; // Not allowed, compiler error name has private access in CStudent
// System.out.print(s.name);
s.SetName("softwareandfinance.com");
System.out.print(s.GetName());
}
}
What is variable hiding and shadowing?
In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope , the one in the outer scope is shadowed.
A Local Variable Shadows An Instance Variable
Inside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.
In the following example, there is a instance variable named "a", and inside the method setA() there is a local variable "a":
class Program {
int a;
int b;
void setA(int a) {
a = a; //here, the local variable has closer scope than the instance
// variable, so the expression set parameter equal to itself
this.a = a; // this is the correct way to set the parameter to the
//instance variable.
}
void setB(int b) {
this.b = b;
}
}
Let's look at another example,
class Program {
String name = "Instance Var.";
void someMethod() {
String name ="Local Var.";
System.out.println(name); //Local Var.
System.out.println(this.name); //Instance Var.
}
}
Note: Instance variables are not overridden, they are hidden.
A Class Variable Shadows the Inherited Variable from Its Parent Classes
When an instance variable in a subclass has the same name as an instance variable in a super class, then the instance variable is chosen in the class that is the reference type.
A class can declare a variable with the same name as an inherited variable from its parent class, thus "hiding" or shadowing the inherited version. (This is like overriding, but for variables.) For example,
public class Base {
public String name = "Base";
public String getName() { return name; }
}
public class Sub extends Base {
public String name = "Sub";
public String getName() { return name; }
}
The above shows two classes and Sub class is a subclass of Base class. Both of classes has a String type variable named name.
*
Use Case I
class Program {
public static void main(String[] args) {
Sub s = new Sub();
System.out.println(s.name); //Output "Sub"
}
}
The above code directly accesses the name variable in the instance of Sub named s. This access, as one would expect, yields a value of "Sub".
*
Use Case II
class Program {
public static void main(String[] args) {
Sub s = new Sub();
Base b = s;
System.out.println(s.name); //Output "Sub"
System.out.println(b.name); //Output "Base"
}
}
In use case II, even though the variable b refers an instance of class Sub, the b.name still evaluates to "Base". Because variables names in Java are resolved by the reference type, not the object they are referencing.
In this example, even though both b and s are referencing the object of type Sub, s is declared as type Sub, b is declared as type Base, Java runtime gets the s.name from the class of Sub, and b.name from the class of Base.
Access Supper Class Variables
When shadowing does happen, you can access the super class name by either the syntax super.name or by casting the object to its super class, with the syntax ((Superclass)object).name.
Variables shadowed by Interface hierarchies are referenced the same way they are for class hierarchies. The following example shows the shadowing from interface hierarchies:
interface IBase {
String name = "base";
}
interface ISub extends IBase {
String name = "sub";
}
public class Shadow implements ISub {
String name = "shadow";
void print(Strig name)
{
System.out.println("method name = " + name);
System.out.println("class name = " + this.name);
System.out.println("IBase name = " +
((IBase) this).name);
System.out.println("ISub name = " +
((ISub) this).name);
}
public static void main(String[] args) {
(new Shadow()).print("parameter");
}
}
The output of this program is:
method name = parameter
name = shadow
IBase name = base
ISub name = sub
As you can see, it can be confusing when variable shadowing occurs. It's a good practice not to use the same variable names in the class or interface hierarchies.
There are exceptions. When the variable in the supper class has private access or is in another package and has default access, there is no room for confusion. The sub class cannot access such variables in its supper class.
Difference between hiding and shadowing in java
Hiding : Happens during inheritance (between superclass and subclass).
Shadowing : Happens within a class (between a member variable and local variable).
Examples :
Hiding :
class Parent {
String city = "Dallas";
}
class Child extends Parent {
String city ="Zurich";
}
"city" in Child hides the one in Parent.
Shadowing :
class Main {
String city = "Dallas";
void method() {
String city = "Zurich";
}
}
"city", local variable shadows the member variable.
Please refer the below link
http://www.xyzws.com/Javafaq/what-is-variable-hiding-and-shadowing/15
Thanks