Unlocking Java’s OOPs Concepts › Understanding Object Oriented Programming
Table of contents
◊ What is OOPs in Java?
Object-oriented programming (OOP) is a paradigm that has revolutionized software development, providing a powerful way to model real-world entities and their interactions in code. Java, with its robust OOP features, has been a cornerstone language for developers worldwide. In this blog post, we will dive deep into the fundamentals of object-oriented programming in Java, covering key concepts, principles, and best practices to help you master OOP and write clean, efficient code.
◊ Concepts in Object oriented Programming:-
»There are various concepts in Object-oriented Programming:
1. Class and object
2. Encapsulation
3. Abstraction
4. Polymorphism
5. Inheritance
1. What is a Class and Object in Object oriented Programming?
Object Oriented programming is a way to write code with the use of entities/objects. It was introduced to apply real world situations and logics to programming. Everything we can see is an entity which had its own properties and functions.
OOPs is implemented using “class” and “objects”. To simply understand, objects are real world entities which has some properties and functions & classes are its blueprint.
Lets take a very popular example of a “car”. Suppose “Car” is a class which will have its functions and features. The features that will describe about the car and the basic functions that a car can perform. A class can have its “data” which describes the features of class and “methods” which can describe the functions of the class, so here “Car” is described by it features and functions i.e data and methods.
So for a particular type of car that will have all these basic features and functions, we can create an object or another car. Thus “Car” is a blueprint and a specific type of “car” is an object of car.
» Class: A class is a layout or draft for creating objects. In our example, we can have a Car class that defines the properties and behaviors common to all cars.
» Object: An object is an instance of a class. Each specific car, such as a Toyota Corolla or a BMW 3 Series, would be an object created from the Car class.
If you still did not get it clearly, let me explain by creating a class “car”.
Here, in the above snapshot we can see “Car” is a class which has features and functionalities of a “Car” and then by creating its objects we can define a specific type of “Car”.
“brandOfCar”, “model”, “year”, “colour”, “price” all represents features of car, therefore they are the attributes of class “Car”.
“Start()”, “accelerate()”, “brake()” all represents functionalities of the car, therefore they are the behaviors or method of the class “Car”.
2. Encapsulation :-
Encapsulation is wrapping up of data members and member functions in a single unit called “class” .It helps in hiding the data from other classes and exposing only what is needed. Encapsulation can be implemented using Access modifiers in Java. Access modifiers are some keywords that can control the accessibility of classes, variables, methods, and constructors in Java. They determine which other classes can access a particular class member.
◊ How Access modifiers help in Encapsulation?
There are different types of Access modifiers:
1. Private
2. Public
3. Protected
If you define a variable as “Private” than it can only be accessed by the members and functions of this class, thus helping to hide this variable from outside of class and implementing encapsulation.
If you define a method as “Public” then it can be accessed not only by this class, but also by other classes. In the below snapshots you can see the implementation of all the access modifiers.
3. Abstraction :-
Abstraction hides the internal implementation of code and creates a skeleton of what is required for the entity. It is achieved by creating a class and defining member variables and functions inside the class as per requirement.
Abstraction is used to reduce the complexity of application by hiding the internal implementation. It solves design level problems.
In our Car class, methods like start(), accelerate(), and brake() provide an abstraction of the car’s behavior without revealing the internal workings.
4. Polymorphism:-
Using an entity in different forms is polymorphism. Imagine you have a toolbox with a hammer, a screwdriver, and a wrench. Each tool has a different purpose, but you can use them interchangeably based on what you need to do. This flexibility and interchangeability is similar to how polymorphism works in programming.
There are two types of polymorphism in java:
1. Compile-time Polymorphism
2. Runtime Polymorphism
Compile-time Polymorphism is also called method Overloading. Think of method overloading like having multiple tools with the same name but different shapes or sizes in your toolbox.
For example, you might have a hammer tool for pounding nails into wood and another hammer tool for pulling nails out of wood. Even though they’re both called “hammer,” they do different things based on the situation. Similarly, in programming, method overloading allows you to have multiple methods with the same name but parameters are different.
In this example, there are two add methods in the Calculator class. One takes two integers and adds them, while the other takes two doubles and adds them. They’re both called add, but they work differently based on the types of numbers you give them.
Runtime Polymorphism is also called method Overriding. Think of method overriding like having a base tool and a specialized version of that tool. For instance, you might have a basic hammer tool, and then a more specialized hammer tool designed specifically for driving nails into hard surfaces. The specialized version still has all the features of the basic hammer, but it’s optimized for a specific task. Similarly, in programming, method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
In this example, both Animal and Dog classes have a sound method. However, when you call the sound method on a Dog object, it makes a specific sound (barking) because the Dog class overrides the sound method from its superclass.
5. Inheritance :-
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows new classes to inherit properties and behavior (methods) from existing classes. In Java, inheritance forms the basis for building class hierarchies and promoting code reusability.
Basically, it allows a child class (subclass) to inherit the properties and methods of another class (parent class or superclass). For example, we can have subclasses like ElectricCar or SportsCar inheriting from the Car class.
CONCLUSION:-
Summarizing , Object-Oriented Programming (OOP) represents more than just a programming paradigm, it signifies a holistic approach to software design thinking. Mastery of OOP in Java empowers developers to create code that is modular, easily maintainable, and scalable, capable of adapting to evolving requirements over time. By comprehending the foundational principles and implementing recommended methodologies, individuals are well-prepared to tackle complex software projects with assurance. Continued practice, exploration, and honing of skills are essential for unlocking the full potential of Object-Oriented Programming in the Java ecosystem.
Read more –