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

    The following are the advantages of OOP over procedure-oriented programming languages:

    • makes 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 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.

    1. Encapsulation

    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 code without affecting another part of code.
    2. Controls the access of the user interface.
    3. With new requirements it is easy to change 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 siince all pieces of the same type are encapsulated in one place.

    2. Inheritance

    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
      {
        //
    GET STUDENT RECORD.....
        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[])
          {
            //
    CREATE OBJECT OF STUDENT RECORD CLASS
            StudentRec
    studObj = new StudentRec();
            studObj.get("SANDEEP SHARMA", 92);
            studObj.showDetails();
          }

        void displayDetails()
          {

            System.out.println("Sample Info Display");
          }
      }

    Output:

    inheritance1.jpg

    3. Polymorphism:

    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.

    1. Method Overloading

    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[])
          {
            //
    create Subjects class object
            Sub sb=new Sub();
            //
    we have to call add() method by passing 2 values
            sb.add(90, 80);
            //
    here also we are calling add() method by passing 3 values, So the 3 arguments (parameters) method will get execute.
            sb.add(95,85,100);
          }
      }

    Output

    metovl.jpg

    2. Method Overriding

    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

    metovrr.jpg

    4. Abstraction

    When we hide the unnecessary detail and defining the useful (relevant) detail, then the procedure is said to be 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.

    Up Next
      Ebook Download
      View all
      Learn
      View all