• 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

strange form?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't understand this:



especially the part that reads:




i never saw anything like this.

does anyone understand please?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a class cast. "z" is of type Object in method accept, and in order to invoke the punkty method on it, it needs to be cast to a class that has that method - Zawodnik apparently does.

Some more information about objects, class casts and other topics you will find useful: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
 
Emanuel Mensa
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:That's a class cast. "z" is of type Object in method accept, and in order to invoke the punkty method on it, it needs to be cast to a class that has that method - Zawodnik apparently does.

Some more information about objects, class casts and other topics you will find useful: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html



So class casting would mean making an instance of one class be an instance of another class right?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. A class cast tells the Java compiler that an object it thinks is of type A is actually of type B. The instance of the object is not changed in any way. Of course, this only works if the object is actually of type B, otherwise you'll get exceptions at runtime.

In this case, the compiler thinks the type is "Object" (which is correct, as every object is of type "Object"). whereas you know it's actually "Zawodnik".
 
Rancher
Posts: 1044
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can check at run-time whether an object is instance of a given type class or interface.


 
Emanuel Mensa
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:No. A class cast tells the Java compiler that an object it thinks is of type A is actually of type B. The instance of the object is not changed in any way. Of course, this only works if the object is actually of type B, otherwise you'll get exceptions at runtime.

In this case, the compiler thinks the type is "Object" (which is correct, as every object is of type "Object"). whereas you know it's actually "Zawodnik".



ok so initially the compiler thinks "z" is of type Object (which is a higher class) because of the parameter in the method . right?
 
Emanuel Mensa
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ivan Jozsef Balazs
where did you get "za" from? it's confused me more .
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome Emanuel

Have you read the book "Head First Java" by by Sierra-Bates? If not, please do so. Devote the next 10 days to understand the fundamentals. After you are thorough with all the basic concepts of Java language, will you be able to appreciate fully what others have tried explaining.

My answer to your question:

What you are confused about is called "Reference variable casting" in Java language. If I start explaining that in detail, it is bound to confuse you even more until you have your fundamentals about basic OOPS concepts like Inheritance, Polymorphism, Abstraction, Encapsulation etc. crystal clear in your mind. Hence, I advised you to read the book. As and when you have doubts, you can ask on the forum.
 
Emanuel Mensa
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:Welcome Emanuel

Have you read the book "Head First Java" by by Sierra-Bates? If not, please do so. Devote the next 10 days to understand the fundamentals. After you are thorough with all the basic concepts of Java language, will you be able to appreciate fully what others have tried explaining.

My answer to your question:

What you are confused about is called "Reference variable casting" in Java language. If I start explaining that in detail, it is bound to confuse you even more until you have your fundamentals about basic OOPS concepts like Inheritance, Polymorphism, Abstraction, Encapsulation etc. crystal clear in your mind. Hence, I advised you to read the book. As and when you have doubts, you can ask on the forum.



Thanks for your advice mansukhdeep. I have read some books...most use technical languages. I couldn't understand everything. I'm a greenhorn and that's why I'm on a forum for java greenhorns. If I understood what I read in books, I wouldn't be a greenhorn....and I prolly might not be here ( except to help others) right? You referring me to other books will keep me in circles...I appreciate your advice but I would be more grateful if you tried to explain to me what's going on like our friends above are trying to do...
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Head First Java explains it this way...

You have different models of TVs. Fancy TVs do fancy things, and basic TVs do basic things. A basic tv may have:

volume up
volume down
channel up
channel down
power

A fancy TV has all the above, plus

