OOP'S Concept in Java:-

0
OOP'S:-

OOP'S means Object oriented programming structure ,it is used to create real world based application using set of rules and features.

 Rules                                                          Features

 Class and object                                        All Features

 Data abstraction                                        Accessibility,Security

 Data Encapsulation                                    Security

  Polymorphism                                          Usability using overloading                                         
                                                                    Extend-ability or up-gradation using Overriding

 Inheritance                                                 Re-usability

 Object                                                        Dynamic memory allocation

Static Component                                       Common memory or Shared memory.

Constructor                                                 Initialization of member

Properties                                                    Data hiding

....................................................................................................................................

what is class?
........................................................................................

It is used to define characteristics of object using data member and member function.

We can define single object definition or multiple objects definition using single class where all objects should be related with class.

class is also called user define datatype which can contain definition of similar type of objects.


for example Electronic class can contain multiple electronic object definition,Animal class can contain animal definition.


package packagename;

class Classname
{
     datamember;
     member function;
   
}

Object:-   It is real world entity which has identity ,state (memory) and behavior.

Object will be defined by class using member function.

Object attribute will be declared as a data member.



import java.util.Scanner;
class Student
{
    Scanner sc = new Scanner(System.in);
    int rno;
    String sname;
    void accept()
    {
       System.out.println("enter rno");
       rno=sc.nextInt();
       System.out.println("enter name");
       sname=sc.next();
 
    }

    void display()
    {
       System.out.println("rno is "+ rno + " name is "+ sname);

    }


}

class Studentmain
{
   public static void main(String args[])
   {
        Student obj = new Student();
        obj.accept();
        obj.display();
        Student obj1 = new Student();
        obj1.accept();
        obj1.display();
        Student obj2 = new Student();
        obj2.accept();
        obj2.display();
 
   
           
   }

}



Assignment:-

WAP to calculate simple interest using three different method accept() ,logic() and display()?

WAP to sort array using three different method()?

Solution:-

import java.util.Scanner;
class Arr
{
   //int arr[] = {5,3,2,4,1,9};
   int arr[];
    Scanner sc = new Scanner(System.in);
   void accept()
   {
      int size;
      System.out.println("enter size");
      size=sc.nextInt();
      arr = new int[size];
       for(int i=0;i<arr.length;i++)
       {
               System.out.println("enter element for "+i+" index");
               arr[i]=sc.nextInt();

       
       }


   }

   void logic()
   {
       for(int i=0;i<arr.length;i++)
       {
          for(int j=i+1;j<arr.length;j++)
          {
                if(arr[i]>arr[j])
                {
                   int temp = arr[i];
                   arr[i]=arr[j];
                   arr[j]=temp;

                }
          }

       }

   }

   void display()
   {
       for(int a:arr)
        {
             System.out.println(a);
        }

   }


}

class Arrmain
{
    public static void main(String args[])
    {
        Arr obj = new Arr();
        obj.accept();
        obj.logic();
        obj.display();


    }

}

WAP to manage employee information using empid,empname,job ,salary and find max salary in all employees?



Component of class:-

1 ) Data member or variable:-

   It is used to define attribute of object using variable or constant.

   type of data member:-

1 Instance or Dynamic member:-

   It will allocate memory at run-time using object.  we can allocate multiple memory allocation for
   Dynamic data member.

   we will always define dynamic data member under dynamic member function.

    Dynamic variable can be declare under class without any modifier.
   class Classname
   {
          Access Specifier Datatype Identifier;
          private int x;   //dynamic
          void fun()
         { 
               int y=10;   //local dynamic

         }

   }



2 Static or Class type:-

     it will allocate single memory allocation at compile time ,we can not re-create memory for static
     data member.

     it will be declare under class using static modifier .
     it can be declared under static member function directly.
 
     class A
    {
         private static int x;
         static void fun()
        {
               int y=10;
        }

   }

if we want to access static to another class  then we can use Classname to access static no need to create object.


Example of Static and Dynamic data member

class Datamember
{
   int x,y;
   static int a,b;

}

class Datamain
{
   public static void main(String args[])
   {
        System.out.println(Datamember.a+","+Datamember.b);

   }

}



Static Example with dynamic memory:-

class Datamember
{
   int x,y;
   static int a,b;

}

class Datamain
{
   public static void main(String args[])
   {
     
        Datamember obj = new Datamember();
        obj.a=100;
        obj.b=200;
        Datamember obj1 = new Datamember();
        obj1.a=2;
        obj1.b=3;
        System.out.println(Datamember.a+","+Datamember.b);
        System.out.println(obj.a+","+obj.b);

   }

}


Example of Dynamic:-

class Datamember
{
   int x,y;
   static int a,b;

}

class Datamain
{
   public static void main(String args[])
   {
     
        Datamember obj = new Datamember();
        obj.x=100;
        obj.y=200;
        Datamember obj1 = new Datamember();
        obj1.x=2;
        obj1.y=3;
        System.out.println(obj1.x+","+obj1.y);
        System.out.println(obj.x+","+obj.y);

   }

}


2 ) Member function or method:-
Member Function:-  It is used to define data member of class.data member should not be accessed directly .it will be defined under member function.

Type of Member Function:-

1 Static Member Function or Class Type Member Function:-

It is used to define static data member of class ,it will be called by Classname.

class A
{
    private static int a,b;
    public static void addition()
    {
         a=100;
         b=200;
         System.out.println(a+b);
    }
     
}
A.addition();  //


2 Dynamic or Instance Type Member Function:-
  It is used to define dynamic data member of class,Dynamic member function will be called by Object.

class A
{
    private int a,b;
    void addition()
    {
        a=10;
        b=20;
        System.out.println(a+b);
    }

}

A obj = new A();
obj.addition();  



Example of Member Function using Program:-

class MemberFunction
{
   static int a,b;
   int x,y;
   static void addition()
   {
       a=10;
       b=2;
       System.out.println(a+b);
   }
   void substraction()
   {
       x=100;
       y=20;
       System.out.println(x-y);

   } 



}

class MemberMain
{
    public static void main(String args[])
    {
        MemberFunction.addition();
        MemberFunction obj = new MemberFunction();
        obj.substraction();
   
    }

}


Example of Static to static ,Static to dynamic,dynamic to dynamic and dynamic to static:-

class MemberFunction
{
   static int a,b;
   int x,y;
   static void addition()
   {
       a=10;
       b=2;
       MemberFunction obj1= new MemberFunction();
       obj1.x=100;
       obj1.y=20;
       System.out.println(a+b);
       System.out.println(obj1.x+obj1.y);
   }
   void substraction()
   {
       x=100;
       y=20;
       a=1000;
       MemberFunction.b=2000;
       System.out.println(x-y);
       System.out.println(a+b);

   } 



}

class MemberMain
{
    public static void main(String args[])
    {
        MemberFunction.addition();
        MemberFunction obj = new MemberFunction();
        obj.substraction();
   
    }

}

3) Constructor:-

   It is used to initialize  dynamic data member of class when we create object.
   It will be called implicit and no need to declare return type under constructor block.
   It will return address of Object.

   Constructor name and class name will be same.
   By default constructor block will be created after program compilation ,it is called default              constructor.

  class A
  {


  }

After Compilation

 class A
{
    static
    {

     }
     A()
    {

     }

}


Type of Constructor :-

1  Default:-

  It will be created implicit when we compile program code.



2 Parametrised:-

   We can pass value from object which will be mapped by parameters.

3 Copy or reference type:-

   We can copy the address of one object to another ,it will work similar to call by reference in c        language.

    class A
    {
        int a,b;
         A()
        {
            a=10;
            b=20;
         }
        A(A o)
        {
           a = o.a;
           b= o.b;
        }

    }
   A obj = new A();
  A obj1 = new A(obj);


Complete Program  of Constructor:-

class Cons
{

   private int a,b;
   Cons()
   {
       a=10;
       b=20;
     
   }

   Cons(int a,int b)
   {
      this.a=a;
      this.b=b;
     
   }
   Cons(Cons o)
   {
       this.a=o.a;
       this.b=o.b;
     
   }
   void displayAddition()
   {
       System.out.println("result is "+(a+b));
   } 
 
}

class ConsMain
{
    public static void main(String args[])
    {
        Cons obj = new Cons();
        Cons obj1 = new Cons(2,3);
        Cons obj2 = new Cons(obj);
        obj2=obj1;
       
         obj.displayAddition();
         obj1.displayAddition();
         obj2.displayAddition();
     
     


    }

}


4) Static Block:-

   It is used to initialize static data member of class ,this block will be called before main().


  Program explanation of Static:-

class A
{

     static int a,b;
      static

     {

         System.out.println("static first");
         a=10;

         b=20;

     }
   

    public static void main(String args[])
     {
         System.out.println("main");
         System.out.println(a+b);
     }

    static

     {

         System.out.println("static");
         a=100;

         b=200;

     }

}

...................................................................





