Object Oriented Programming has been around for a while. When I began my career, I programmed using procedural methods until I learned about OOP. What is OOP really? When I began learning about OOP, it took me a bit to catch on to the concept. I found it to be somewhat of a large topic that at first was muddy. There are a lot of OOP principles to learn…more than can be explored within one blog post. I will periodically explore various OOP principles and you will find them scattered throughout my code projects.
When I began programming with OOP principles, one of the first items I had to learn was what classes and objects are and how they work together.
First…the definitions…
Class – a set of functions that work together to accomplish a task. It can contain or manipulate data, but it usually does so according to a pattern rather than a specific implementation. An instance of a class is considered an object.
Object – receives all of the characteristics of a class, including all of its default data and any actions that can be performed by its functions. The object is for use with specific data or to accomplish particular tasks. To make a distinction between classes and objects, it might help to think of a class as the ability to do something and the object as the execution of that ability in a distinct setting.
Here is another way of thinking about it…
Let’s say you were going to build a brand new subdivision where all the houses look alike. You create the blueprints and build your first house. You then re-create the blueprints and build a second house that looks exactly like the first one. You then repeat this process throughout the subdivision. This would not be a very wise way to build your subdivision. Why re-create the blueprints if the houses are all alike? It is a waste. Instead, you would use the first set of blueprints to build all of the houses within the subdivision.
Classes = Blueprints
Objects = Houses
So you create the class once and then can build a lot of objects from that class. Here is an example in Java…
[code lang=”java”]
// This is my class
public class House{
public House(String name){
// This constructor has one parameter, type.
System.out.println("Passed Type is :" + type);
}
public static void main(String []args){
// Following statement would create an object myHouse
House myHouse = new House( "ranch" );
}
}
[/code]
Happy Coding!
Clay Hess