Can we Override static methods in java?
We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’.
If a derived class defines a static method with same signature as a static method in base class, the method in the derived class hides the method in the base class.
An instance method cannot override a static method, and a static method cannot hide an instance method. For example, the following program has two compiler errors.
/* Java program to show that if static methods are redefined by
a derived class, then it is not overriding but hidding. */
// Superclass
class Base {
// Static method in base class which will be hidden in subclass
public static void display() {
System.out.println("Static or class method from Base");
}
// Non-static method which will be overridden in derived class
public void print() {
System.out.println("Non-static or Instance method from Base");
}
}
// Subclass
class Derived extends Base {
// Static is removed here (Causes Compiler Error)
public void display() {
System.out.println("Non-static method from Derived");
}
// Static is added here (Causes Compiler Error)
public static void print() {
System.out.println("Static method from Derived");
}
}
1) For class (or static) methods, the method according to the type of reference is called, not according to the object being referred, which means method call is decided at compile time.
2) For instance (or non-static) methods, the method is called according to the type of object being referred, not according to the type of reference, which means method calls is decided at run time.
Overriding method resolution will take care by JVM based on runtime Object. Hence overriding is
considered as runtime polymorphism or dynamic polymorphism or latebinding.
The process of overriding method resolution is also known as “dynamic method dispatch”.
native methods can be overridden as non-native, similarly we can override abstract methods,
synchronized methods.
We can override a non-abstract method as abstract also
Overriding in static methods
A static method can’t be overridden as non-static
Similarly a non-static method can’t be overridden as static method
If both parent and child class methods are static then there is no compile time error or run time error
it seems that overriding is happened but it is not overriding this concept is called “method hiding”.
All the rules of method hiding are exactly similar to overriding, except both methods declared as
static.
In the case of method hiding method resolution will take care by compiler based on reference
type(But not runtime object).
public static Object m1() throws Exception {
System.out.println("PArent");
return -1;
}
}
class C extends P {
public static Object m1() throws Exception {
System.out.println("Child");
return null;
}
}
public class test {
public static void main(String args[]) throws Exception {
P p = new C();
p.m1();
}
}
O/P:
Parent
In the case of method hiding the method resolution will take care by compiler based on reference
type. Hence method hiding is considered as ‘static polymorphism’ or ‘compile time polymorphism’ or
‘early binding’.
Rules :
1. Visibility or access Modified can only increase i e if parent is public child can only be public.
2. Return Types in Child class should be co-variant types ie if parent is Object then child can be String or integer etc but the vice versa is not true.
3. while overriding the size of checked exceptions we are not allowed to increase in throws class but
we can decrease the size of checked exceptions. But there are no restrictions on unchecked
exceptions.
Overriding in the case of Variable
Overriding concept is not applicable for variables. And it is applicable only for methods.
Variable resolution always takes care by compiler based on reference type.
Ex:
class P
{
int i = 888;
static
non-static
}
class C extends P
{
int i = 999;
}
class Test
{
public static void main(String arg[])
{
P p = new P();
Case1: System.out.println(p.i); //888
C c = new C();
Case2: System.out.println(c.i); //999
P p1 = new C();
Case3: System.out.println(p1.i); //888
}
}
1. Conclusion for Handling such Exceptions: Hence, following conclusions can be derived from the above examples:
If SuperClass does not declare an exception, then the SubClass can only declare unchecked exceptions, but not the checked exceptions.
If SuperClass declares an exception, then the SubClass can only declare the child exceptions of the exception declared by the SuperClass, but not any other exception.
If SuperClass declares an exception, then the SubClass can declare without exception.
Consider the following parent class method declaration….!
Parent : public void m1(int i)
Which of the following methods are allowed in the child class.
Child : 1) private void m1(float f)throws Exception (overloading)
2) public void m1(int i)throws classCastException (overloading)
3) public final void m1(int i) (overloading)
4) private abstract void m1(long l)throws Exception X (private & abstract)
5) public synchronized int m1() throws IOException (over loading)
6) public int m1(int i) X
7) public void m1(int i)throws Exception X (Exception Increased)
We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’.
If a derived class defines a static method with same signature as a static method in base class, the method in the derived class hides the method in the base class.
An instance method cannot override a static method, and a static method cannot hide an instance method. For example, the following program has two compiler errors.
/* Java program to show that if static methods are redefined by
a derived class, then it is not overriding but hidding. */
// Superclass
class Base {
// Static method in base class which will be hidden in subclass
public static void display() {
System.out.println("Static or class method from Base");
}
// Non-static method which will be overridden in derived class
public void print() {
System.out.println("Non-static or Instance method from Base");
}
}
// Subclass
class Derived extends Base {
// Static is removed here (Causes Compiler Error)
public void display() {
System.out.println("Non-static method from Derived");
}
// Static is added here (Causes Compiler Error)
public static void print() {
System.out.println("Static method from Derived");
}
}
1) For class (or static) methods, the method according to the type of reference is called, not according to the object being referred, which means method call is decided at compile time.
2) For instance (or non-static) methods, the method is called according to the type of object being referred, not according to the type of reference, which means method calls is decided at run time.
Overriding method resolution will take care by JVM based on runtime Object. Hence overriding is
considered as runtime polymorphism or dynamic polymorphism or latebinding.
The process of overriding method resolution is also known as “dynamic method dispatch”.
native methods can be overridden as non-native, similarly we can override abstract methods,
synchronized methods.
We can override a non-abstract method as abstract also
Overriding in static methods
A static method can’t be overridden as non-static
Similarly a non-static method can’t be overridden as static method
If both parent and child class methods are static then there is no compile time error or run time error
it seems that overriding is happened but it is not overriding this concept is called “method hiding”.
All the rules of method hiding are exactly similar to overriding, except both methods declared as
static.
In the case of method hiding method resolution will take care by compiler based on reference
type(But not runtime object).
public static Object m1() throws Exception {
System.out.println("PArent");
return -1;
}
}
class C extends P {
public static Object m1() throws Exception {
System.out.println("Child");
return null;
}
}
public class test {
public static void main(String args[]) throws Exception {
P p = new C();
p.m1();
}
}
O/P:
Parent
In the case of method hiding the method resolution will take care by compiler based on reference
type. Hence method hiding is considered as ‘static polymorphism’ or ‘compile time polymorphism’ or
‘early binding’.
Rules :
1. Visibility or access Modified can only increase i e if parent is public child can only be public.
2. Return Types in Child class should be co-variant types ie if parent is Object then child can be String or integer etc but the vice versa is not true.
3. while overriding the size of checked exceptions we are not allowed to increase in throws class but
we can decrease the size of checked exceptions. But there are no restrictions on unchecked
exceptions.
Overriding in the case of Variable
Overriding concept is not applicable for variables. And it is applicable only for methods.
Variable resolution always takes care by compiler based on reference type.
Ex:
class P
{
int i = 888;
static
non-static
}
class C extends P
{
int i = 999;
}
class Test
{
public static void main(String arg[])
{
P p = new P();
Case1: System.out.println(p.i); //888
C c = new C();
Case2: System.out.println(c.i); //999
P p1 = new C();
Case3: System.out.println(p1.i); //888
}
}
1. Conclusion for Handling such Exceptions: Hence, following conclusions can be derived from the above examples:
If SuperClass does not declare an exception, then the SubClass can only declare unchecked exceptions, but not the checked exceptions.
If SuperClass declares an exception, then the SubClass can only declare the child exceptions of the exception declared by the SuperClass, but not any other exception.
If SuperClass declares an exception, then the SubClass can declare without exception.
Consider the following parent class method declaration….!
Parent : public void m1(int i)
Which of the following methods are allowed in the child class.
Child : 1) private void m1(float f)throws Exception (overloading)
2) public void m1(int i)throws classCastException (overloading)
3) public final void m1(int i) (overloading)
4) private abstract void m1(long l)throws Exception X (private & abstract)
5) public synchronized int m1() throws IOException (over loading)
6) public int m1(int i) X
7) public void m1(int i)throws Exception X (Exception Increased)
No comments:
Post a Comment