Till now, we have written a program under the guise of a class, though what we have written was just a program and not a class in the proper sense. Now, we shall start programming the object-oriented way. In the due course of being taught the way to write simple programs, you have also been told an impressive deal about classes and objects. We shall now recollect all that we have learned about object-oriented programming, classes, and objects. We shall also look into the three features of object-oriented programming.

Classes and objects are the basics of object-oriented programming. In an object-oriented model, the focus is on data (contained in objects) and not on the code that manipulates the data. A class contains instance variables, constructors, and methods. We instantiate (create) objects from these classes by invoking (calling) the constructors. Each object created from a class has a copy of the instance variables. Each object manipulates its instance variables independently using the methods of the class. While variables represent an object, the methods help in the modification of these variables, and the accomplishment of tasks.

The object-oriented model of programming helps to simulate real-world objects in a better way. Let’s discuss how this is done by considering a ‘Car ‘class as an example. The class can have instance variables such as current Speed, quantityOfPetrol, and so on. We can provide methods such as startCar(), stopCar(), chageSpeed() and so on. Some of these methods do not require any additional information while a few others require.

For instance, startCar() and stopCar() doesn’t require any additional information while changeSpeed() requires the speed to which the car is to be raced. This information that is to be passed is known as an argument. In conclusion, we have designed the ‘Car’ class and we are now capable of building several Car objects from them such as ‘Car1’, ‘Car2’ and so on.

Each of these objects functions independently from each other by maintaining their own sets of variables. We can make unique objects such as ‘Car1’ and ‘Car2’ interact with each other. We now have a world of cars. We have been able to successfully build a virtual world. That object-oriented programming is all about- simulating the real world using objects built from classes.

There are three principles of object-oriented programming – abstraction, inheritance, and polymorphism. We shall look at them one by one.

Abstraction represents the essential features of a system without getting involved with its complexities. Consider the Car example again. Once you write the ‘Car’ class, other people will create Car objects and use them. You need not disclose to them how you implemented the starting and stopping of the Car. All that you need to tell them is what those methods can be called. This is what abstraction is all about.

Other people can use your Car class, create Car objects, and call methods such as startCar() and stopCar(). They are capable of starting and stopping the car without knowing how it is being done. Complexities have been hidden. Only the essentialities have been exposed.

The next feature is inheritance. It is the capability of an extra class to inherit the variables and methods of other classes. The new class can define its own variables and methods in addition to what has been inherited to provide additional capabilities to the new class. It is like how sons and daughters inherit property and can add their own earned property to the inherited property.

If we talk in terms of the Car example, a new class called SuperCar can be made to inherit from the class Car so it will have the functionality of starting, stopping and changing speed and in addition, will perform some special tasks like flying in the air similar to what we see in Hollywood movies.

The last feature of object-oriented programming to be discussed is polymorphism. This is the property by which unfamiliar objects can react to the same method call or in simpler terms a message and still produce the correct output i.e. the desired output. If we talk in terms of our Car, then we can state the feature of polymorphism as the ability to change speed to both a numeric value and a fractional value.

Unorthodox methods in those classes will handle these calls, but still, the message that would be passed would be the same, which is changeSpeed(). The only difference is in the arguments (the information to be passed). While one call passes 34 as an argument, the other call would pass 34.7 as the argument. These things might appear trivial for now, but you will appreciate these features later on when we deal with how these features are implemented in Java.

Hoping that you understand what object-oriented programming is, we can now move on to the details of how classes are defined in Java. For the rest of our discussion about building classes, we will focus on building a Student class. A student will have a name, a roll number, and marks in three subjects. Once the class is built, we should be able to create Student objects.

And there should be methods for finding the total marks, the average of two students and so on. In the due course of time, we will also see how we can assign the roll numbers automatically to Student objects starting from 1. We shall also explore some other countless possibilities.