• Post Reply Bookmark Topic Watch Topic
  • New Topic

can i use if?  RSS feed

 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have differents types of ball
i have three class
i have a class plasticball
i have a class woodball
i have a class normalball
and one UserInput

i want in my main
i have 30 balls and the user to give which one of them is plasticball,which one of them is woodball and whitch is normalball.So could i do that:


 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggest you read about polymorphism, which is what this appears to be.
You define a base class Ball, then you extend from this for your three special types of ball.
You won't need any if statements then, you let polymorphism take care of which method is called, which java can do automatically based on the actual object (ball) type.
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:Suggest you read about polymorphism, which is what this appears to be.
You define a base class Ball, then you extend from this for your three special types of ball.
You won't need any if statements then, you let polymorphism take care of which method is called, which java can do automatically based on the actual object (ball) type.
can you give me a simple example because i didnt understand it well.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the Java Tutorials? There's a part about polymorphism.
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Have you been through the Java Tutorials? There's a part about polymorphism.
yes ofc but nothing i didnt understand its confusing
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Paul can teach his dog polymorphism, you should look in the same place.
 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If Paul can teach his dog polymorphism, you should look in the same place.

Off Topic: I think Kathy Sierra wrote that little story. She is the 'CowGirl' after all.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're probably right; the style is much more KS.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!