Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Confusion On Objects

    For the longest time I've been confused about the concept of objects so I decided to look back at the java tutorials. Well when I saw this: What Is a Class? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts) I had been under the impression that classes defined an object, but the class BicycleDemo seems like just a name for the program(which is what I used to think when I first started learning) instead of a sub-object of Bicycle. So I was wondering if somebody could give me a basic explanation on objects and classes and how I'm supposed to use and name them?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Confusion On Objects

    Quote Originally Posted by HandMeTheBacon View Post
    I had been under the impression that classes defined an object, ...
    And you are correct in this assumption.


    ... but the class BicycleDemo seems like just a name for the program(which is what I used to think when I first started learning) instead of a sub-object of Bicycle.
    BicycleDemo is the name of a class and the name of a program. It has nothing more than a static main method in it, and so it can be run as a program, and since it has no instance methods or fields, you'll never create BicycleDemo objects. Bicycle on the other hand is a class that has non-static methods and fields, and so you will notice that Biccyle objects are being created in the BicycleDemo's main method.

  3. #3
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Confusion On Objects

    An object is a component of a solution to a problem. An object holds the data and the operations on that data. All java programs are classes. An instance of a class is an objectof that class. That is how you "link" the "real world" conceptually to what you do in your code. Where in the real world a car is made up of many components, so is an object in
    your code. Nouns are fields(properties) and verbs are your methods (for the most part).

  4. #4
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Confusion On Objects

    Hey thanks for the responses, that makes a lot more sense now. So does BicycleDemo act more or less as a place holder or is there some sort of standard naming convention?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Confusion On Objects

    Classes don't define objects, rather they define types of object. So, confronted with a potato and a pot of water you might say "this potato is a type of Vegetable", "this pot is a type of CookingInstrument" etc. Types abstract in a way that objects are not.

    In the example you link to BicycleDemo is not a sub-object of Bicycle. In part because types-of-thing can be subclasses of other types-of thing, but there is no sub-object relation defined for concrete instances of thing. ("this potato is/is not a sub-object of that potato" makes no sense. but "Tuber is a subclass of Vegetable" does.) Also, in this particular case, BicycleDemo is not related at all to Bicycle. If one class is a subclass of another it will use the keyword "extends" to mark the fact.

    Looking closely at the code on that page we can count 2 classes and 3 objects. The classes we have already seen. The objects are 2 Bicycle instances created near the start of the main() method. And... an instance of BicycleDemo that is not explicitly created with "new" anywhere. The BicycleDemo instance does exist: it is created by the Java runtime when the program starts, and the main() method that gets run is the main() method of that particular instance of BicycleDemo.

    Classes are typically named with nouns. But since they are types-of-thing rather than specific-things (we use nouns for both) they are usually given general sorts of noun. It's not a hard and fast rule, because we have no hard and fast distinction between general and specific things - in language or (some philosophers might argue) in reality.

    Both Bicycle and BicycleDemo are reasonable nouns. (But remember a BicycleDemo is no more a Bicycle than a BicycleRepairshop is.) It just so happens that there is only one demo instance involved and two bicycle instances. If you are wondering about what to call your class, consider someone wandering past and asking "what sort of thing does that define?". If you can answer with a noun (possibly modified by an adjective or two) that will be your class name. If you cannot, then the chances are there's too much stuff in your class.
    Last edited by pbrockway2; 01-19-2012 at 01:41 AM. Reason: slow !(

  6. #6
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Confusion On Objects

    pbrockway2 Thanks for the thorough response I think I mostly get it now, I'm gonna try doing some problems to try and get it down pat. Thanks!

Similar Threads

  1. [HELP] bit string confusion
    By aufis in forum New To Java
    Replies: 1
    Last Post: 03-07-2011, 05:42 PM
  2. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 04:42 PM
  3. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 02:47 AM
  4. Confusion over arrays
    By dbashby in forum New To Java
    Replies: 5
    Last Post: 04-04-2009, 10:38 PM
  5. Replies: 1
    Last Post: 01-22-2009, 05:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •