Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

What class is used to create Server side object ?

0 Answers  


how to use debug in my elipse to solve problems that exist in my project

0 Answers   SAP Labs, TCS,


Difference between JRE and JVM?

3 Answers   HeadStrong, Infotech,


For which statements does it make sense to use a label?

0 Answers  


Why won’t the jvm terminate when I close all the application windows?

0 Answers  


What is difference RMI registry and OSAgent?

1 Answers  


Why does most servlets extend HttpServlet?

4 Answers   Accenture, Wipro,


How to determine applet?s height and width?

1 Answers  


What is MOM?

1 Answers   Wipro,


what is stringBuffer and StringBuilder?

3 Answers  


What is re-entrant. Is session beans reentrant. Is entity beans reentrant?

0 Answers  


what is handle?

0 Answers  


Categories