- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why static methods of parent class gets hidden in child class in java?
When we have two classes where, one extends another and if, these two classes have same method including parameters and return type (say, sample) the method in the sub class overrides the method in the super class.
i.e. Since it is inheritance. If we instantiate the subclass a copy of superclass’s members is created in the subclass object and, thus both methods are available to the object of the subclass.
But if you call the method (sample), the sample method of the subclass will be executed overriding the super class’s method.
Example
class Super{ public static void sample(){ System.out.println("Method of the superclass"); } } public class OverridingExample extends Super { public static void sample(){ System.out.println("Method of the subclass"); } public static void main(String args[]){ Super obj1 = (Super) new OverridingExample(); OverridingExample obj2 = new OverridingExample(); obj1.sample(); obj2.sample(); } }
Output
Method of the superclass Method of the subclass
Overriding Static Methods
When superclass and subclass contains same method including parameters and if they are static. The method in the superclass will be hidden by the one that is in the subclass.
This mechanism is known as method hiding in short, though super and subclasses have methods with same signature it they are static, it is not considered as overriding.
Example
class Super{ public static void demo() { System.out.println("This is the main method of the superclass"); } } class Sub extends Super{ public static void demo() { System.out.println("This is the main method of the subclass"); } } public class MethodHiding{ public static void main(String args[]) { MethodHiding obj = new MethodHiding(); Sub.demo(); } }
Output
This is the main method of the subclass
Reason for method hiding
The key in method overloading is, if the super class and sub class have method with same signature, to the object of the sub class both of them are available. Based on the object type (reference) you used to hold the object, the respective method will be executed.
SuperClass obj1 = (Super) new SubClass(); obj1.demo() // invokes the demo method of the super class SubClass obj2 = new SubClass (); obj2.demo() //invokes the demo method of the sub class
But, in case of static methods, since they don’t belong to any instance, you need to access them using the class name.
SuperClass.demo(); SubClass.Demo();
Therefore, if a super class and sub class have static methods with same signature, though a copy of the super class method is available to the sub class object. Since they are static the method calls resolve at the compile time itself, overriding is not possible with static methods.
But, since a copy of static method is available, if you call the sub class method the method of the super class will be redefined/hidden.
- Related Articles
- Are the private variables and private methods of a parent class inherited by the child class in Java?
- Static import the Math Class Methods in Java
- What are Class/Static methods in Java?\n
- How do I create static class data and static class methods in Python?
- Instance child class in abstract static method PHP?
- How to call parent constructor in child class in PHP?
- Static class in Java
- Declare static variables and methods in an abstract class in Java
- What are static methods in a Python class?
- How to access Static variable of Outer class from Static Inner class in java?
- Methods of StringBuffer class in Java.
- Methods of StringBuilder class in Java.
- Methods of StringTokenizer class in Java.
- Use static Import for sqrt() and pow() methods in class Math in Java
- BitSet class methods in Java