pic-in-pic
direct channel access (i.e. "go to channel 87)
SAP

Now, a basic remote can zap a fancy tv, and it will work. You will not be able to access the fancy feature with a basic remote, but you can do the basic stuff. Changing what type of remote you use does not change the TV.


so...

the TVs are like objects. The remotes are like references. So when you have something like


on line 1, you have an Object reference (named 'z'). That is like a basic remote. It is pointing to some kind of TV, but for right now, the only 'buttons' available are the ones for an Object.

When you say


You are telling the compiler: "I know you think z should not be allowed to call the punkty method since 'z' is defined as being an Object reference, and the Object reference (i.e. remote) does not have a punkty method on it. But I know that when we get here, the ACTUAL object 'z' is pointing to is a Zawodnik, so for now, pretend that is true and let me do this.".

The compiler will see if this is even possible. For example, you cannot cast a String reference to an ArrayList, because there is NO POSSIBLE way that could happen. However, since the inheritance tree does allow for Zawodniks and Objects to be in the same tree, the compiler says "OK".

Now, when you get to your runtime, if it indeed turns out that the Object is NOT a Zawodnik, you'll get a runtime error.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you paste the whole code Emanuel? Then I would be able to understand the whole picture. As of now, your code says:




Can you also paste the implemented interface "Predicate"? I will be able to give you a better picture of what is going on and why it is done the way it is.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emanuel Mensa wrote:Thanks for your advice mansukhdeep. I have read some books...most use technical languages. I couldn't understand everything. I'm a greenhorn and that's why I'm on a forum for java greenhorns...


OK, well the best advice I can give you, as a greenhorn, is: DON'T CAST.

The whole point of using an Object-Oriented language is that you have the correct type of object; otherwise, why not just make everything an Object and be done with it?

Admittedly, there are a few situations when you can't always know; but since 1.5 and the introduction of generics, there really is very little reason why you should ever need to cast anything.

So, my advice:
1. Avoid casting at all costs.
2. If you find you absolutely cannot do without a cast (and ask yourself whether there is any other possibility at least 3 times before you code one), follow the good advice you've been given.

HIH

Winston
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Winston, people are trying to answer the kid's question on how to hold the bat. And you just come and hit a home run. Not fair.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:Well Winston, people are trying to answer the kid's question on how to hold the bat. And you just come and hit a home run. Not fair.


You may be right, but it's simple advice: don't cast.

Just like:
  • don't obsess about memory
  • don't use '==' unless you have to
  • don't use String for anything except strings

  • The problem with all of the above is that explaining why you don't do it takes more knowledge than a lot of beginners have when you give the advice. That doesn't make it bad.

    Winston
     
    Mansukhdeep Thind
    Ranch Hand
    Posts: 1164
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You left out instanceof operator as one of the things one must avoid using as far as possible. It goes against the very idea of OOP paradigm which calls for classification of objects in a proper manner
     
    Emanuel Mensa
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @fred rosenberger: you're AMAZING!! thanks for your time and explanation. you made it so simple. i actually bought (hehe, i know i could download it for free) "Head First Java" yesterday and i started from scratch.
    not because i don't know what a variable is or what an Object is or what inheritance is. but wanted to catch all the basic things again . thanks man.

    @Winston Gutkowski: simple advice but what if I HAVE to cast. then i have to know how to do it and i MUST do it right? hehe. but thanks, i'll try not to cast, even if i need to do it i won't )

    @Mansukhdeep Thind: thanks for your help mate. actually i didn't write that program. in my data structures class we're learning iteration now and my prof post that code to explain
    something, i know about casting and autoboxing except that i never had an idea you could cast an object you created yourself (i didn't think hard enough)...but you guys are amazing!!

    thanks...now i'm happy. lets get back to coding.

    PS: i'll have more questions that will do nothing but force you to think i'm stupid .
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Emanuel Mensa wrote:@Winston Gutkowski: simple advice but what if I HAVE to cast. then i have to know how to do it and i MUST do it right? hehe. but thanks, i'll try not to cast, even if i need to do it i won't )


    As I said, with generics, there is almost no need to cast these days (the only major exception still being, unfortunately, when you're implementing equals()); and furthermore, it's often a sign that your code (or your design) is flawed.

    PS: i'll have more questions that will do nothing but force you to think i'm stupid .


    Asking questions is almost never stupid. Us bods who think we have all the answers on the other hand...

    Winston
     
    Mansukhdeep Thind
    Ranch Hand
    Posts: 1164
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    PS: i'll have more questions that will do nothing but force you to think i'm stupid .



    I believe it's the opposite. If you have no questions at all, then probably you are stupid because you think that you know everything.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic