Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


Can we override static methods?

Answers were Sorted based on User's Feedback



Can we override static methods?..

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

Can we override static methods?..

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

Can we override static methods?..

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

Can we override static methods?..

Answer / devender negi

We can't override the static method we can only redefine
them

Is This Answer Correct ?    1 Yes 2 No

Can we override static methods?..

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

Can we override static methods?..

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

Can we override static methods?..

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

Can we override static methods?..

Answer / karun

yes ,we can override the static methods to a nonstatic or
static methods

Is This Answer Correct ?    4 Yes 52 No

Post New Answer



More Core Java Interview Questions

What does @override mean?

0 Answers  


Relationship between an event-listener interface and an event-adapter class?

1 Answers  


Can a main method be overloaded?

0 Answers  


How to create a thread in java?

0 Answers  


Explain about narrowing conversion in java?

0 Answers  






How do you find the absolute value?

0 Answers  


How u dubugg ur project?

1 Answers   iFlex,


What are other modifiers?

2 Answers   Wipro,


What is ide with example?

0 Answers  


What is variable and constant explain with example?

0 Answers  


What is a jit compiler?

0 Answers  


What is I/O Filter?

2 Answers   TCS, WAX,






Categories