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
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".
~ Mansukh
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
~ Mansukh
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...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
~ Mansukh
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.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
~ Mansukh
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 )
PS: i'll have more questions that will do nothing but force you to think i'm stupid .
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
PS: i'll have more questions that will do nothing but force you to think i'm stupid .
~ Mansukh
Don't get me started about those stupid light bulbs. |