• 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

Objects

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to create 4 classes for 4 types of objects. say, apples, oranges, cherries, and fruits.

does anyone have some good suggestions?
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What type of suggestions are you looking for? Fruit should be abstract.
 
Ahrumi Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you show me how they can be structured?

public abstract fruit()
{
}

public apple() ... fruit
{
}
and so on?

what can possible go into these objects as methods or properties(?)?
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/index.html
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that if I wanted to define an object type (or class) of Foo, I'd write something like the following.If I wanted to define an abstract object type, I'd write something like the following.Here is a list of free on-line Java tutorials and books that I have found useful:
  • Sun's Java Tutorial
  • Introduction to Computer Science using Java by Bradley Kjell
  • Introduction to Programming Using Java by David J. Eck
  • Dick Baldwin's Java Programming Tutorials
  • Interactive Programming In Java by Lynn Andrea Stein
  • Bruce Eckel's Thinking In Java
  • JavaRanch's own Campfire Stories
  • Allen B. Downey's How To Think Like A Computer Scientist
  •  
    Ranch Hand
    Posts: 243
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I made Fruit an abstract class. That means that you can use it to base other classes on, but you can't make Fruit objects directly.

    The Apple class has everything that the Fruit class has because it extends the Fruit class. Fruit is the "superclass" and Apple is the "subclass". But now the Apple class has something that the Fruit class does not: a String called myMoto.

    So now you can create (in other words, "instantiate") an Apple object and print out its values. I'll call my Apple object "a".

    Notice how the "a" object has a hasVitaminC variable. That's because the "a" object is an instance of the Apple class. The Apple class extends the Fruit class, and the Fruit class has a hasVitaminC variable.

    It prints:

    Moto = One of me a day keeps the Doctor away
    Has Vitamin C = true


    My Orange class has everything that the Fruit class has, plus a juicynessFactor variable.

    My Cherry class has everything that the Fruit class has, plus a warning variable.


    Just a note on naming classes: I called the class Apple instead of Apples because class names are singular by convention. I called the class Apple instead of apple because class names start with an uppercase letter by convention.

    I'd like to talk more, but I'm hungry now.
     
    Ahrumi Lee
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    THANK YOU VERY MUCH EVERYONE!!!
     
    what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic