Search This Blog

Oops Concept


            Oops stand for Object-oriented programming, which represents concept of Objects. It is used to for designing modular, reusable software systems. Object-orientation is simply the logical extension of older programming techniques such as structured programming. An object has state (data) and behavior (code). Objects correspond to things found in the real world.

What is Object?
Object is base of object oriented programming concept.In real world we are surrounded by objects like desk, dog, cycle, mobile etc.In real world objects does have two characteristic, state  and  behavior.
The real-world observations all translate into the world of object-oriented programming. Software objects also are similar to real world objects and they too consist of state and related behavior.Software objects keep their state in variable and expose their behavior through methods.By attributing state and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it.
Below are the few properties of Object oriented programming

Encapsulation
Encapsulation is a technique which is used to prevent the data. Encapsulation is combining the data (information) and the methods (functions) that can manipulate that data into one capsule (class/object). Depending on how you write program, encapsulation guarantees that the encapsulated data is not accessed by any other function/method/program outside the encapsulated object.To achieve perfect encapsulation define all the variables private and methods public in class.In the below example rollNo  and name are accessible only through methods declared in class.

Public Encapsulation {
            Private int rollNo=10;
            Private String name=”John”;
         
             public getRollNo(){
               //implementation of code}
             public getName(){
                 //implementation of code}
      }

Inheritance
          Different kinds of objects often have a certain amount in common with each other.Lets understand the above said statement through an example.Consider mobiles of Apple, Blackberry, Samsung all the three have common functionality of making calls , sending text etc even though they have different operating system and way of functioning. All the three share the characteristics of being a mobile yet each also defines additional features that make them different e.g  Samsung's smart scroll, Apple's facetime, blackberry's BBM :). 
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example "Mobile" class would behave as superclass and Apple, Samsung and Blackberry classes will inherit common behavior  from Mobile class and will behave as child class. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses.
Polymorphism
  Polymorphism in Java is similar to its literal meaning which is "many forms"(poly --> many, morphs--> forms).In biology polymorphism is used to point out those species or organism which can have many different states or forms, Similarly in Java the the childclass or the subclass of class can have its own unique functionality (method implementation) and at the same time it can have the common behavior from the parent class, So polymorphism is ability of any object to take many different forms.

There are two types of polymorphism

1) Static or compile-time polymorphism (Method Overloading)
    Static polymorphism can be achieved through Method overloading. Method overloading can be achieved when in any class we use same method many time but with different order/types/number of parameter.Java knows which method would be invoked at compile time only, So its called compile time polymorphism.
             
                class maths{
                       public int sum(){//code}
                       public int sum(int a){//code}
                       public int sum(int a, int b){//code}
                       public int sum(String abc, int a){//code}
                       }

2) Dynamic or run-time polymorphism (Method Overriding)
     To understand dynamic polymorphism lets consider an example where class A is super class with implementation of method "toMove" .Class B is a child class of class A (B extends A) and have its own implementation of  "toMove" method i.e. called method overriding . From calling class if we create object of class B and assign it to reference variable of class A and try to access toMove method it will go to Class B and this decision happens only at run time and thats why called dynamic polymorphism.

Abstraction
Abstraction is the process of hiding the details and exposing only the essential features of a particular concept or object. 
          

No comments:

Post a Comment