Object Oriented Programming concepts inJava
For us to understand what object-oriented programming is, we need to first understand what an object is. To understand the concept of an object, we need to first understand the concept of primitive data types. Primitive data types are variables that store single simple values, e.g, Byte, Int, Float, Double, Char and Boolean. These primitive data types store a single type of data. A few decades back, most of the programming requirements were fulfilled using primitive data types, as many programs written at the time were not complex enough and these data types sufficed the requirements of programming. However, as programs became larger and more complex the primitive data types fell short of meeting the complex data structures requirement that coding required. Programmers wanted to group variables of similar types together and there were certain common properties exhibited by these programming inputs that needed to be grouped together. e.g., when programming a letter in a word game, we will need a letter name variable, a position variable, a Boolean variable for whether a space is available or filled, and factor in the same combination of variables for the opponent’s letter pieces. So, grouping related variables together became a sine qua non for complex programming to become a reality.
What is an Object
This led to the concept of an object. An object is an entity, it can be a real physical thing or it can be something like a meeting, a game, an appointment or an event. Programmers define objects as having various types of data associated with them together with certain rules which describe how to treat this data. So, an object refers to an entity exhibiting a bundle of attributes and behaviour:
State/ attributes represented by Variables
Behaviour represented by Methods
Let us look at some examples to understand it further:
Breed, color, size, fresh water, sea water
Swims, eats, communicates
Breed, color, age, gender
Barks, eats, sleeps, runs, plays, guards
Wooden/other, no of keys, pedal or no pedal, electric, with/without microphone
Plays notes, vibrates, amplifies sound, produces music
Brand, memory size, pixel of Camera, battery strength, sd slot
Call, text, photos, recordings, play videos, audios, search feature
Colour, taste, smell, size, name of variety
Sweet, delicious, big, can be eaten
Name, age, address, hobbies
Walking, talking, eating, breathing
An object resides inside a class in the program, so a class is also called the blueprint of an object. A class is a set of objects that share common attributes and behaviour. When an object of a class is created, the class is said to be instantiated. A single class may have a number of instances. We use the keyword ‘new’ to create an object of the class.
Four basic concepts of Object-Oriented Programming language
The four fundamental concepts of Object-Oriented Programming are Abstraction, Encapsulation, Inheritance and Polymorphism.
Abstraction means to simplify reality. e.g, a person is an object. But when we have to design software about a person, it is unlikely that we would be interested in knowing everything about that person. Rather, we will only be concerned about the data that are relevant to our application and the tasks that can be performed with those data. Abstraction refers to the idea of only showing the essential details to the user and hiding the inner workings of the code or software which are not relevant to the user's requirements. E.g., when driving a car, we need to understand how the steering wheel, brakes, gas and gas tank works. It is quite possible to drive a car without knowing in detail how the inner mechanism of a car engine functions. The makers of the car took care of the minutest specifications of the car so that general users of a car do not have to worry about those. The same principle applies to abstraction. there are two ways to create an abstract class in Java:
create an Abstract class using the keyword ‘abstract’
create an interface to achieve 100% abstraction
The second fundamental concept of OOP is Encapsulation. it means that the data contained inside an object and methods defined inside a class are bound together and tied as an entity. A class can be made fully encapsulated by making all the data members of the class private and then using setter and getter methods to set and get data. This is done to hide the complexity and inner workings of an object from the programs and programmers who may not need access to it. When working on complex projects, classes and methods are created by one set of programmers which are then used by other programmers to write code without knowing the exact details of how the original classes were created. A programmer will be able to write and execute code by knowing the name of the class, the properties and method available and any data that needs to be supplied when they are called. The implementation code of those properties and methods may remain hidden from those who do not need it to write their code. This ensures that the data and methods encapsulated as a unit are safe. Encapsulation keeps the programmer in control of access to data and safeguards the intellectual property of the company. Class access modifiers help define which classes have access to other classes, methods or attributes. There is a basic structure of a class as well. it declares the following things:
Name of the class—starts with upper case
Access Modifier—could be any of the following four types and is placed before a class name. it defines how and from where the class methods and variables can be accessed in a java program.
public—accessible by all classes
protected—accessible by the package in which it is defined
private—accessible only within the class in which the method is defined
default –accessible within the same class and package
Return type—the data type of the value returned by the method or null if no value is returned.
Method name—a meaningful name showing what the method does followed by a parenthesis which may or may not contain parameters.
Method body—block of code to be executed, this helps in code reusability and maintenance.
This brings us to the third fundamental concept of OOP which is Inheritance. This means that a class can derive its methods and properties from another class. For instance, a school class defines the methods and properties of a school object. A student at a school is also part of that school database. So, through inheritance, a student class derives the method and properties of a school class. A teacher at the school is also a part of the school employee database. A teacher-employee class can have some additional methods and properties of its own. It can extend the school class. So can a student. An admin staff of the school is also a school employee. Inheritance helps understand these types of different hierarchical relationships. A class at the top of the inheritance hierarchy is the base class or parent class. Any class that derives from another class is the sub-class or child class. The keyword used to establish inheritance is ‘extends'. Inheritance can either be an IS-A relationship or a HAS-A relationship. e.g., A student IS-A part of school but he/she also HAS-A name, address, and age. There are different types of inheritance in Java.
Single inheritance---> When a class inherits another class, e.g., Mango class extends Fruit class.
Multilevel inheritance---> When there is a chain of inheritance, e.g., a MangoSeed class inherits Mango class which in turn inherits Fruit class.
Hierarchical inheritance---> When two or more classes inherit a single class, e.g., both Mango class and Grapes class inherit the Fruit class.
The final fundamental concept of object-oriented programming is Polymorphism. Literally, the word polymorphism means many forms. It means that a class can implement an inherited method in its own way. The school class has a method that will save details of all objects created from the school class through its database. But it may be necessary to save a student’s details or teacher’s details differently in separate databases. Polymorphism allows for this. The teacher class can override the workings of any methods or properties that it inherits with a new version of its own. If a class has multiple methods having the same name but different parameters, it is known as method overloading. There are two ways to overload a method in Java:
By changing the number of arguments
By changing the data type.
If a child class provides a method which is the same method as declared by the parent class, it is known as method overriding. The rules are that the method must have the same name, the same parameter as the parent class and there must be an inheritance IS-A relationship between the child class and the parent class.
Understanding OOP concepts is key to learning how an object-oriented programming language works. To recap, the following are the fundamental concepts of OOP:
Abstraction means to simplify and focus only on the data and processes that are relevant to the application under development.
Encapsulation means that data and programs are bound together as a unit and their complexity is hidden to safeguard the original piece of code.
Inheritance means that a class can derive its methods and properties from another class. This can arise from an extensive hierarchy of parent and child classes.
Polymorphism means that different subclasses of the same superclass which inherit the same interface can implement those interfaces in their own ways.