Getting Started with OOP: Your Kitty object may be a gangster!

To find out if the Kitty is a gangster, call method isTrouble().

When you're first diving into software development, opening up an Integrated Development Environment (IDE) like VS Code or Eclipse can feel a little like stepping into a spaceship. Text lights up, tabs open and close, and autocomplete suggestions appear unexpectedly. It can feel like a lot at first. But this unfamiliar vehicle is where the programming journey begins. IDEs are powerful tools that make writing, organizing, and debugging code easier/faster/better. 

They’re especially helpful when you're learning concepts like object-oriented programming (OOP), because they give you a window into objects and the data they contain in real time.

Objects Explained

In software programming, OOP lets you build self-contained little modules that combine data and behavior called objects making your code modular, readable, and manageable. Then you can put those objects together to build programs that are organized, intuitive, and much easier to work with.

One of the best things about OOP is how well it mirrors the real world. When you use object-oriented design, your program is built out of things referred to as objects. Your program might have a user, product, or a cat defined in your code, and each will have its own attributes (called instance variables) and behaviors (called functions in C++, and methods in java). This makes complex software easier to manage because you can focus on one object at a time, like zooming in on a single cat at the Arizona Humane Society (AHS).

For example, here's the cat object in java called Kitty used to log and evaluate the cats in the AHS. The Kitty class object has 4 instance variables (name, color, feral, gangsterFactor) and three methods: isTrouble(), purr(), and scratch(). 

A Kitty object can be created whenever a new cat is brought to the AHS and the name and color is entered. Next a boolean is set indicating if the cat is feral. Lastly, the gangsterFactor is entered as an integer from 1-10 depending on the number of the cat's prior convictions.

The Kitty is then evaluated by calling the isTrouble() method. If the Kitty is feral and has a high gangsterFactor, it will return true and the user is warned of being scratched! If not, the Kitty will purr.

Screenshot created by the Author.

And that, my friends, is an object.

As you can see in this example, OOP is awesome because it helps keep everything nice and tidy, flexible, reusable, and scales up beautifully as Kittys are added and their gangsterFactor is evaluated.

For more about object-oriented programming:

One of my favorite tutorial sites is w3schools. Here's a great object-oriented page that spells it all out using java in more detail - Java Classes and Objects (w3schools.com)

Another favorite is GeeksforGeeks. Here's a great page with an overview to give more about what we started here - Introduction of Object Oriented Programming | GeeksforGeeks

Whether you’re coding your first object or architecting a full-blown application, OOP gives you the tools to think big. So, fire up your IDE, start sketching out your first object, and know that coding objects isn't just banging out lines of java, it's creating a world inside the application you're building, just like the Kittys at the Arizona Humane Society.

👉 If you have questions about getting started on your first java OO program, please comment here. I'm happy to help!

Comments