Wednesday, March 25, 2015

Object Oriented Programming

     





                                                          ("OOP Concepts");




1.What is OOP?
* OOP Mean object oriented programming
*OOP is Based On Object
*In OOP Class File will Collection Of Objects.

2.What are the different between class and object?

       Class (Computer) Example             
                                                                       Object(Computer) Example
           (1)Brand Name (DELL)             
               RAM Size (4GB)                                                  
               Processor Type(i5)                                         
               OS Name (Windows)                                                  


Many Object can be said to same type or class EXAMPLE:- MY COLLEGE, YOUR COLLEGE, Ru_Ban's COLLEGE.





What Does Come First?Class OR Object?
Example with Answer
John, Raj, Ruban, Peter those are Persons but we commonly we call those are Name's
so first come with CLASS.


There are four Type  Principals Of OOP

1. Encapsulation
2. Inheritance
3. polymorphism
4. Abstraction




                                               Encapsulation
Encapsulation is like a money locker we hiding the money we know what is  there but we hiding.


  • Its Safe resources of an object form out side interface
  •  it's works like a wrappers
  •  it's the mechanism that hide complexly .


  •   The main benefit of encapsulation is the ability to modify our implemented code without  breaking the code of others who   use our code





Example-1


Example-2


Example:-3
public class EncapsulationDemo{
    private int ssn;
    private String empName;
    private int empAge;

    //Getter and Setter methods
    public int getEmpSSN(){
        return ssn;
    }

    public String getEmpName(){
        return empName;
    }

    public int getEmpAge(){
        return empAge;
    }

    public void setEmpAge(int newValue){
        empAge = newValue;
    }

    public void setEmpName(String newValue){
        empName = newValue;
    }

    public void setEmpSSN(int newValue){
        ssn = newValue;
    }
}
public class EncapsTest{
    public static void main(String args[]){
         EncapsulationDemo obj = new EncapsulationDemo();
         obj.setEmpName("Mario");
         obj.setEmpAge(32);
         obj.setEmpSSN(112233);
         System.out.println("Employee Name: " + obj.getEmpName());
         System.out.println("Employee SSN: " + obj.getEmpSSN());
         System.out.println("Employee Age: " + obj.getEmpAge());
    } 
}
Output
Employee Name: Mario
Employee SSN: 112233
Employee Age: 32

                                   Inheritance


  • Inheritance can be defined the process where one object access the properties of another.
  •  inheritance based on their parents and they also derived some common things from them.
  •  simply as i can give example easy to understand. C is a programming language then C++ and java. C is first programming language C++, java it's kind a like common things their.



  •   Inheritance like a relationship or clan.
  • Animal is a Class Dog and Monkey is a sub_class its like a inheritance.

 *  Inheritance can be classified  5 types     
          1.Single  Inheritance
          2.hierarchical  Inheritance
          3. Multi level Inheritance
          4. Hybrid  Inheritance
EXAMPLE-2
          5. Multiple Inheritance

EXAMPLE 1
Example:-3
The keyword used for inheritance is extends. Syntax:
public class ChildClass extends BaseClass  {
   // derived class methods extend and possibly override
}
// A class to display the attributes of the vehicle
class Vehicle {
   String color;
   int speed;
   int size;
   void attributes() {
      System.out.println("Color : " + color);
      System.out.println("Speed : " + speed);
      System.out.println("Size : " + size);
   }
}

// A subclass which extends for vehicle
class Car extends Vehicle {
   int CC;
   int gears;
   void attributescar() {
      // The subclass refers to the members of the superclass
      System.out.println("Color of Car : " + color);
      System.out.println("Speed of Car : " + speed);
      System.out.println("Size of Car : " + size);
      System.out.println("CC of Car : " + CC);
      System.out.println("No of gears of Car : " + gears);
   }
}
public class Test {
   public static void main(String args[]) {
      Car b1 = new Car();
      b1.color = "Blue";
      b1.speed = 200 ;
      b1.size = 22;
      b1.CC = 1000;
      b1.gears = 5;
      b1.attributescar();
   }
}
Output
Color of Car : Blue
Speed of Car : 200
Size of Car : 22
CC of Car : 1000
No of gears of Car : 5


