Object Oriented Programming in Java
Object oriented programming in Java deals with objects to resolve real-world complex problems
In this article, we discussed some basics of OOPs and their characteristics. Before going to know more detail about Object Oriented Programming (OOP) we need to go through the difference between OOps and Procedural Programming.
Object Oriented VS Procedural Programming
In Procedural programming, programs are divided into a number of modules or functions. It gives more priority to the functions rather than data. So we can say procedural programming is action-based programming that makes it difficult to modify the data and functions whenever the user wants. Eg: C, FORTRAN, Pascal, Basic.
To resolve this problem, a new way of programming is introduced that focuses on real-world entities named as objects. The object oriented programming consists of objects that hold members and member functions/methods to solve real-world problems. So modification of member functions or members becomes easier as compared to Procedural programming. Eg: C++, Java, C#, Python.
Object Oriented Programming
Object and Class
An object is a real-world entity with a state, behavior, and identity. The state represents the value of the object, behavior represents the functionality and identity refers to a unique ID that hides from the external world.
A class contains a set of related attributes and methods to perform different operations of the object. A class refers to the structure of an object, and its constructor has the capacity to create multiple objects based on the user's needs.
A class can be created by using the ‘class’ keyword. Every class has its own default constructor. A constructor holds the same name as a class and also takes responsibility for the creation of an object.
Here in the given example, the class ‘Learning’ instantiates an object named ‘student1’ with the help of the class constructor.
The class Learning contains data members and member functions, that accessed the outside world through the object named student1.
Pillers of Oops
The four important factors or pillars of object-oriented programming is
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
A detailed description of the above concepts is given below.
Encapsulation
IGenerally, Encapsulation refers to wrapping up of data members and member functions into a single unit as a capsule. So it becomes easier to manipulate related methods and data associated with a class.
Encapsulation leads to data abstraction and data hiding and it is implemented with the help of access modifiers such as private ( restricts access in class), public ( access everywhere), protected ( restricts access in class and subclass), and protected ( access within package).
Abstraction
Abstraction is one of the major concepts in Java that handles the complexity of code by hiding its implementation details of it. Abstract class only shows relevant information to the outside world. Instead, the implementation details are held by its subclass.
When coming to the real-world scenario, consider a vehicle and we only know how to drive that vehicle. The internal details like, how the engine works, how they induced the fuel mix into the cylinder, etc are hidden from the outside world. Only relevant things like gear, break, and Steering are given to the outside world.
Get Anukrishna T’s stories in your inbox
Join Medium for free to get updates from this writer.
An abstract class can be created by using an abstract keyword in Java. An abstract class contains abstract methods and non-abstract methods. An abstract class can’t be instantiated and its members can be accessed through its subclass instances. The given example shows the implementation of an abstract class.
The class Car is abstract and its method is handled by its subclass SUV with reference to the instance of subclass.
Inheritance
Inheritance specifies the relationship between classes. Here the object of one class acquires the properties of another class. The class which inherits the properties is called a child class or subclass. The class whose features are inherited by another class is called the parent class or superclass.
Different Types of Inheritance are shown below.
In Java, multiple inheritance is only achieved through interfaces. Inheritance is achieved by using the ‘extends’ keyword in Java. The child class extends the parent and holds the all properties of the parent.
In the given example Fruit class’s color() method is accessed by the Apple class’s object by extending the Fruit class
Polymorphism
Polymorphism in Java refers to the ability of a method can live in different forms. The real-world example of polymorphism is carbon, as carbon can exist in many forms, i.e., diamond, graphite, coal, etc. There are mainly two types of polymorphism in Java. Compile-time polymorphism or method overloading and run-time polymorphism or method overriding.
In Java method overloading refers to that there are multiple methods with the same name. But they differ in their no.of arguments, return type, type of argument, etc. For eg. void sum(int i, int j) and int sum(int k) hold the same method name and different return types and arguments.
If a method has the same signature as the parent class, then the method is treated as an overrides method. Method overriding can be achieved through the inheritance of class.
Here we conclude that the basics of the four building blocks of object-oriented programming and the difference between procedural and object-oriented programming.










