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


can a static method be overridden

Answers were Sorted based on User's Feedback



can a static method be overridden ..

Answer / ramandeep

Yes you can override static methods..
see the following two classes :
public class Super
{
public static void main(String args[])
{

}

public static void m1()
{
System.out.println("superclass");
}
}


public class Sub extends Super
{
public static void main(String args[])
{
Super superWalaObj = new Sub();
superWalaObj.m1();

Sub subWalaObj = new Sub();
subWalaObj.m1();
}

public static void m1()
{
System.out.println("subclass");
}
}

Running this gives the following output :
superclass
subclass

Which explains the behaviour itself.

By overriding the static methods we are forcing them to
behave as non-static methods when used with object.
However, making a call directly to the staticy. method will
call the method belonging to that class only

Is This Answer Correct ?    7 Yes 1 No

can a static method be overridden ..

Answer / narasimha rao bodagala

no, static methods are not overriden cause the compiler
will load the class is loded by the jvm so the static we
can't overriden.

Is This Answer Correct ?    1 Yes 0 No

can a static method be overridden ..

Answer / vivek srivastav

yes static method can
bt only as a static method

Is This Answer Correct ?    1 Yes 0 No

can a static method be overridden ..

Answer / sushant

@ guys above.... if you don't know the exact vcorrect
answers don't put your guesses here so as to confuse people
further.

static method can NEVER be overridden.

Static method in a way are hidden and are called on the
Class itself not on the instance.You can do it but it
implicitly calls the static methods on the class.

Overriding doesn't just mean having the same method name in
the subclass as it is in the superclass. You can still have
the same method name in the subclass but it is not
overriding the superclass method and it is not polymorphism
as well. For more info please google on this.

static method can NEVER be overridden.

-Sushant

Is This Answer Correct ?    2 Yes 1 No

can a static method be overridden ..

Answer / nilesh more

Finally, remember that static methods can't be overridden!
This doesn't mean they can't be redefined in a subclass, but
redefining and overriding aren't the same thing.
Let's take a look at an example of a redefined (remember,
not overridden), staticmethod:

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







The above written material is from kathe Siera which clearly
says that "Static methods can't be overridden."
But you can only redefine them which is not as overriding.
For any further perfect explanation please refer kathe Ciera
book

Is This Answer Correct ?    1 Yes 0 No

can a static method be overridden ..

Answer / ravi

any one reply the answer with ex

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / prabhu

static method can be overriden...absolutely its
possible...try it guys

Is This Answer Correct ?    1 Yes 1 No

can a static method be overridden ..

Answer / prathyusha

Static Method methods are class methods (doesnot belong to
particular Object) so the concept of runtime polymorphism
doesn't arise here..

Staic void dostuff() method is actually redefined in
subclass but not overridden..

Hence Static methods can NEVER be overridden

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / vinay

Making things more clear, how static and non-static method
behaves
public class Super
{

public static void m1()
{
System.out.println("superclass static ");
}
public void m2()
{
System.out.println("superclass nonstatic ");
}
}

public class Sub extends Super
{
public static void main(String args[])
{
Super superWalaObj = new Sub();
superWalaObj.m1();
superWalaObj.m2();

Sub subWalaObj = new Sub();
subWalaObj.m1();
subWalaObj.m2();
}

public static void m1()
{
System.out.println("subclass static ");
}
public void m2()
{
System.out.println("subclass nonstatic ");
}
}


Result:
superclass static
subclass nonstatic
subclass static
subclass nonstatic

Note: The first output is not "subclass static" as with non
static methods.

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / narasimha rao bodagala

Example:
class A {
static void something () {}
}

class B extends A {
static void something () {}
}

....
A anA = new B ();
anA.something ();

so, the compiler will not check the which one is to create
or which one is called method.

Is This Answer Correct ?    0 Yes 1 No

Post New Answer



More Advanced Java Interview Questions

Is “abc” a primitive value?

0 Answers  


What is a task?s priority?

1 Answers  


What is the purpose of the wait() method?

0 Answers  


what is the Scope of Final Keyword in Java?

1 Answers   HCL,


What are transaction attributes?

0 Answers  






What is difference RMI registry and OSAgent?

1 Answers  


Is the session factory thread safe?

0 Answers  


Name three component subclasses that support painting?

0 Answers  


AS a developer will u create a data source in connection pool? If so how will u do that, how to access the object from connection pool using IRAD tool?

0 Answers  


which is the better collection class if the requirement is only displaying the data

1 Answers  


In howmany ways a thread can be created?

3 Answers   HCL,


What is Stream and Types?

2 Answers   Patni,






Categories