- Exact answer is NO. We can not override static methods.
- Before discussing this topic lets see what is static in java.
Static methods in java:
- Static means class level if we declare any data or method as static then those data(variables) and methods belongs to that class at class level.
- Static variables and static methods belongs to class level.
- Static methods are not the part of objects state . Belongs to class
- Without using object we can call these static methods.
Instance methods in java:
- Instance methods will be called at run time.
- Instance variables and instance methods are object level. variables values may change from one object to another where as static variable values are class level so if we access by using any object and changes that values will effect to all objects means its class level variable.
- Lets see about overriding in java.
Method overriding in java:
- Defining the super class method in sub class with same signature.
- Even though in inheritance all properties of supers class can access in sub class we have an option of overriding super class methods in sub class known as method overriding.
- So by this every-time sub-most object method will be called.
Instance methods - Method Overriding
- class SuperClassDemo{
- public void instanceMethod(){
- System.out.println("SuperClassDemo instanceMethodcalled");
- }
- }
- class SubClassDemo extends SuperClassDemo{
- public void instanceMethod(){
- System.out.println("SubClassDemo instanceMethod called");
- }
- public static void main(String []args){
- SuperClassDemo superObj= new SuperClassDemo();
- SuperClassDemo superobj1= new SubClassDem();
- SubClassDemo subObj= new SubClassDem();
- // here no need to create object to call a static method please note that.
- superObj.instanceMethod();
- superObj1.instanceMethod();
- subObj.instanceMethod();
- }
- }
Output
- SuperClassDemo instanceMethodcalled
- SubClassDemo instanceMethodcalled
- SubClassDemo instanceMethodcalled
Static methods - method overriding:
- Lest an example program on static methods in method overriding concept and then discus about this clearly.
- class SuperClassDemo{
- public static void staticMethod(){
- System.out.println("SuperClassDemo staticMethod called");
- }
- }
- class SubClassDemo extends SuperClassDemo{
- public static void staticMethod(){
- System.out.println("SubClassDemo staticMethod called");
- }
- public static void main(String []args){
- SuperClassDemo superObj= new SuperClassDemo();
- SuperClassDemo superobj1= new SubClassDem();
- SubClassDemo subObj= new SubClassDem();
- // here no need to create object to call a static method please note that.
- superObj.staticMethod();
- superObj1.staticMethod();
- subObj.staticMethod();
- }
- }
Output
- SuperClassDemo staticMethod called
- SuperClassDemo staticMethod called
- SubClassDemo staticMethod called
Method Overriding - Method Hiding:
- Method overriding : in method overriding we will define super class method in sub class with same signature and these methods will be called at run time based on the object.
- Method Hiding: In method hiding even though super class methods are accesible in sub class they are not the part of sub class so the methods and these methods will be called based on the class.
- In method Hiding if we define same static method in super class and sub class they are not same they are unique and distinct from the other.
No comments