Introduction
In this article, we will discuss Object-Oriented Programming (OOP) principles in Java. We will also discuss its features in detail i.e polymorphism, inheritance, encapsulation and abstraction.
OOP Concepts In Java
Object means real-world things such as pen, paper, chair, etc. OOP is a technique that helps in designing a program more effectively using classes and objects. It provides the following concepts.
Class
Object
Inheritance
Polymorphism
Abstraction
Encapsulation
Advantages of OOP in Java
The following are the advantages of OOP over procedure-oriented programming languages:
- makes the development of programs easier; in procedure-oriented languages it's difficult to manage the coding of the program when the program increases in size.
- provides a way to interact with real-world data more effectively.
- allows the development of solutions for real-world problems.
- provides hiding of codes but in a procedure-oriented language, the data can be accessed from anywhere.
There are four main features of OOP; they are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Let's discuss each one in detail.
Encapsulation in Java
Encapsulation means binding all methods and classes in a single class. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private then it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code.
Encapsulation provides maintainability, flexibility and extensibility of our code.
Advantages
The following are a few advantages of using Encapsulation:
- Provides the ability to change one part of the code without affecting another part of code.
- Controls the access of the user interface.
- With new requirements, it is easy to change the encapsulated code.
- Helps to write immutable classes in Java that are beneficial in multi-threading environments.
- Encapsulation in Java makes unit testing easy.
- Reduces the coupling of modules since all pieces of the same type are encapsulated in one place.
Inheritance in Java
The main feature of Inheritance is code re-usability. So when making a new class we can use a previously written class and further extend it. In Java, classes may inherit or acquire the properties and methods of other classes. A class derived from another class is called a subclass, whereas the class from which a subclass is derived is called a super class. A subclass can have only one super class, whereas a super class may have one or more sub-classes.
Example
- class StudentRec
- {
-
- String name;
- int rollno;
- int get(String n, int r) {
- name = n;
- rollno = r;
- return (0);
- }
- void showDetails() {
- System.out.println("Name : " + name);
- }
- }
-
- class InClassDemo extends StudentRec
- {
- public static void main(String args[]) {
-
- StudentRec studObj = new StudentRec();
- studObj.get("SANDEEP SHARMA", 92);
- studObj.showDetails();
- }
- void displayDetails() {
- System.out.println("Sample Info Display");
- }
- }
Output
Polymorphism in Java
In Core Java, Polymorphism is an easy concept to understand. Polymorphism in Greek is a combination of poly, which means many and morphism which means forms. It refers to the object's ability to be Polymorphic depending on its type.
There are two types of Polymorphism available in Java.
- Static Polymorphism
- Dynamic Polymorphism
Let's discuss Static Polymorphism. It's compile-time Polymorphism. We have two important concepts in Polymorphism, i.e Method Overloading and Method Overriding.
Method Overloading in Java
In Java method overloading we define two or more methods with the same name but different parameters. The methods are said to be overloaded, and the process is referred to as method overloading.
Example for Method overloading
The following example program will make you understand Method Overloading:
- class Sub {
- void add(int tamil, int english) {
- System.out.println("The total of tamil and english is " + (tamil + english));
- }
- void add(int tamil, int english, int maths) {
- System.out.println("The total of tamil english and maths is " + (tamil + english + maths));
- }
- }
-
- public class MetOvlDemo {
- public static void main(String arg[]) {
-
- Sub sb = new Sub();
-
- sb.add(90, 80);
-
- sb.add(95, 85, 100);
- }
- }
Output
Method Overriding in Java
Now we will discuss what dynamic polymorphism is. It's run time polymorphism. We can also call it Method Overriding. In a class hierarchy, when a method in a sub class has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. This feature is called method overriding.
Example
- class MathsSqr1 {
- void calculate(double price) {
- System.out.println("Sqare value " + (price * price));
- }
- }
-
- class MathsSqr2 extends MathsSqr1 {
- void calculate(double price) {
- System.out.println("Sqare value " + (Math.sqrt(price)));
- }
- }
-
- public class Metovrr {
- public static void main(String arg[]) {
- MathsSqr2 msd = new MathsSqr2();
- msd.calculate(25);
- }
- }
Output
Abstraction in Java
When we hide the unnecessary detail and defining the useful (relevant) detail, then the procedure is said to be an abstraction. An interface or abstract class is something that is not concrete, something that is incomplete. Another way of providing a simple explanation is to use a more complex system as an example. One does not want to understand how an engine works. Similarly one does not need to understand the internal implementation of the software objects.
Abstraction in Java is done by using an interface and abstract class in Java. In order to use an interface or abstract class, we need to explain the methods of an interface or abstract class in a sub-class.
Abstraction Example: Engine, Driving.
Main advantage: the user gets data according to their needs but they don't need to use unnecessary data. The following example will show Abstraction.
Example
- public class Abstraction {
- private int accNO;
- private String custName;
- private float accBlnc;
- private float profit;
- private float loan;
- public void dislayClerkInfo() {
- System.out.println("Accout number " + accNo);
- System.out.println("Customer name " + custName);
- System.out.println("Account Balance " + accBlnc);
- }
- }
Note: This class only defines the structure but not any implementation of them.