• 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

can i use if?

 
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you been through the Java Tutorials? There's a part about polymorphism.
 
ekte spiriopoulos
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If Paul can teach his dog polymorphism, you should look in the same place.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're probably right; the style is much more KS.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic