This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between Hidden and overriden methods. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between Hidden and overriden methods." Watch "Difference between Hidden and overriden methods." New topic
Author

Difference between Hidden and overriden methods.

Somesh Rathi
Ranch Hand

Joined: Apr 27, 2006
Posts: 36
Static methods cant be overriden. However they can be hidden.
Whats is meant by Hidden methods ?
Ganesha Kumar
Ranch Hand

Joined: May 04, 2006
Posts: 56
Suppose there is a static method in the class A. Another class B extends class A. Here, the static method in A is not inherited by the class B. So, you cannot override it. However you can have a static method which has the same signature as the one in the parent class. This is called as hiding.
Somesh Rathi
Ranch Hand

Joined: Apr 27, 2006
Posts: 36
I have written the following code:

Output is : "In Parent"
How is it ? Please clarify ,I'm confused.
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
1)Suppose there is a static method in the class A. Another class B extends class A. Here, the static method in A is not inherited by the class B. So, you cannot override it. However you can have a static method which has the same signature as the one in the parent class. This is called as hiding.

a)the static method in A is not inherited by the class B-->WRONG.
b)the static method in A is not inherited by the class B ONLY if their is similer static method in class B which has same method prototype as that of static method in the superclass(In which case static method in superclass is hidden).

Hi somesh

I have written a Article on this let me know ur mail id so that it will be usefull to you.
Filippo Vitale
Greenhorn

Joined: May 06, 2006
Posts: 12
As far as I understand means that if:

_ B extends A
_ A has a method like : public static String hello(String name) { return "A: Hello " + name; }
_ B has a method like : public static String hello(String name) { return "B: Hello " + name; }

The "hello" method in B doesn't override the one in A but just it hides it. Examples:



BUT if the hello methods are NOT static




Filippo


_ _ _____________________________ _ _ <br /> <br /> * SCJP 5.0
Filippo Vitale
Greenhorn

Joined: May 06, 2006
Posts: 12
Full working code:

Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153


As I said before static method in Parent class in inheritted in to Child class-->so you are able to access it as if it is defined in Child class.
Ganesha Kumar
Ranch Hand

Joined: May 04, 2006
Posts: 56
Originally posted by Girish Nagaraj:
[QB]1)Suppose there is a static method in the class A. Another class B extends class A. Here, the static method in A is not inherited by the class B. So, you cannot override it. However you can have a static method which has the same signature as the one in the parent class. This is called as hiding.

a)the static method in A is not inherited by the class B-->WRONG.
b)the static method in A is not inherited by the class B ONLY if their is similer static method in class B which has same method prototype as that of static method in the superclass(In which case static method in superclass is hidden).

Can you please tell me why static methods cannot be overridden
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
1)Hint 1:static methods(also called class methods) belongs to class and not to any particular instance.

2)Hint 2 eclared type of reference cannot be changed where as reference can be made to point to its subclasses.

The Rest is for You to THink!
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
1)Hint 1:static methods(also called class methods) belongs to class as a whole and not to any particular instance.

2)Hint 2 eclared type of reference cannot be changed where as reference can be made to point to its subclasses(instance it points to can be changed).

The Rest is for You to THink!
Ganesha Kumar
Ranch Hand

Joined: May 04, 2006
Posts: 56
Originally posted by Girish Nagaraj:
1)Hint 1:static methods(also called class methods) belongs to class and not to any particular instance.

If static methods belog to the class, then why are they inherited.
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
Originally posted by Ganesha Kumar:

If static methods belog to the class, then why are they inherited.


No static methods are not inherited, it is a class property and not an object's property, that is why we can also call a static method without creating an instance of the class
Ganesha Kumar
Ranch Hand

Joined: May 04, 2006
Posts: 56
Originally posted by faisal usmani:


No static methods are not inherited, it is a class property and not an object's property, that is why we can also call a static method without creating an instance of the class


But the output of the following program is "In parent". That means that the
class child inherits the static method print() in the class Parent
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
1)static methods are inherited by the subclass.
2)Here inherited static method becomes property of the subclass.
3)Where as when you inherit instance method it will become property of each instance of the subclass not to subclass as a whole.

Now do you still think static method should not be inherited.
Somesh Rathi
Ranch Hand

Joined: Apr 27, 2006
Posts: 36
Hi Girish,
I'm little confused. It would be great favour if you mail me that article at rathis@techmahindra.com.
Thank you all for your response.
With Best Regards,
Somesh
sri latha
Greenhorn

Joined: Mar 03, 2006
Posts: 27
Hi Giresh, can u please send your notes to my mailid too. haripriya@haripriya.net
Thanks in advance
Harsh Pathak
Greenhorn

Joined: May 09, 2006
Posts: 4
ganesh, please email ur notes to me too

i am taking the scjp exam on may 27.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
8.4.8 Inheritance, Overriding, and Hiding
 
wood burning stoves
 
subject: Difference between Hidden and overriden methods.