Java OOPs Simplified - Part 1
This article explains about the Object Oriented Programming concept, Class, Object in Java.
What is Object Oriented Programming?
It is a programming paradigm based on the concept of "objects".
(So what is meant by a Programming paradigm? These are nothing but different ways or styles in which a given program or programming language can be organized)
OOPs follow a bottom-up approach.
(See, in the bottom-up approach we solve smaller problems and then integrate them into a complete solution, whereas in the top-down approach, we break up the problem into smaller parts and then solve it.)
OOPs is based on real-world entities, which means that they just bring, the way we think and conceptualize things into the software design.
C++, Java, Python, C#... etc. are examples of OOPs based programming languages.
Advantages of OOPs
Code Reusability- Once we write a code, it can be reused multiple times and this is achieved mainly with the help of a key concept called Inheritance.
Security- with the help of Encapsulation, we can bind data(attributes) and methods into a single unit called class and can control the access to these using access specifiers like "Private", "Protected", and "Public.
Flexibility- In Java Polymorphism allows us to create multiple classes that can be reused, also it allows us to reuse the methods with "the same name and same parameter" in different classes and "methods with the same name and different parameters" in the same classes. ie. overriding & overloading respectively.
Improves Readability - when we use Abstraction, we can create abstract methods, which help in data hiding as well as improve the code readability for the user(here user means a programmer).
Disadvantages of OOPs
Complexity- the length of the programs will be larger than the ones written using procedural-oriented programming.
Memory Usage- Object-oriented programs tend to consume more memory than the procedural-oriented approach.
The overhead of abstraction - excessive use of abstraction may lead to complex code that is difficult to navigate, in such cases, it will be a challenge for the developer to understand how the data and methods are connected.
So these are all the major advantages and disadvantages of OOPs in Java. While stating the disadvantages I have mentioned procedural-oriented programming. So what is that? How is it different from the object-oriented programming approach? Come let's have a look.
OOPs vs. POPs(Procedural-Oriented Programming)
Object- Oriented Programming | Procedural-Oriented Programming |
Bottom-up approach | Top-down approach |
Here program is divided into "objects" | Here program is divided into "functions" |
It supports code-reusability | It doesn't support code-reusability |
It supports Inheritance | No support for Inheritance |
It supports access specifiers | No support for access-specifiers |
Now, let's have a look at the 2 important features of OOPs, Class and Object.
CLASS
So, what's a class in Java?
In simple words, it's a template or a blueprint for creating "objects". So we can say a class decides the structure and behavior of the object, a class will have.
Example: Car Class
Imagine you are designing a computer program to manage information about cars. In Java, you would create a "Car" class. Here's how you can relate it to real life:
Class: Car
- A class named "Car" represents the blueprint for all cars.
Attributes (Data):
In our "Car" class, you can define attributes such as:
Make (e.g., Audi, BMW)
Model (e.g., Q8, 7series)
Year (e.g., 2022, 2023)
Colour (e.g., Black, Blue)
Methods (Behavior):
You can also define methods to perform actions, such as:
Start the engine
Stop the engine
Now, when you create objects from the "Car" class, each object will have its specific values for the attributes (e.g., one car might be a black Audi from 2022, another may be an Ambassador from 1977), and you can use methods to perform actions on those individual cars (e.g., start the engine of a particular car).
So, in this real-life example, the "Car" class serves as a blueprint that defines the structure (attributes) and behaviour (methods) of cars, just like how car manufacturers have blueprints for designing and producing different car models.
NB: But when we speak in technical terms, Class in Java is a user-defined datatype that encapsulates data and behaviour related to a specific concept or entity.
Now let's have a look at Objects in Java.
// Define the Car class
class Car {
// Attributes
String make;
int year;
// Constructor method
public Car(String make, int year) {
this.make = make;
this.year = year;
}
// Methods
public void startEngine() {
System.out.println("Engine Started");
}
public void stopEngine() {
System.out.println("Engine Stopped");
}
}
public class CarExample {
public static void main(String[] args) {
// Create instances (objects) of the Car class
Car car1 = new Car("Audi", 2022);
Car car2 = new Car("Ambassador", 1977);
// Use the methods of the Car class
car1.startEngine();
car2.accelerate();
}
}
OBJECT
In Java, an object is an instance of a Java class, meaning it is a copy of a specific class.
It has 3 primary characteristics: identity, state, and behaviour.
Identity: It is a unique identifier like a memory address or a unique name.
State: It controls aspects of an object; in the case of describing a fan, you could have on, off, low, medium, or high state options.
Behaviour: It is used to describe what an object can do, such as a fan turning on or off or changing speeds.
Class Cars{
Cars c1 = new Cars() // an object is created
}
// Cars - class name
// c1 is the object reference variable
// new keryword(Dynamic Memory Allocator) - It allocates memory for the object and initializes it.
// Cars() - Constructor call, these are special methods within a class that are used to initialize objects.
So if "Cars" is the class, then "Audi Q8", "Ferrari 296 GTS", "Honda Jazz" etc. will be the objects. So in short the class just instructs how the object should be.
So now let's have a look at the difference between Classes and Objects in Java.
Class | Object |
Class is a logical entity | Object is a physical entity |
When a class is created, no memory is allocated. | But objects are allocated memory space whenever they are created. |
Class has to be declared first and only once. | Objects can be created many times as per requirement. |
So thats all about the OOPs, Class and Object. Now in the next part, we can look deep into the four main pillars of OOPs, i.e. Inheritance, Polymorphism, Abstraction and Encapsulation.