• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding vs Hiding Java

 
Ranch Hand
Posts: 32
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read overriding and hiding methods from OCA Oracle Certified Associate Java SE Programmer I Sybex.
But i cant find it is overriding method or hiding method when i look the code. For example in the book there are two example
First one is hiding. It hides parent method.

And the second is overriding. Child method replaces the parent method in both of parent andchild method.


But when i see both of these code without any explanation i dont now which one is overriding and which one is riding
 
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key is to look at the method’s modifier. An overridable method is one that is not declared as private, static or final. Any other member that would otherwise be inherited but is replaced in a subtype is said to be hidden.

JLS

Hiding, in the technical sense defined in this specification, applies only to members which would otherwise be inherited but are not because of a declaration in a subclass.

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nil. Hatamova wrote: But when i see both of these code without any explanation i dont now which one is overriding and which one is riding


The difference between overriding and hiding is one of the very common topics in this forum, so you'll find plenty of explanations and code examples. In this topic you'll find a very good explanation. And like I said, it's very common so using the search function you'll find plenty of topics:
  • Overriding static methods
  • overriding Interface variables
  • Question about static or private method hidden/redefined in subclass
  • Confused about static and instance methods with same name (with an excellent explanation about which rules you have to follow when hiding a static method)
  • Exceptions, overriding and constructors


  • Hope it helps!
    Kind regards,
    Roel
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Joe Bishara wrote:The key is to look at the method’s modifier. An overridable method is one that is not declared as private, static or final.


    That's not 100% correct! A subclass in a different package can define a method with the same signature as a method with package-private access of the superclass, but it's not an override (and thus you can use a more restrictive access modifier). Illustrated in this code snippet

    Here are my "rules" for overriding/hiding:
    1/ you can only override instance methods; static (class) methods can never be overridden, you can only hide static (class) methods.
    2/ in order for a non-final method to be overridden or hidden, it must be visible to the subclass

    And here you'll find a Wiki article about the difference between hiding and overriding as well.

    [edit] fixed an error. Thanks Joe!
     
    Nil. Hatamova
    Ranch Hand
    Posts: 32
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for explanation friends. I think now it is exactly clear for me
     
    Joe Bishara
    Ranch Hand
    Posts: 175
    17
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:in order for a non-final method to be overridden or hidden, it must be visible to the subclass


    I agree with this statement.

    Joe Bishara wrote:An overridable method is one that is not declared as private, static or final.


    This statement implies that a method can be overridden if it is not declared as private, static or final. In your code, the makeSound() method in the Animal class is an overridable method and can be overridden as follows:


    I get your point that it is not possible to override it in a class where it is not visible, however, the makeSound() method in the Animal class is an overridable method.

    Roel De Nijs wrote:A subclass in a different package can hide a method with package-private access of the superclass, but not override it.


    Based on your original statement "in order for a method to be overridden or hidden, it must be visible to the subclass", in your code, the makeSound() method in the Lion class does not override or hide the makeSound() in the Animal class because it is not visible.

    The Lion class simply declares a makeSound() method.
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Joe Bishara wrote:

    Joe Bishara wrote:An overridable method is one that is not declared as private, static or final.


    This statement implies that a method can be overridden if it is not declared as private, static or final.


    I agree with this statement. But that was different from my interpretation of your initial statement ("The key is to look at the method’s modifier. An overridable method is one that is not declared as private, static or final."). And if I have another interpretation, iother ranchers might have a different interpretation too. And overriding/hiding is an important topic, so you want to avoid ambiguous statements. That's why I posted the code snippet of a method with a different (access) modifier which is not overridden (as it is not visible). And that's also why I tried to answer the OP's question with 2 statements which are (hopefully) crystal-clear (and not ambiguous).

    Joe Bishara wrote:Based on your original statement "in order for a method to be overridden or hidden, it must be visible to the subclass", in your code, the makeSound() method in the Lion class does not override or hide the makeSound() in the Animal class because it is not visible.


    You are spot-on! I fixed this error in the original post (so other ranchers don't get confused when reading this thread) and gave you credit for it. Have a cow for spotting this mistake.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic