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 is OOPs & Why?
If an application has multiple classes in it, is it okay to have a main method in more than one class?
What is difference between Heap and Stack Memory?
what is the logic inside any default constuctor?
Can we use synchronized block for primitives?
why we write public static void main (String args[]) in core java plz explain briefly??????????????????
Is null a string in java?
Write a java program to check if a number is prime or not?
There are two classes named classa and classb. Both classes are in the same package. Can a private member of classa can be accessed by an object of classb?
What exactly is java?
What is n in java?
Can we modify the throws clause of the superclass method while overriding it in the subclass?