Procedural languages are efficient
and useful, don't let anybody tell you otherwise. However as the product
gets larger and larger it is harder keep track of how everything fits
together.
In the very basic sense Object Oriented code is a way of breaking up
code into chunks reconizable in the real world. If you wanted to make a
program that represented my house, you could have some Objects like
"House", "Person" and "Dog".
My House object would hold information about where in the world it is,
and what kind of Objects that are inside it (Like Person and Dogs).
There are a bunch of Person Objects in the house, there is the Person
Object called Adam, the Person Object called Carla, the Person Object
called Justin and so on.
Currently we only have one Dog Object (who is called Mate), sadly the
great Garbage Collector came and took our other Dog Object away :( (Java
coding joke).
Below you will see two classes (House and People), notice how I can keep
adding people into my house?
public class House {
int streetNumber;
String streetName;
String suburb;
String City;
int postCode;
String country;
<Person>ArrayList people;
<Dog>ArrayList dogs;
//constructor, getter and setter stuff to make these variables
//valid
/**
makes a new person and puts it in the ArrayList
people.
@params personName is a String representing a
persons name
*/
public void newPerson(String personName){
Person p = new Person()
p.setName(name);
people.add(p);
}
/**
Lists out all of the people in the ArrayList
peoples, to the console.
*/
public void listPeople(){
Person p;
for (Person item : people){
p = item;
System.out.println(p.getName());
}
}
}
/**
An Object of People
*/
public class People {
String name;
//constructors and stuff
/**
sets this persons name
*/
public void setName(String name){
this.name = name;
}
/**
returns this persons name.
*/
public String getName(){
return name;
}
}