This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects" Watch "Objects" New topic
Author

Objects

Ahrumi Lee
Greenhorn

Joined: Jul 15, 2004
Posts: 6
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

Joined: Jun 08, 2004
Posts: 118
What type of suggestions are you looking for? Fruit should be abstract.
Ahrumi Lee
Greenhorn

Joined: Jul 15, 2004
Posts: 6
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

Joined: Jun 08, 2004
Posts: 118
http://java.sun.com/docs/books/tutorial/java/index.html
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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


  • [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    Rick Portugal
    Ranch Hand

    Joined: Dec 17, 2002
    Posts: 243

    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.


    IBM 286, SCJP, SCWCD, EIEIO
    Ahrumi Lee
    Greenhorn

    Joined: Jul 15, 2004
    Posts: 6
    THANK YOU VERY MUCH EVERYONE!!!
     
    Consider Paul's rocket mass heater.
     
    subject: Objects