Sunday, September 2, 2012
The answer to this question is NO. You cannot override static methods in Java. In order to show this,have a look at code below :
public class SuperClass{
public static void show(){
System.out.println("Static SuperClass Method");
}
}
public class SubClass extends SuperClass{
public static void show(){
System.out.println("Static SubClass Method");
}
}
public class Demo{
public static void main(String[] args){
SuperClass ob1=new SuperClass();
SuperClass ob2=new SubClass();
SubClass ob3=new SubClass();
ob1.show();
ob2.show();
ob3.show();
}
}
The above code will give output as below :
Static SuperClass Method
Static SuperClass Method
Static SubClass Method
If the static method had been overridden then the second line of the output would have been same as that of third line. So the method has not been overridden and a brand new method has been created in sub class. To understand it more deeply just rewrite the sub class as shown below :
public class SubClass extends SuperClass{
@Override
public static void show(){
System.out.println("Static SubClass Method");
}
}
If you write the latter one you won't be able to compile it. The compiler will tell that there is no such method in super class. So it is recommended that you always use the @Override annotation to verify that whether you have correctly overridden the method or not. So instead of getting an unexpected output you will get a compile time error which is better and easy for debugging. This is possible only with jdk1.5.0 and above.
REASON : The reason why static methods cannot be overridden is that the concept of inheritance is a concept of object. Now static methods and variables are class specific features and not object specific while non-static things are object specific features and for that reason only you can access static things directly using class name. Though static things can be accessed through objects yet they are related to classes and bound with them. Many of you may confuse with this. That's why C# does not allow you to access any static things with objects and thus have removed this confusing feature of Java.
public class SuperClass{
public static void show(){
System.out.println("Static SuperClass Method");
}
}
public class SubClass extends SuperClass{
public static void show(){
System.out.println("Static SubClass Method");
}
}
public class Demo{
public static void main(String[] args){
SuperClass ob1=new SuperClass();
SuperClass ob2=new SubClass();
SubClass ob3=new SubClass();
ob1.show();
ob2.show();
ob3.show();
}
}
The above code will give output as below :
Static SuperClass Method
Static SuperClass Method
Static SubClass Method
If the static method had been overridden then the second line of the output would have been same as that of third line. So the method has not been overridden and a brand new method has been created in sub class. To understand it more deeply just rewrite the sub class as shown below :
public class SubClass extends SuperClass{
@Override
public static void show(){
System.out.println("Static SubClass Method");
}
}
If you write the latter one you won't be able to compile it. The compiler will tell that there is no such method in super class. So it is recommended that you always use the @Override annotation to verify that whether you have correctly overridden the method or not. So instead of getting an unexpected output you will get a compile time error which is better and easy for debugging. This is possible only with jdk1.5.0 and above.
REASON : The reason why static methods cannot be overridden is that the concept of inheritance is a concept of object. Now static methods and variables are class specific features and not object specific while non-static things are object specific features and for that reason only you can access static things directly using class name. Though static things can be accessed through objects yet they are related to classes and bound with them. Many of you may confuse with this. That's why C# does not allow you to access any static things with objects and thus have removed this confusing feature of Java.
Labels:Inheritance,Java FAQs
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment