• Register
278 points
6 1

OOPs Concepts in Java

posted May 8 6 min read

Basic Concepts of OOP (Object - Oriented Programming)

Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts.

There are some basic concepts of object oriented programming as follows:

  1. Object
  2. Class
  3. Data abstraction 
  4. Data encapsulation
  5. Inheritance
  6. Polymorphism

Object:

  • Any entity that has state and behaviour is known as an object.
  • Object is the basic unit of object-oriented programming.
  •  An Object is the instance of a class. 
  • It is a bundle of variables and related methods. 
  • Objects can be created in a program only after declaring the properties and procedures in a class.
  • The information that is to be in an application is stored in the object, not in the class.
  • Objects are identified by its unique name. There can be more than one instance of an object. Each instance of an object can hold its own relevant data.
  • For example whenever a class is created, an object must be created. Without creating object, we can’t able to use class.
  • In other words, Object means a real word entity such as Pen, Chair, Table etc.

Class:

  • A class is a simply a blue print of a structural representation of attributes and actions (Object).
  • A class is a template. It neither holds any information nor occupies any memory; but an object does.
  • Class is an Abstract concept. 
  • A class contains data and methods to manipulate the data.
  • All the properties and functions are defined in a class, while they are used in an object.
  • For example, Class of Cars, Employees etc.

Note: Sub classes can be created, but sub-object cannot be created.

public class NumberDisplay      
{
     int a = 10;                                       
     int b = 15;
     public void disp()                                                                  
     {
           System.out.println(a+" "+b);                                          
     }
     public static void main(String args[])                              
     {
           NumberDisplay numDisp = new NumberDisplay(); 
           numDisp.disp();
     }
 }

Data Abstraction:

  • Data Abstraction also represents to hide the internal details and showing functionality
  • Or we can say, Abstraction refers to the act of representing essential features without including the background details or explanation between them.
  • For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components. To build the Car class, user does not need to know how the internal actions, but only how to interface with them.
  • In java, we use abstract class and interface to achieve abstraction.

Data encapsulation:     

  • Binding (or wrapping) code and data together into a single unit is known as encapsulation.
  • When using Data Encapsulation, data is not accessed directly, it is only accessible through the functions present inside the class.
  • Used for security purpose.
  • For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components which binds in to a single unit i.e., Car.

Inheritance:

  • Inheritance is the process of forming a new class from an existing class or base class.
  • Inheritance facilitates code reusability which means when we inherit from an existing class, we can reuse methods and variables of parent class and also, we can add new methods and variables.
  • The class being inherited from is called Super class (Base class or Parent class).
  • The class that inherits from another class is called Sub class (Derived or Child class or Extended class).
public class Parent
{
      int i=10;
      void disp1()
      {
            System.out.println(i);
      }
}
public class Child extends Parent
{
      int i=100;
      void disp2()
      {
            System.out.println(i);
      }
      public static void main(String args[])
      {
            Child c=new Child ();
            c.disp1();
            c.disp2();
      }
}

Polymorphism:

  • Polymorphism means , one name many forms. 
  • In OOPs terminology Polymorphism means, one method performing many tasks.
  • Overloading is one type of Polymorphism. It allows multiple methods having same name but different in parameters.
public class Employee
{
      int id;
      String name;
      int sal;
      int age;
      public void disp(int id,String name)
      {
             System.out.println("Employee ID is:"+id+"\n"+"Employee Name is:"+name);
      }
      public void disp(int sal,int age)
      {
             System.out.println("Employee Salary is:"+sal+"\n"+"Employee Age is:"+age);
      }
      public static void main(String args[])
      {
             Employeee=new Employee();
             e.disp(1,"Seeta");
             e.disp(5000,20);
      }
}