Polymorphism


  •  poly mean many morph mean form.
  • same symbol to  be interpreted correctly in different situation based on the context
  • polymorphism allows you define one interface and have multiple implementations
  • one thing use many purpose



                                                                  Example-1



There are two types of polymorphism 

1/ compile time polymorphism(overloading)
2/ Run time polymorphism(overriding)




                                         

                       overloading

  • in java have two types of polymorphism overloading is one of the important part 
  • multiple methods with same name but different signature
  • it can have a different modifiers
  •  overloading method can throw different exceptions
  •  May have the same or different return types
Example

class Overload
{
    void demo (int a)
    {
       System.out.println ("a: " + a);
    }
    void demo (int a, int b)
    {
       System.out.println ("a and b: " + a + "," + b);
    }
    double demo(double a) {
       System.out.println("double a: " + a);
       return a*a;
    }
}
class MethodOverloading
{
    public static void main (String args [])
    {
        Overload Obj = new Overload();
        double result;
        Obj .demo(10);
        Obj .demo(10, 20);
        result = Obj .demo(5.5);
        System.out.println("O/P : " + result);
    }
}
  •  In this code method is overloaded 3 times first having 1 int parameter, second one has 2 int parameters and third one is having double argument                           

Output
a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25
                                            

                              overriding
  • Method overriding is used to provide specific implementation of a method that is already provided by its superclass.
  • It is also known as Runtime polymorphism.
  • Abstract methods must be overridden
  • Constructors cannot be overridden
Example
lass Human{
   public void eat()
   {
      System.out.println("Human is eating");
   }
}
class Boy extends Human{
   public void eat(){
      System.out.println("Boy is eating");
   }
   public static void main( String args[]) {
      Boy obj = new Boy();
      obj.eat();
   }
}

Output
Boy is eating



Abstraction
  • Abstraction means to show only the necessary details to the client of the object.
  • hiding internal details and showing the functionality.
  • we know what is that but we cannot say exactly is that
  • in java we use abstract class and interface to acheive abstraction.
  • You can create an abstract class using the abstract keyword.
  • example:- do you know what happen inside the mobile phone? no because they hiding the thinks it will show only necessary think.
* Necessary things mean we should to know before start a car

1. Name of Car
2. Color of Car
3. Steering
4. Rear View Mirror
5. Brakes
6. Gear
7.Car seat

Unnecessary things mean  not  compulsary to know for Car rider

1. Internal Details of a Car
2. Car Engine
3. Diesel Engine
4. Silencer

Example

public class Car

    private string _nameofcar = "My Car";
    private string _colorofcar = "Red";


    public string NameofCar


// Here used access modifier PUBLIC

        set

         

            _nameofcar = value;

        }

        get

         

            return _nameofcar;

        }

    }


    public string ColorofCar

     

        set

         

            _colorofcar = value;

        }

        get

         

            return _colorofcar;

        }

    }


    public void Steering()

     

        Console.WriteLine("Streering of Car");

    }


    public void RearViewMirror()

     

        Console.WriteLine("RearViewMirror of Car");

    }


    public void Brakes()

     

        Console.WriteLine("Brakes of Car");

    }

    public void Gear()

     

        Console.WriteLine("Gear of Car");

    }



    private void InternalDetailsofCar()
//Here Private use for hiding

     

        Console.WriteLine("InternalDetailsofCar of Car");

    }


    private void CarEngine()



        Console.WriteLine("CarEngine of Car");

    }



    private void DiesalEngine()

     

        Console.WriteLine("DiesalEngine of Car");

    }


    private void ExhaustSystem()

     

        Console.WriteLine("ExhaustSystem of Car");

    }


    private void Silencer()

     

        Console.WriteLine("Silencer of Car");

    }



}




https://www.blogger.com/blogger.g?blogID=802917879027693758#editor/target=post;postID=529702529841438832;onPublishedMenu=posts;onClosedMenu=posts;postNum=0;src=postname

No comments:

Post a Comment