OOP Principles In Java

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:
    1.  Encapsulation
    2.  Inheritance
    3.  Polymorphism
    4.  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:
    1. Provides the ability to change one part of the code without affecting another part of code.
    2. Controls the access of the user interface.
    3. With new requirements, it is easy to change the encapsulated code.
    4. Helps to write immutable classes in Java that are beneficial in multi-threading environments.
    5. Encapsulation in Java makes unit testing easy.
    6. 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
    1. class StudentRec     
    2. {    
    3.     //GET STUDENT RECORD.....    
    4.     String name;    
    5.     int rollno;    
    6.     int get(String n, int r) {    
    7.         name = n;    
    8.         rollno = r;    
    9.         return (0);    
    10.     }    
    11.     void showDetails() {    
    12.         System.out.println("Name : " + name);    
    13.     }    
    14. }    
    15.     
    16. class InClassDemo extends StudentRec     
    17. {    
    18.     public static void main(String args[]) {    
    19.         //CREATE OBJECT OF STUDENT RECORD CLASS    
    20.         StudentRec studObj = new StudentRec();    
    21.         studObj.get("SANDEEP SHARMA"92);    
    22.         studObj.showDetails();    
    23.     }    
    24.     void displayDetails() {    
    25.         System.out.println("Sample Info Display");    
    26.     }    
    27. }     
    Output
     
    inheritance1.jpg 
     

    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.
    1. Static Polymorphism
    2. 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:
    1. class Sub {    
    2.     void add(int tamil, int english) {    
    3.         System.out.println("The total of tamil and english is " + (tamil + english));    
    4.     }    
    5.     void add(int tamil, int english, int maths) {    
    6.         System.out.println("The total of tamil english and maths is " + (tamil + english + maths));    
    7.     }    
    8. }    
    9.     
    10. public class MetOvlDemo {    
    11.     public static void main(String arg[]) {    
    12.         //create Subjects class object    
    13.         Sub sb = new Sub();    
    14.         // we have to call add() method by passing 2 values    
    15.         sb.add(9080);    
    16.         //here also we are calling add() method by passing 3 values, So the 3 arguments (parameters) method will get execute.    
    17.         sb.add(9585100);    
    18.     }    
    19. }    
    Output
     
    metovl.jpg
     

    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
    1. class MathsSqr1 {    
    2.     void calculate(double price) {    
    3.         System.out.println("Sqare value " + (price * price));    
    4.     }    
    5. }    
    6.     
    7. class MathsSqr2 extends MathsSqr1 {    
    8.     void calculate(double price) {    
    9.         System.out.println("Sqare value " + (Math.sqrt(price)));    
    10.     }    
    11. }    
    12.     
    13. public class Metovrr {    
    14.     public static void main(String arg[]) {    
    15.         MathsSqr2 msd = new MathsSqr2();    
    16.         msd.calculate(25);    
    17.     }    
    18.    
    Output
     
    metovrr.jpg
     

    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
    1. public class Abstraction {    
    2.     private int accNO;    
    3.     private String custName;    
    4.     private float accBlnc;    
    5.     private float profit;    
    6.     private float loan;    
    7.     public void dislayClerkInfo() {    
    8.         System.out.println("Accout number " + accNo);    
    9.         System.out.println("Customer name " + custName);    
    10.         System.out.println("Account Balance " + accBlnc);    
    11.     }    
    12. }    
    Note: This class only defines the structure but not any implementation of them.