This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Difference between Hidden and overriden methods.  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods cant be overriden. However they can be hidden.
Whats is meant by Hidden methods ?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written the following code:

Output is : "In Parent"
How is it ? Please clarify ,I'm confused.
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Filippo Vitale
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Full working code:

 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Giresh, can u please send your notes to my mailid too. haripriya@haripriya.net
Thanks in advance
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ganesh, please email ur notes to me too

i am taking the scjp exam on may 27.
 
Ranch Hand
Posts: 2023
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!