Reputation: 1385
i have following code in which the base class Employee have a static method meth1() which i am able to call from a child class (Pro) object . Is it a case of method hiding or what ? , i am not sure because i haven't implemented the meth1() method in Pro class, but still able to call Emplyee static method from Pro object.
class Employee
{
String s;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
protected static void meth1()
{
System.out.println("inside emp-meth1");
}
}
public class Pro extends Employee {
/*
* public void meth1()
{
System.out.println("inside encapsulation-meth1");
}
*/
public static void main(String as[])
{
Pro e = new Pro();
// e.s ="jay";
e.meth1();
}
}
Output:
inside emp-meth1
Thanks
Jayendra
Upvotes: 0
Views: 64
Reputation: 146
What are you trying to hide? Try the below code
emp.meth1()
will call method based on reference not based on the object being referred.
class Employee
{
String s;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
protected static void meth1()
{
System.out.println("inside emp-meth1");
}
}
public class Pro extends Employee {
protected static void meth1()
{
System.out.println("inside encapsulation-meth1");
}
public static void main(String as[])
{
Pro e = new Pro();
Employee emp = new Pro();
emp.meth1(); //this is case of method hiding
e.meth1();
}
}
Upvotes: 1