Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects

 
Ahrumi Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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?
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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
  •  
    Rick Portugal
    Ranch Hand
    Posts: 243
    • Mark post as helpful
    • send pies
    • 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
    • Quote
    • Report post to moderator
    THANK YOU VERY MUCH EVERYONE!!!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic