Java was written around the concept of Object Oriented Programming. That’s OOP for short. To understand Java programming, it’s useful to know what is meant by “objects” and “object oriented.”
Let’s explore objects by using the example of a car. A car has an owner, gas, and the ability to move. A car is an object. Objects encapsulate identity, state, and behavior. In the case of our car, the car’s identity might be Tom’s car. It is in the state of having a half tank of gas. Also, one of the car’s behaviors is its ability to accelerated so it is moving or braking to a stop.
Here is a representation of car written in Java.
The above code is a Car class. It is stored in a file named Car.java and compiled down to Car.class. Think of classes as templates or blueprints for objects. First you design the object in a class, and then you use the object in a real program, just like in the real world, an engineer would first make a blueprint for a car and then make lots and lots of cars from the blueprint.
Here is an example of code using the Car class to create an instance of a car.
Organizing the objects in many applications is easy when the application is representing or manipulating real world objects or graphic interfaces. It’s easy to figure out the objects for a calculator for instance. You need a Button class that the number button objects are created from, and a Display class that allows a display object to handle displaying answers and numbers that were typed in. Object Oriented Programming makes organizing actions and attributes easy when a real world thing is being described.
Where OOP gets a bit sketchy is when the system being designed is conceptual, rather than something that actually exists. Imagine you are designing a program to solve word searches. The concepts involved can be organized in many ways, but mostly bad ways.
The first task you have when designing a new Object Oriented Program is to create a list of attributes and actions that your program is responsible for. Separate related actions and attributes and clump them together. If one clump of actions and attributes is two big, split that group into multiple subgroups of actions and attributes. When you have your actions and attributes broken down into manageable chunks, name them. You now have a the classes for your application.
This introduction works for small applications, but is only a very cursory description. For larger applications, the types of applications that cost tens or hundreds of millions of dollars to create, we often see whole teams using specialized methodologies working on the application’s design. However, this OOP introduction is good enough for the beginning Java programmer.