[HD Pg 0, Sec. 12.1.4 - inheritance-of-instance-methods-vs-static-methods]

Moderator: admin

Post Reply
demetrio
Posts: 16
Joined: Mon Sep 30, 2019 11:40 am
Contact:

[HD Pg 0, Sec. 12.1.4 - inheritance-of-instance-methods-vs-static-methods]

Post by demetrio » Tue Oct 22, 2019 7:01 pm

Statement copied from page 265: "Conceptually, however, the difference that I highlighted above for variables also exists for methods. Conceptually, a subclass inherits its own copy of an instance method. This is proven by the fact that a subclass can completely replace the behavior of an inherited instance method for objects of the subclass by "overriding" it with a new implementation of its own without affecting the behavior of the superclass's implementation for objects of the superclass. On the other hand, a subclass merely gets access rights to a static method of its superclass and so, the subclass cannot change the behavior of the inherited static method. The subclass can "hide" the behavior of the superclass's static method by providing a new implementation but it cannot replace the super class's method ".

Deshmukh, Hanumant. OCP Oracle Certified Professional Java SE 11 Programmer I Exam Fundamentals 1Z0-815: Study guide for passing the OCP Java 11 Developer Certification Part 1 Exam 1Z0-815 (p. 265). Enthuware. Edição do Kindle.

Kindly, can you explain by some example what is the difference between "overriding" and "hide"? Well, it is clear that regard overriding "a subclass can completely replace the behavior of an inherited instance method for objects of the subclass by "overriding" it with a new implementation of its own" and about hiding it can be reached by "providing a new implementation". Well, at the end, isn't exactly the same result? I read carefully https://stackoverflow.com/questions/105 ... a-confused and as far as I can see it is just a semantic approach: weather is not static then it is override while weather this is static then we call hide. At the end we replace behaviours in either case.

admin
Site Admin
Posts: 8543
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 0, Sec. 12.1.4 - inheritance-of-instance-methods-vs-static-methods]

Post by admin » Tue Oct 22, 2019 11:18 pm

>Well, at the end, isn't exactly the same result?
The way the replacement behaves in overriding and hiding is different. In overriding even after casting a reference variable to base class, the subclass method is invoked. In hiding, if you cast the subclass reference to base class, the base class's field or static method is invoked. So, no, it is not the same result. Why do you think it is the exact same result?
If you like our products and services, please help us by posting your review here.

demetrio
Posts: 16
Joined: Mon Sep 30, 2019 11:40 am
Contact:

Re: [HD Pg 0, Sec. 12.1.4 - inheritance-of-instance-methods-vs-static-methods]

Post by demetrio » Wed Oct 23, 2019 4:47 pm

I read: "I suggest that you write a similar example to validate the above rules in case of hiding of a static method" (source: Deshmukh, Hanumant. OCP Oracle Certified Professional Java SE 11 Programmer I Exam Fundamentals 1Z0-815: Study guide for passing the OCP Java 11 Developer Certification Part 1 Exam 1Z0-815 (p. 289). Enthuware. Edição do Kindle).

Regard your question "Why do you think it is the exact same result?" as fafr as I can see the way to override an inherited instance method is exactly the same way to hide a static method: re-write the method respecting the rules (for instance: same return type, exact parameter type and order). So what is the real difference from a programmer perspective? Maybe you can provide na example that shows the difference. I guess you have memory in mind when you talk about cast but from a programmer viewpoint, at the end, no matter if it is an instance method override or a static method hide, there is a new behaviour available, nothing diferent from one another, right?

admin
Site Admin
Posts: 8543
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 0, Sec. 12.1.4 - inheritance-of-instance-methods-vs-static-methods]

Post by admin » Wed Oct 23, 2019 8:57 pm

May be I am not understanding your point but think it from another perspective. Had they been exactly the same, why would there be two different terms ( overriding and hiding) for it?
You may want to get an opinion from another expert on coderanch.com forum.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests