Java OOP Concepts – Basics of OOP

મિત્રો, આ Tutorial માં આપણે શીખીશું Basics of OOP – OOP ના બેઝિકસ concepts (Object-Oriented Programming, OOP Concepts, Java OOPs, Java OOP Concepts, Object Oriented Programming Concepts in Java, OOP in Java).

હવે તમે C++ માં Class, Object, Polymorphism વગેરે જેવા concepts જોયા હશે આ દરેક concepts એ OOP ના basic concepts છે. તો ચાલો આજે ફરી એક વાર એજ concepts ને શીખીએ આપણી સરળ ભાષા ગુજરાતી માં.

Java OOP Concepts – Basics of OOP

OOP માં ઘણા બધા concepts present છે જે તેને વધારે powerful, secure, reliable અને easy બનાવે છે.

OOP Concepts:

  • Object
  • Class
  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism
  • Message passing
  • Overloading
Java OOP Concepts - Basics of OOP
Java OOP Concepts

Object

  • Object એ class નો instance હોય છે. જે variable ની જગ્યાએ original value ને contain કરે છે.
  • Object એ એક basic run time entity છે.
  • Object એ દરેક વસ્તુઓ હોય છે જેને આપણે ઓળખી શકીએ. આપણા આસપાસ ની બધી વસ્તુઓ જેમ કે pen, book, car, etc બધા objects છે.
  • દરેક object ને એક unique identity હોય છે.
  • Object એ program નો component છે જે જાણે છે કે કેવી રીતે program ના બીજા ભાગો સાથે interact કરવું.
  • Object એ type class નો variable છે.
  • Object એક basic run time entity છે.
  • એક class માં એક થી વધારે objects પણ હોઈ શકે છે.   
  • Object create થઈ ગયા બાદ તે memory માં variable જેટલી જ space લે છે.

Example:
Object: Mobile
State: Name, Model Number, Color
Behavior: Calling, messaging, switch ON, switch OFF etc.

Class

  • Objects ના collection ને Class કહેવાય છે.
  • જેમ કે mouse, keyboard, monitor આ બધા computer ના parts છે અને તે computer class ના member છે.
  • Class એ user defined data type હોય છે અને class data તથા functions નો group છે.
  • Class એ template છે જે objects ના behavior અને attributes ને specify કરે છે.
  • Class એ blueprint અથવા prototype છે જેમાંથી objects create થાય છે.
  • Class એ abstract data type [ADT] નું implementation છે.
  • તે attributes અને methods ને define કરે છે જે data structure અને ADT ના operations ને implement કરે છે.
  • Class space consume કરતો નથી.

Example:

class Maricollege // Class
{
      int id = 10;// data member અથવા instance variable પણ કહેવાય.
      String name = "Mari College";
      public static void main(String args[])
      {
            Maricollege M1 = new Maricollege(); //Maricollege Class નો Object Create કર્યો.
            System.out.println(M1.id); //Reference variable થી member ને access કર્યો.
            System.out.println(M1.name);
      }
}

Output:
10
Mari College

Encapsulation

  • Data અને functions ને એક unit માં સાથે જોડવાને [wrap] કરવાને encapsulation કહે છે.
  • Data એ બહારના world માટે accessible હોતો નથી, માત્ર તેઓજ function ને access કરી શકે છે જે single unit માં એકસાથે wrap કરેલ હોય.
  • આમાં class ના variable private હોય છે અને તેને class ની બહાર access કરી શકાય નહીં.
  • Encapsulation ને class ના રૂપ માં use કરવામાં આવે છે.
  • એક class માં આપણે data અને method ને એક unit ના રૂપમાં એકસાથે રાખી શકીએ છીએ.
  • Java bean પૂરી રીતે એક encapsulated class છે કારણકે તેમાં બધા data members private હોય છે.

Abstraction

  • Data abstraction નો મુખ્ય ઉદ્દેશ એ છે કે એવા object ને choose કરવું જે program માટે જરૂરી છે અને જે objects program માટે જરૂરી નથી તેને ફ્રી કરી દેવા.
  • આ data security provide કરવા માટે class માં implement થાય છે.
  • આપણે abstraction મેળવવા માટે abstract class અને interface નો ઉપયોગ કરીએ છીએ.
  • Abstraction એ background details add કર્યા વગર જરૂરી features ને represent કરવાના act ને refer કરે છે.
  • Abstraction માં user થી અમુક details છુપાવવામાં આવે છે અને જે જરૂરી અને simple હોય તેવી information જ બતાવવામાં આવે છે. આ process ને abstraction class દ્વારા કરવામાં આવે છે.

Inheritance

  • Inheritance એટલે વિરાસત માં મળેલ.
  • Old class માંથી નવા class ને મેળવવાના mechanism ને inheritance કહે છે.
  • Inheritance એ એક process છે જેમાં એક class બીજા class ની properties ને મેળવે [acquire કરે] છે.
  • જૂનો class એટલે કે old class એ super class અને નવો class એટલે કે new class એ sub class તરીકે ઓળખાય છે.
  • OOP માં inheritance નો concept એ reusability નો concept provide કરે છે.
  • આનો મતલબ એ છે કે આપણે existing class ને એટલે કે already બનાવેલ class ને modify કર્યા વગર તેમાં વધારે information add કરી શકીએ છીએ.
  • આનો ઉપયોગ runtime polymorphism ને મેળવવા માટે થાય છે.
Inheritance

Polymorphism

  • Polymorphism જેમાં poly એટલે many અને morphism એટલે forms.
  • તો polymorphism નો અર્થ many forms થાય છે.
  • એક કામ ને બે અલગ રીતે કરવું એટલે polymorphism.
  • એક structure જેમાં data items, functions, classes હોય તેવું અને operator numerical addition અને string ને જોડવા માટે use થાય છે.
  • તે single નામ ને એક થી વધારે purpose માં use કરવા માટે allow કરે છે.
  • તેનો મતલબ છે કે functions અને operators ની ability ને જુદી જુદી situations માં જુદી જુદી રીતે work કરાવવું.
  • આપણે polymorphism ને મેળવવા માટે method overloading અને method overriding નો ઉપયોગ કરીએ છીએ.

જાવામાં 2 types ના polymorphism છે:

  1. Compile-time polymorphism
  2. Run-time polymorphism

1. Compile-time polymorphism

  • Compile time polymorphism ને આપણે method overloading અથવા early binding પણ કહીએ છીએ.
  • આ polymorphism નો અર્થ એ છે કે આપણે same નામની method ને જુદી જુદી રીતે declare કરી શકીએ છીએ કારણકે જુદા જુદા task ને એક જ method નામ સાથે perform કરીએ છીએ.  

2. Run-time polymorphism

  • આ પ્રકાર ના polymorphism ને late binding અથવા dynamic binding અથવા method overriding કહે છે.
  • આનો અર્થ એ છે કે આપણે same નામ ની method ને same signature ની સાથે declare કરીએ છીએ.  

Message passing

  • Objects information ને send અને receive કરી એકબીજા સાથે communicate કરે છે.
  • જેમ આપણે લોકો message માં એક બીજા સાથે વાતો કરીએ છીએ તેવી જ રીતે objects પણ information દ્વારા વાતો કરે છે.
  • Communicate કરવાના basic steps:
  • Classes create કરો જે objects અને તેમના behavior ને define કરે.
  • Class definition માંથી objects create કરો.
  • Objects ના વચ્ચે communication establish કરો.

Overloading

  • Single method નામ ને જુદા type ના task ને perform કરવા માટે ની મેથડ ને overloading કહે છે.
  • Overloading એ polymorphism નો type છે.

Example:

void add(int a, int b);
void add(float a, float b);
  • અહીં, પેહલા add function માં બે integer number છે અને બીજા add function માં બે float number છે.

આ પણ વાંચો – Difference between POP and OOP

આ પોસ્ટ જેમાં મિત્રો આપણે જોયું કે Basics of OOP – OOP ના બેઝિકસ concepts (Object-Oriented Programming, OOP Concepts, Java OOPs, Java OOP Concepts, Java OOP Concepts, Object Oriented Programming Concepts in Java, OOP in Java).

જો મિત્રો તમને આ પોસ્ટ મદદરૂપ લાગી હોય, તો તમે તમારા મિત્રો સાથે ચોક્કસ શેર કરો અને જો તમને જાવા અથવા બીજા કોઈ વિષય સંબંધિત કોઈ પ્રશ્ન હોય તો તમે નીચે કોમેન્ટ કરીને અમને જણાવી શકો છો. આભાર.

Leave a Comment