The topic headline and description contradiction.
Title
Question
Madam/Sir,
In the following link how the data hiding concept is possible if the static method can't override, even if they have same signature. Please check the point no. 2 headline and its description in the photo, in the link.
https://drive.google.com/file/d/1yanBnOk0bDBZu-3BlfrpDjC2gryNAqe3/view?usp=drivesdk
Java Static-Methods 00-01 min 0-10 sec
Answers:
Sample Program: Demonstration of different behaviour of static and non static methods.
class Super{
public static void sample(){
System.out.println("Static Method of the Super class");
}
public void disp(){
System.out.println("Non-Static Method of the Super class");
}
}
public class Sub extends Super {
public static void sample(){
System.out.println("Static Method of the Sub class");
}
public void disp(){
System.out.println("Non-Static Method of the Sub class");
}
public static void main(String args[]){
// Sub class instance with reference to Super class
Super obj = new Sub();
//Class Method binding to Super class reference at compile time
obj.sample();
//Instance Method binding to Sub class at run time though referencing to Super class
obj.disp();
//Sub class instance with reference to Sub class
Sub obj1 = new Sub();
//Class Method binding to Sub class reference at compile time (Thus, Hiding the Super class method)
obj1.sample();
//Instance Method binding to Sub class at run time also referencing to Sub class
obj1.disp();
}
}
Result:
Static Method of the Super class
Non-Static Method of the Sub class
Static Method of the Sub class
Non-Static Method of the Sub class
Note: Class methods are early binding (Compile time) methods, while Instance methods are late binding (Run time) methods.
Instance methods and class methods behave differently, thus different terms - "overriding" for instance methods and "hiding" for class methods are used.
Login to add comment