5) initializer block :-

    this block will be called before constructor means if we want to initialize dynamic data member before constructor execution .



Example of Static,Constructor,Init,Main():-


class A
{
     int a1,b1;
     A()
     {
       System.out.println("constructor");
       System.out.println("addition is "+(a1+b1));

     }
   
     {
         System.out.println("init");
         a1=11;
         b1=12;

     }



     static int a,b;
      static

     {

         System.out.println("static first");
         a=10;

         b=20;

     }
   

    public static void main(String args[])
     {
         System.out.println("main");
         System.out.println(a+b);
         A obj = new A();

     }

    static

     {

         System.out.println("static");
         a=100;

         b=200;

     }

}





6) Properties

7 Inner Class


2) Data abstraction & Data encapsulation:-

Data abstraction :-

To hide internal functionality of the program is managed by data abstraction.

Program contain all features of object using this we can show only essential details and hide non-essential details.

It is used to perform security and accessibility option in program.


How many ways to implement data abstraction in java?

1 using Access Specifier 


2 using Abstract class and Abstract method


3  using Interface



Data encapsulation:-

It is used to encapsulate data member and non-accessible member function into accessible member function.
means we can say that it is used to bind all details of class in a single unit for accessibility.




import java.util.Scanner;
class Bank
{
    private int balance;
    Scanner sc = new Scanner(System.in);
    Bank()
    {
        balance=5000;
    } 
    private void credit(int amt)
    {
         balance+=amt;

    }
     private void debit(int amt)
    {
         balance-=amt;

    }
    private void checkbalance()
    {
        System.out.println("Balance is "+balance);

    }  
    private int accept()
    {
        System.out.println("enter amount");
        return sc.nextInt();
     }

    public void login(int pin)
    {
        if(pin==1234)
         {
             while(true)
             {
                 System.out.println("press c for credit");
                 System.out.println("press d for credit");
                 System.out.println("press b for balance"); 
                 System.out.println("press e for balance");
                 char ch = sc.next().charAt(0);
                 switch(ch)
                 {
                     case 'c':
                     credit(accept());
                     break;
                     case 'd':
                     debit(accept());
                     break;
                     case 'b':
                     checkbalance();
                     break;  
                     case 'e':
                     System.exit(0);
                     break;   
                     default:
                     System.out.println("enter c,d and b");
                     break;

                 } 
                  
                 
             }

         }
         else
         {
             System.out.println("invalid inpincode");
         }
          

    }

  
}

class Customer
{
    public static void main(String args[])
    {
         Bank obj = new Bank();
         System.out.println("enter pin code");
         int pin=obj.sc.nextInt();
         obj.login(pin);

     }

}








Polymorphism:-

Poly means many and morph-ism means forms ,it is used to provide single entity for multiple usability .

using this we can create same name method and operator which can be used into multiple ways in single program.


Java only support polymorphism concept based on method not by operator .operator can not be redefine because it will be managed by runtime register.


Type of Polymorphism:-

1)  Static polymorphism or Compile Time Polymorphism or  Early binding:-

  Function overloading is the implementation of Static Polymorphism .it will perform operation at compile time hence it is also called compile time or early binding process.

  what is Function Overloading:-

   We will create same name method only number of parameters can be different.

Parameter can be different based on number of parameters for same type and sequence of parameters for different type.

return type and agrument can  not define Function Overloading ,it will be managed by parameters only.

void addition(int a[],int b[])
{

}
void addition(int a,int b)
  {

  }
void addition(int a,float b)
  {

  }
 void addition(float a,int b)
  {

  }

 int addition(float m,float n)
  {

  }
int addition(int x,int y)   //wrong 
 {


  }

addition(10,20);







2)  Dynamic polymorphism or RunTime Polymorphism or  Later binding:-

     It will perform operation at runtime,function overriding is the best example of runtime polymorphism.


Inheritance is required for Dynamic polymorphism.


5 Inheritance:-

    It is used to perform reusebility of code from base class to derived class.
    We can reduce common code from application modules.
   
     Type of Inheritance:-

      5.1)  Single  :-   Base class to derived class
     class A
     {

    }
    class B extends A
    {

   }

    
  

      5.2) Multilevel :-   Base class to derived class to sub derived .......

       class A
     {

    }
    class B extends A
    {

   }

    class C extends B
   {

   }
   
      5.3) Hierarchical:-    Base class to multiple derived class to multiple subderived ....

         class A
     {

    }
    class B extends A
    {

   }
   class C extends A
   {

   }
   



      













               
























Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)