can a static method be overridden

Answers were Sorted based on User's Feedback



can a static method be overridden ..

Answer / ya7ia

canont override static method but
can overload static method

Is This Answer Correct ?    1 Yes 0 No

can a static method be overridden ..

Answer / vivek

If a subclass defines a static method with the same signature as a static method in the superclass, the method in the subclass hides the one in the superclass. The distinction between hiding and overriding has important implications.

public class Animal
{
public static void hide()
{
System.out.format("The hide method in Animal.");
}
public void override()
{
System.out.format("The override method in Animal.");
}
}

public class Cat extends Animal
{
public static void hide()
{
System.out.format("The hide method in Cat.");
}
public void override()
{
System.out.format("The override method in Cat.");
}
}

But still I am not convinced with the exact difference in between hiding and overriding a method...???

Is This Answer Correct ?    1 Yes 0 No

can a static method be overridden ..

Answer / koushik1121

class Animal
{
static void meth1()
{
System.out.println("THIS IS A METHOD");
}
}
class Dog extends Animal
{
static void meth1()
{
System.out.println("THIS IS An overriding METHOD");
}
}
public class Test2 extends Dog
{

public static void main(String args[])
{

((Animal)new Dog()).meth1();
}

}

if static method can be overriden output of the above
program should be
THIS IS An overriding METHOD
because overriden depends on object type not reference type

but real output is
THIS IS A METHOD
depending upon the reference Animal for Dog object.

Is This Answer Correct ?    1 Yes 0 No

can a static method be overridden ..

Answer / kiran kadarla

Static methods CANNOT be overridden as they belong to a
class and not an instance of the class.

Aswini De: First of all, you didnot override the cal method.

Karteek: In your example, the fact that
parent.mystaticMethod and child.mystaticmethod are printing
different answers is in itslef an indication that the method
is not overridden!!

-kk

Is This Answer Correct ?    9 Yes 9 No

can a static method be overridden ..

Answer / mayank

only static method in a subclass can over ride the static
method in the parent class.

for example

public class A {
public static int display(){
System.out.println("i an in A");
return 1;
}

}


public class B extends A{

public static int display(){

System.out.println("i an in B");
return 1;
}


/*public int display(){

}*/
}

will work fine. but if the subclass tries to override parent
class static method with a non static method it generates
compilation error.

for eg

public class B extends A{

/* public static int display(){

System.out.println("i an in B");
return 1;
}*/


public int display(){

}
}
the above code will result in compilation error.

Is This Answer Correct ?    5 Yes 5 No

can a static method be overridden ..

Answer / konthoujan dhanabir singh

static method cannot be overriden to non-static.so static
method can be overriden to static.
the above example is true in static way
e.g.

class Animal {
static void doStuff() {
System.out.print("a ");
}
}

class Dog extends Animal {
static void dostuff() { // it's a redefinition,
// not an override
System.out.print("d ");
}

public static void main(String [] args) {
Animal [] a = {new Animal(), new Dog(), new Animal()};
for(int x = 0; x < a.length; x++)
a[x].doStuff(); // invoke the static method
}
}

Running this code produces the output:
a a a
Some case:
in the subclass Dog, if the method dostuff() is not
static,it will be compile time error in the above code
block .

Is This Answer Correct ?    5 Yes 5 No

can a static method be overridden ..

Answer / tarun

static method cant be overridden but can be redefine
confuse

Is This Answer Correct ?    3 Yes 3 No

can a static method be overridden ..

Answer / shiv patil

Please refer following link. And please use javaranch for
all you technical calrifications. As this is the correct
place to learn. ( am no way related to java ranch, I
learned from this place hence suggesting)

http://faq.javaranch.com/view?OverridingVsHiding

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / kapil dave

yes static methods can be overwritten.but it in fact hides the 1 in d superclass with the subclass 1.so the calling depends on d name of class frm which it is called.

there is a link to suns site where dis is described in detial..

http://java.sun.com/docs/books/tutorial/java/IandI/override.html

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / dheerendra

static method can be overriden the case is that in in
subclass that method should also be static.

class A {
static void something () { System.out.println("static method
in A");}
}

class B extends A {
static void something () {System.out.println("static method
in B");}
}

class TestClass{

public static void main(String args[]){

A a = new A();
B b = new B();
A.something();
B.something();

A t = new B();
t.something();
}
}

the output is
static method in A
static method in B
static method in A

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Advanced Java Interview Questions

Java is fully object oriented languages or not?

0 Answers  


Hi I have joined java course. I also want additional help from any tutorials website. Please suggest me tutorials which provides easy to understand online applet tutorials?

0 Answers  


What are local interfaces? Describe.

0 Answers  


What is resource bundle?

1 Answers  


difference between HashMap, Hashset and hashTable?

2 Answers   HCL, Infotech,






What is the purpose of the notifyall() method?

0 Answers  


Which are the different segments of memory?

0 Answers  


How are the elements of a borderlayout organized?

0 Answers  


What is multithread synchronizing ?

4 Answers   Sun Microsystems,


If i learn Java, what kind of applications can i create that will help Banking, Retail, Hotel, Logistics industry.

2 Answers  


can we write implementation for a method with in another method?

3 Answers   Cap Gemini,


What is the map interface?

0 Answers  


Categories