Dear Friends,
I am back with some Java Tricky Topics to explain here. I would like to start with Data Hiding Concept in Java.
Please do comment if you have any questions or if any wrong data presented in this post. It will help the users to get to know the correct information.
Its a straight forward topic - In general whenever you are overriding a method (sply non - static) then if you create an object of Sub class with Parent Class / Super Class object reference then it will call the Sub class method only.
But in case of Static method overriding ( in general we cannot override static method! but here I am trying to explain what is that means ) this will not be working similar to Overriding behavior. So this is called Data hiding in Java.
An example for demonstration the difference between static / non static overriding and Data Hiding.
Example 1: With Non Static Overriding.
// do necessary imports
class SuperClass {
public void oneMethod() {
System.out.println("In Super Method");
}
}
public class SubClass extends SuperClass{
public void oneMethod() { // Method Overriding.
System.out.println("In Sub Method");
}
public static void main(String []s) {
SuperClass superClass = new SubClass(); // Creating Superclass Obj reference with Sub class //Obj creation
superClass.oneMethod();
}
}
// Output:
In Sub Method
____
The same will be different in case of those methods are Static - i.e. You cannot achieve the purpose of Override - hence it is called as Data hiding.
Example 2: With Static Overriding.
// do necessary imports
class SuperClass {
public static void oneMethod() {
System.out.println("In Super Method");
}
}
public class SubClass extends SuperClass{
public static void oneMethod() { // Method Overriding can't be done.
System.out.println("In Sub Method");
}
public static void main(String []s) {
SuperClass superClass = new SubClass(); // Creating Superclass Obj reference with Sub class //Obj creation
superClass.oneMethod();
}
}
// Output:
In Super Method
I am back with some Java Tricky Topics to explain here. I would like to start with Data Hiding Concept in Java.
Please do comment if you have any questions or if any wrong data presented in this post. It will help the users to get to know the correct information.
Data Hiding
Its a straight forward topic - In general whenever you are overriding a method (sply non - static) then if you create an object of Sub class with Parent Class / Super Class object reference then it will call the Sub class method only.
But in case of Static method overriding ( in general we cannot override static method! but here I am trying to explain what is that means ) this will not be working similar to Overriding behavior. So this is called Data hiding in Java.
An example for demonstration the difference between static / non static overriding and Data Hiding.
Example 1: With Non Static Overriding.
// do necessary imports
class SuperClass {
public void oneMethod() {
System.out.println("In Super Method");
}
}
public class SubClass extends SuperClass{
public void oneMethod() { // Method Overriding.
System.out.println("In Sub Method");
}
public static void main(String []s) {
SuperClass superClass = new SubClass(); // Creating Superclass Obj reference with Sub class //Obj creation
superClass.oneMethod();
}
}
// Output:
In Sub Method
____
The same will be different in case of those methods are Static - i.e. You cannot achieve the purpose of Override - hence it is called as Data hiding.
Example 2: With Static Overriding.
// do necessary imports
class SuperClass {
public static void oneMethod() {
System.out.println("In Super Method");
}
}
public class SubClass extends SuperClass{
public static void oneMethod() { // Method Overriding can't be done.
System.out.println("In Sub Method");
}
public static void main(String []s) {
SuperClass superClass = new SubClass(); // Creating Superclass Obj reference with Sub class //Obj creation
superClass.oneMethod();
}
}
// Output:
In Super Method
If you like this post - please do share with your friends.
Because in Knowledge
Because in Knowledge
Sharing is Gaining
0 comments:
Post a Comment