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?

Answer Posted / sadheez

It may seems to be overriding the static methods, but the
real fact is HIDING.

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 StaticHiding {
public static void main(String[] args) {
Foo f = new Bar();

f.instanceMethod();
f.classMethod();


when u run this program output will be:

instanceMethod() in Bar
classMethod() in Foo.

Here if u say it to be overriding then the
subclass ie., Bar class having static classMethod() should
be executed. But the fact here is Foo class static
classMethod() is executed.

So its method HIDING and not method
overriding..

I hope i have given answer to my best if
anyone feels wrong plz do post ur suggestions..

Is This Answer Correct ?    59 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Does java return by reference?

999


What are benefits of java?

1046


What are the differences between processes and threads?

1063


How to Sort Strings which are given in List and display in ascending order without using java api.

4434


What is a generic code?

1056


What is a method in coding?

1051


Can we write multiple catch blocks under single try block?

1086


How do you get length in java?

1025


What are the two ways to create a thread?

1006


Can static method access instance variables ?

1136


What is string literal in java?

1051


I am a fresher and know core java, c languge, html, css etc if I am illegible for any job then send it on my email tatranakshay276@gmail.com

1699


Who found java?

977


What are the procedures?

1057


What is the this keyword?

1028