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 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 are keyboard events?

0 Answers  


Can a method be overloaded based on different return type but same argument type?

0 Answers  


What is the difference between interface & abstract class?

0 Answers  


What is the difference between procedural and object-oriented programs?

0 Answers  


What is java reflection api?

0 Answers  


What is integer size in java?

0 Answers  


What are possible key words, we can use to declare a class?

2 Answers   Tech Mahindra,


What are the restriction imposed on a static method or a static block of code?

0 Answers  


5 Coding best practices you learned in java?

0 Answers  


Is string a class in java?

0 Answers  


How do you load an HTML page from an Applet ?

2 Answers  


Write java program to reverse string without using api?

0 Answers  


Categories