• 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

Book "Head First Java"2e edition. Page 210

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book give this question:
Q: If it's so good to use polymorphic types, why don't you just make ALL your methods take and return type Object?

I don't understand the question.
Can the sentence be replaced by:
Q: If it's so good to use polymorphic types, why don't you just make ALL your methods polymorphic methods?

Kind regards
Cor
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "If it's so good to use polymorphic types" part must be referring to whatever they just covered. I would assume it went something like this:


It's better to declare public List doSomething(Collection c) than public ArrayList doSomething(ArrayList a). That allows you more flexibility. Your method can accept any type of Collection--after all, what it's doing with that parameter isn't specific to ArrayList, is it? And you're not locking yourself into returning an ArrayList in particular. You can change your implementation to return a LinkedList, and no client code will have to change.



Or something along those lines.

What they're asking, then, boils down to something like, "Given that it's better to use less specific types, why not just declare everything as public Object doSomething(Object o)?"

Not having read the preceding material, however, that is only a guess
 
cor van dijk
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank for your answer.
I expected an answer that says YES your right or NO "take and return type Object?" means that ....

 
Ranch Hand
Posts: 32
Mac OS X Safari Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello! im new to java, and i'm also studying HFJ 2nd edition, so... i try to reply to your question as a kind of "sharpen your pencil" (fingers in this case ).รน

if i've understood well, polymorphism has to do with inheritance. So a polymorphic method means that method can be overridden, right?

I dont think the question in the book can be replaced by your question...

let's see what the question is asking. it asks if it would be good to use always type object. but, as it explains in the answer if you use always type object, you could use only the method declared in class object. None else.

to say, as you write, that every method is polymorphic, means that every method could be overridden (i think ) or that some method are abstract (and have to be overridden forcefully). but it's useful to have some method that cant be overridden (it's possible to create a very huge api, for instance, having the certainty that everybody in the world when uses a certain class will get that result...).

ok, im not sure i've replied to your question... i just tried... it's only 1 month studying java, and i've never programmed before. so take it as it comes.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what it means is that you should be specific about what types you are handling, but warns against being too specific. I hope Bert won’t mind, but I think I can copy what it actually says under “fair use”.

HFJ 2/e page 210 wrote:Q: If it’s so good to use polymorphic types, why don’t you just make ALL your methods take and return type Object?

A: Aaah… think about what would happen. For one thing, you would defeat the whole point of ‘type‑safety’, one of Java’s greatest protection mechanisms for your code. With type‑safety, Java guarantees that you won’t ask the wrong object you meant to as of another object type. Like, ask a Ferrari (which you think is a Toaster) to cook itself.
But the truth is, you don’t have to worry about that fiery Ferrari scenario, even if you do use Object references for everything. Because when objects are referred to by an Object reference type, Java thinks it’s referring to an instance of type Object. And that means the only methods you’re allowed to call are the ones declared in class Object! So if you were to say:

Object o = new Ferrari();
o.goFast(); //Not legal!


You wouldn’t even make it past the compiler.
Because Java is a strongly‑typed language, the compiler checks to make sure that you’re calling a method on an object that's actually capable of responding. In other words, you can call a method on an object reference only if the class of the reference type actually has that method. We'll cover this in greater detail a little later, so don't worry if the picture isn't crystal clear.

So you cannot replace the question as OP suggested.

There is an answer to the OP's alternative question: there are some methods where the writer wishes to insist they have exactly the same behaviour in all instances. There are various ways (e.g. declaring a method final to prevent overriding and prevent the use of polymorphism for a particular method.
 
cor van dijk
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alex Derek and Campbell Ritchie,
Thank you for your answers. I studied the subjact again I think I understand it.
Thank for your help.
Kind regards
Cor van Dijk
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Alex Derek
Ranch Hand
Posts: 32
Mac OS X Safari Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java is a big bitch, but it's also really funny
 
cor van dijk
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Derek wrote: java is a big bitch, but it's also really funny


I think your right. Most of the time learning Java is exhausting, because you can't keep up with your scedule.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

cor van dijk wrote:I expected an answer that says YES your right or NO "take and return type Object?" means that ....


Programming 101 lesson for you: Be very suspicious of any YES/NO answer, or one that includes the words "always" or "never". And that doesn't just apply to Java.

Questions like this relate to design; and design is almost never black-and-white (note: almost ).

One thing to think about: if any function could be written, as Jeff suggested, as:
public Object doSomething(Object... o) {
don't you think it would already be part of every Object-Oriented language on the planet?

My suggestion: write a method like that and then try to use it. That'll probably teach you more than any amount of discussion here.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

cor van dijk wrote:Thank for your answer.
I expected an answer that says YES your right or NO "take and return type Object?" means that ....



Well, considering that I DIDN'T say "yes," and DID say "what I think they mean is...," a bit of thought should tell you that's a lot more like a no than a yes.
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic