• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Java Study Guide OCP I can't get the clue of hiding/polymorphism?

 
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I thought I understand but now I have question Java OCP guide 17 Q22.
The two lines marked with //*,  is my question, especially the first one. The second is more to check if my clue is right?
(I have put the comments so that you can more or less follow what and how I think.)



And what I understand, correct me if I'm wrong At compile-time than the reference type is important and at Runtime although it behaves as Child object or not? (polymorphism, right?
Assigning the value to name and t.name is done at run-time, right?

I hope that I have not mixed up (a lot of) things?




 
Marshal
Posts: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't say, “reference type.” A reference type means a type from which objects are (at least in theory) created, meaning arrays, classes, and interfaces. The opposite of reference type is, “primitive type," and there are eight kinds of primitive type. You doubtless meant to say, “declared type.”
Please don't write comments such as to produce long lines, because they are so difficult to read.Write /* comments */ instead, and spread them over multiple lines.
Please avoid single‑letter names for parameters or variables as much as possible; use names that make their meaning obvious.
What does the book say about the problem? Which book is it? Is it Boyarsky and Selikoff? I don't have a copy. Where did the code you showed come from? It shows some poor design. I presume that poor design is there to show a point.
As far as I am concerned, polymorphism and overriding apply to instance methods, instance methods, instance methods, and instance methods. Nothing else. It doesn't even apply to private instance methods because they cannot be overridden.
If an instance method is called at runtime on the name of an object, the bytecode says to look for the method declaration in the Class<?> object that the object was derived from. That is the Cass object corresponding to the runtime type of the object.So, calling an instance method might take you to the Class<Cat> object or to the Class<Dog> object. There you might find a method definition, and that definition will be used. You might get, “Woof‑Woof” or, “Miaow.” That is called late binding, andd is also called dynamic binding and also called runtime binding.
Which version of a static method is called is determined at compile‑time. The compiler sees that the declared type is Animal and calls any static methods on that type, Animal. That is called early binding or static binding or compile‑time binding. The details of a static method are determined by static binding. All fields also undergo compile‑time binding. If you have fields with the same name in superclass and subclass, they do not override each other, but hide each other. They are two fields completely independent of each other, and therefore a potent source of confusion. The same applies to static methods; even if they have the same name, they are independent of each other and can only be hidden not overridden. No, the name field does not bind to any object, but to the declared type of the variable. That is why static members should always be referred to by ClassName.memberName and never by ObjectReferenceName.memberName.
The overridden method in Child calls the fields linked at compile‑time to its declared type, which for t is Person. Yes, that is very confusing code.
 
Nico van de Kamp
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ritchie,

I've read it again from the book en play it again. Although it's  still hard for me to understand and in some ways confusing, but I start to understand it now. And, a month later, I understand you're explanation also now....!

Thanks!

(BTW, this was one of the questions, Chapter 6 Q22)
 
Campbell Ritchie
Marshal
Posts: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nico van de Kamp wrote:Thanks Ritchie . . . I understand you're explanation also now....!

Thanks! . . .

That's a pleasure and it is always good to know when an answer has its desired effect.
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic