SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Honestly: I have trouble understanding it. The problem basically started when I picked up a SAMSnet Publishing book on Java 2 (Teach Yourself Java 2 in 21 Days). I read through parts of it, and was doing okay - however early on (the 2nd section of the book, I believe), it talked of OOP.

    I did my best to understand these classess and such, but I just cannot get it. And seeing as how many languages make use of OOP, I know I need to learn it. The closest I've come is finally getting the hang of functions (yes, pathetic: I know! ).

    I'd be very grateful if someone could put it into English for me, either here or through email would be fabulous. Just the basics, examples, etc. A link to an outside tutorial would be great as well, however I've seen several and I've had trouble with those as well. I think something more personal and direct might help.

    I'd me more than willing to give someone some button impressions on MyCoding.com (yes, I'm still working on it) if they can help me out.

    Thanks very much.

  2. #2
    SitePoint Enthusiast JohnM's Avatar
    Join Date
    Dec 2000
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Object oriented programming uses objects to help organize your code.

    For instance, you might have an object called $car (in PHP as I don't know java...)

    you might access variables in that object:

    $car->color = "red";
    $car->doors = 2;

    you might call subroutines:

    $car->start();

    or even have sub-objects:

    $car->windows->open(); (not sure if PHP or java can do that, I know you can in VB...)

    Hope this helps a bit,
    John

  3. #3
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This is about the same as John's description. Little more detailed and less programming.

    Concepts of Object Oriented Programming

    The purpose of Object Oriented Programming is to eliminate work. Computers need to be told specifics they can't guess at what something should be. If you forget a detail then the computer would never know about it. Without objects, todays modern programs would be hundreds of times larger than they are. I would like to try and explain Object Oriented Programming without being specific to any particular language. These concepts can be applied to programming techniques in all modern programming languages on or off the web.

    Without objects, you would have to tell the computer each room had a ceiling floor, walls, doors and windows. Think of your home as a Computer Program. With Objects you can say that a Kitchen is a room and it has the same properties as a Living Room and some extra ones.

    Your home is made up of rooms with specific functions (Objects). But each room has shared features (properties). Let's list some:
    • Floor (True/False default=TRUE)
    • Ceiling (True/False default=FALSE)
    • Walls (Integer default=4)
    • Doors (Integer default=1)
    • Windows (Integer default=2)
    • Decor (String valid="Paint, Wallpaper, Paneling")
    • FloorCovering (String valid="Carpet, Tile, Linoleum, Wood")
    • WindowCovering (String valid="Curtains, Drapes, Blinds")
    • Furniture (Collection)
    • Appliances (Collection)
    • Occupied
    • Lit (True/False)
    • Clean (True/False)


    You can change Properties over time or make them change in respect to other properties. To do this you use Methods. Each method can change one or more properties. Everything that happens to that object is handled by properties which are changed by methods.

    Methods of a room could include:
    • doClean (Sets Clean to True if all Subobjects (Collections) are Clean.
    • decorate (3 Parameters, Floor, Walls, Windows).
    • light (toggles the Lit property)
    • enter (sets occupied = TRUE)
    • exit (set occupied = FALSE)


    Now that we know what a room is made of, we can build a house. We could redefine everything for each room as we did above or we could reuse the room specification we already created. We are going to reuse because it is easier. For simplicity sake our house has 3 properties.

    home = New Object(House)
    • Rooms(collection)
    • Color
    • Address


    As you can see we added a Room Collection. Each room is a copy of the specification above. To add new rooms we would fill the collection.

    Kitchen = New home.room[1]
    Living = New home.room[2]
    Family = home.room[3]
    Bedroom[1] = home.room[4]
    Bedroom[2] = home.room[5]
    Bedroom[3] = home.room[6]
    Bathroom[1] = home.room[7]
    Bathroom[2] = home.room[8]
    Attic = home.room[9]
    Garage = home.room[10]

    Let's Design our Kitchen:
    Kitchen.Windows=3
    Kitchen.decorate ("Wood", "Wallpaper", "Curtains")
    Kitchen.Doors=2

    Do you understand the basic concept now?

    Wayne Luke
    ------------


  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, that is a nice and detailed example of OOP, Wayne!
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I'm getting it! I'll re-read that a few times and see where I stand. Thanks.

  6. #6
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    I had the same problem with that book. I hit that part and nearly had a heart attack. I mean I followed it at first but then it just lost me

  7. #7
    SitePoint Member
    Join Date
    Dec 2000
    Location
    NC, USA
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just wait till they start talking about inheritance and polymorphism. I have trouble spelling it let alone coding it!

  8. #8
    Hi there! Owen's Avatar
    Join Date
    Jan 2000
    Location
    CA
    Posts
    1,165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay here's a java example:
    Code:
    public class Point
    {
      private int x;
      private int y;
    
      Point() // defaut constructor... sets x and y to zero by default.
      {}
    
      Point( int xx, int yy ) // constructor w/ 2 args. sets x and y to whatever you specify.
      {
         x = xx;
         y = yy;
      }
    
      public void setX( int p )
      {
         x = p;
      }
    
      public void setY( int p )
      {
         y = p;
      }
    
      public int getX()
      {
        return x;
      }
    
      public int getY()
      {
        return y;
      }
    
      public int distanceBetween( Point p2 )
      {
         return ((x - p2.x)/(y - p2.y));
      }
    
    }
    Now in your main function:

    Code:
    Point atHome = new Point(); // atHome is  0,0.
    Point atWork = new Point(5,6); // at work is 5,6.
    atWork.setX(3); // we've changed the X coord (which was 5) to 3.
    atWork.setY(4); // now atWork is at 3,4.
    
    // print out the distance between x and y.  (0-3)/(0-4) in this case.
    // I know it's not the correct distance, but it's just an example.
    System.out.println("Distance between work and home is: " + atHome.distanceBetween(atWork));
    
    // now prints out some more information usting getX() and getY()
    System.out.println("Home is at " + atHome.getX() + ", " + atHome.getY());
    System.out.println("Work is at " + atWork.getX() + ", " + atWork.getY());
    As you can see, this example makes it easy to keep track of a coordinate system. Instead of having to keep track of all these variables, it makes it easy to deal with everything. If you had 500 points, it's much easier to keep track of everything. You can then write useful functions and every object of type point can use them.

    Hope this helps,
    Owen
    <Edited by Owen on 12-07-2000 at 05:24 PM>

  9. #9
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's another example of an object:

    A car.

    The car object belongs to the transportation class.

    The plane, boat, ski-doo, and hanglider objects also belong to the transportation class.

    The car object has many methods. You don't know how the methods work, or how they're programmed.. you only know what they require for input, and what they provide as output.

    The car object has an input method called drive. Another called park, one called honk, one called left-blinker.. etc..

    See how it works?
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  10. #10
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To identify something belongs to a class, use IS A relation.
    For example: HP Pavilion IS A computer, SPF IS A website
    Therefore, objects in a class share the characteristics (instance vars) and what it could do (methods)
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


Bookmarks

Posting Permissions

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