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 |
How OOPS concept is achieved in Java?
6 Answers Cognizant, JPMorgan Chase, Xavient,
What is a native method in java programming?
Can we declare a static variable inside a method?
Can list be final in java?
What is the main method java?
What is the use of java?
Can we declare the static variables and methods in an abstract class?
Explain 5 io best practices?
Can we declare the main method of our class as private?
What is treemap in java?
What is charat java?
Can I import same package/class twice?