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. :)
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:
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,
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)
- 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)
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
Living = New home.room
Family = home.room
Bedroom = home.room
Bedroom = home.room
Bedroom = home.room
Bathroom = home.room
Bathroom = home.room
Attic = home.room
Garage = home.room
Let's Design our Kitchen:
Kitchen.decorate ("Wood", "Wallpaper", "Curtains")
Do you understand the basic concept now?
Wow, that is a nice and detailed example of OOP, Wayne!
I think I'm getting it! :) I'll re-read that a few times and see where I stand. Thanks.
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 :)
Just wait till they start talking about inheritance and polymorphism. I have trouble spelling it let alone coding it! :xeye:
Okay here's a java example:
Now in your main function:
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()
public int getY()
public int distanceBetween( Point p2 )
return ((x - p2.x)/(y - p2.y));
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.
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());
Hope this helps,
<Edited by Owen on 12-07-2000 at 05:24 PM>
Here's another example of an object:
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?
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)