A Therapeutic Approach to OOP - Part 2

A Therapeutic Approach to OOP - Part 2

In part one of A Therapeutic Approach To Object-oriented programming (OOP) , I gave an overview of OOP and its concepts.

In this article, I will introduce you to classes and objects in OOP, how to create and implement them in your code and its importance with practical examples using Java.

Prerequisites

Before you begin this article you will need the following:

What are Classes and Objects in Java?

You must have come across the word “class” in your code in one way or the other, I remember a friend asking me if there is another definition that can be given to a class apart from “a class is the blueprint of an object” Hmmm! I guess this is a popular definition everywhere!

Objects, or more precisely, the class objects come from or are essentially reusable software components. There are date, time, audio, video, automobile, people objects, etc.

Almost any noun can be reasonably represented as a software object in terms of attributes (e.g., name, color, and size) and behaviors (e.g., calculating, moving, and communicating).

Practical example of an Object

To help you understand objects and their contents, let’s begin with a simple analogy of a car. Before you decide if you want to drive a car really fast by pressing its accelerator pedal, someone has to design it.

To design a car, the first step is to create engineering drawings, similar to the blueprints that describe the design of a house. These drawings include the design for an accelerator pedal. The pedal hides from the driver the complex mechanisms that actually make the car go faster, just as the brake pedal “hides” the mechanisms that slow the car, and the steering wheel “hides” the mechanisms that turn the car.

This enables people with little or no knowledge of how engines, braking, and steering mechanisms work to drive a car easily. Just as you cannot cook meals in the blueprint of a kitchen, you cannot drive a car’s engineering drawings. Before you can drive a car, it must be built from the engineering drawings that describe it. A completed car has an actual accelerator pedal to make it go faster, but even that’s not enough the car won’t accelerate on its own (hopefully!), so the driver must press the pedal to accelerate the car.

Let’s use our car example to introduce some key object-oriented programming concepts. Performing a task in a program requires a method. The method houses the program statements that actually perform its tasks. The method hides these statements from its user, just as the accelerator pedal of a car hides from the driver the mechanisms of making the car go faster. In Java, we create a program unit called a class to house the set of methods that perform the class’s tasks. For example, a class that represents a bank account might contain one method to deposit money to an account, another to withdraw money from an account and a third to inquire what the account’s current balance is. A class is similar in concept to a car’s engineering drawings, which house the design of an accelerator pedal, steering wheel, and so on.

Just as someone has to build a car from its engineering drawings before you can actually drive a car, you must build an object of a class before a program can perform the tasks that the class’s methods define. The process of doing this is called instantiation. An object is then referred to as an instance of its class.

A practical example of a class

Think of a class as a prototype of a car, it contains details about the colors, door type, engine, wheels, etc. The car serves as the object here. Since many cars can be made from the same description, we can create many objects from a class. Without a class you can't have your objects, the objects enable you to create information for your database and get information from your database. For example, Facebook has an account object that has our name, age, etc. A class is where you list all the fields and implement the methods while defining what an object should look like.

How to create and implement classes and objects in java

Creating a class using the IDE IntelliJ IDEA but you can opt for other IDEs you are comfortable with, it's almost the same procedure.

  • Now to create our first class, right-click on this src folder and select New and then Java Class

Screenshot (98).png

  • Give your class a name, let's call it testing.

Screenshot (99).png

  • You can see that IntelliJ has automatically created a file inside the src folder with the same name as the class testing.

Screenshot (100).png

In java, each class should be implemented in its own file (e.g. myClass.java).

A class declaration contains the following:

  • Modifiers: It can be public or default modifiers
  • Class name: This is the name you give to your class
  • Superclass: This is if there is a parent class in the equation (I will explain this better in future articles)
  • Body: It is enclosed with curly braces {}

public class testing { //body }

Creating an Object

In Java, you create an object by creating an instance of a class or, in other words, instantiating a class.

Objects can be created in the following form:

  1. School Uniben = new School();

This creates a new School object. This single statement actually performs three actions: declaration, instantiation, and initialization. School Uniben is a variable declaration that simply declares to the compiler that the name Uniben will be used to refer to an object whose type is School, the new operator instantiates the School class (thereby creating a new School object), and School initializes the object.

2. Then we have the declaration of an object which is not a necessary part of object creation, object declarations often appear on the same line as the creation of an object. Like other variable declarations, object declarations can appear alone like this:

School Uniben;

3. The new operator instantiates a class by allocating memory for a new object of that type.

School Uniben = new School("100", "Uniben");

Classes provide constructor methods to initialize a new object of that type. A class may provide multiple constructors to perform different kinds of initialization on new objects. When looking at the implementation of a class, you can recognize the constructors because they have the same name as the class and have no return type. Get more details about constructors here.

Conclusion

Classes and Objects are very important in Java and without them, you can't code in Java. A java code can’t be outside a class. Objects allow you to do things with them while a class enables you to make those objects.

Credits

W3Schools

Geeks for Geeks

Udacity on Object-oriented programming