Can we override static methods?
Answers were Sorted based on User's Feedback
Answer / amitasite
you cannot override static method from super class.
e.g.,
class SuperType{
public static void method(){
System.out.println("Super Static");
}
}
class SubType extends SuperType{
public void method(){
System.out.println("Super Static");
}
}
It will give compilation error : Instance method cannot
override static method from SuperType
| Is This Answer Correct ? | 3 Yes | 1 No |
Answer / shailesh
Hi, we can overried the static method as well as we can
overload them. The exam which you have given is wrongly
interpreted. Never try to access the static method with the
instance variable, it can create confusion.
In the Foo, Bar example if you do like this
Foo f = new Bar();
and call f.(some staic method). It will always call the
static method of Foo (but not of Bar). Check the java docs.
So just modify the code like this
class Foo {
public static void classMethod() {
System.out.println("classMethod() in Foo");
}
public void instanceMethod() {
System.out.println("instanceMethod() in Foo");
}
}
class Bar extends Foo {
public static void classMethod() {
System.out.println("classMethod() in Bar");
}
public void instanceMethod() {
System.out.println("instanceMethod() in Bar");
}
}
class Test {
public static void main(String[] args) {
class Foo {
public static void classMethod() {
System.out.println("classMethod() in Foo");
}
public void instanceMethod() {
System.out.println("instanceMethod() in Foo");
}
}
class Bar extends Foo {
public static void classMethod() {
System.out.println("classMethod() in Bar");
}
public void instanceMethod() {
System.out.println("instanceMethod() in Bar");
}
}
class Test {
public static void main(String[] args) {
Foo f = new Foo();
f.instanceMethod();
Foo.classMethod();
Foo b = new Bar();
b.instanceMethod();
Bar.classMethod();
}
}
If you run this, the output is
instanceMethod() in Foo
classMethod() in Foo
instanceMethod() in Bar
classMethod() in Bar
| Is This Answer Correct ? | 3 Yes | 2 No |
Answer / manikandan [ gtec,vellore ]
Dear yogesh, overriding is not a compile time polymorphism
so u have to run the code.
static methods can't override
pls run below example
class test extends a
{
public static void main(String[]asd)
{
a as=new test();
as.a();//it'll not invoke a() from class test
}
static void a()
{
System.out.println("test");
}
}
class a
{
static void a()
{
System.out.println("a");
}
}
out put: a
as.a(); this line'll not invoke the method a()from class
test instead it'll invoke a a()method from class a so there
is no overriding.
| Is This Answer Correct ? | 2 Yes | 1 No |
Answer / devender negi
We can't override the static method we can only redefine
them
| Is This Answer Correct ? | 1 Yes | 2 No |
Answer / rambabu
Yes, We can! see Below---
public class Final1 {
public static void mone() {
System.out.println("Iam in final method of
super class");
}
}
public class Final extends Final1{
public static void mone() {
System.out.println("Iam in final method of
sub class");
}
public static void main(String a[]) {
Final f = new Final();
f.mone();
}
}
| Is This Answer Correct ? | 0 Yes | 8 No |
Answer / neha jain
yes we can override static method but can not overload that
is called method hiding nt overloading.
| Is This Answer Correct ? | 3 Yes | 17 No |
Answer / p.sreekiran
no we cannot overriding the static methods .
if we override the static method it will gives error
| Is This Answer Correct ? | 25 Yes | 54 No |
Answer / karun
yes ,we can override the static methods to a nonstatic or
static methods
| Is This Answer Correct ? | 4 Yes | 52 No |
What are keyboard events?
Can a method be overloaded based on different return type but same argument type?
What is the difference between interface & abstract class?
What is the difference between procedural and object-oriented programs?
What is java reflection api?
What is integer size in java?
What are possible key words, we can use to declare a class?
What are the restriction imposed on a static method or a static block of code?
5 Coding best practices you learned in java?
Is string a class in java?
How do you load an HTML page from an Applet ?
Write java program to reverse string without using api?