Thursday 6 February 2014

Object Oriented Concepts

Below are main features of object oriented programming language :--

1. Class : 

Class is a programming language construct / a template that describes the data and the behavior associated with an instance of that class. Class act as a blueprint /specification to create objects.

Class contains the attributes and methods that all the created objects of that class share.

Fundamentally, class encapsulates the state and behavior of the objects. Class encapsulates the state through data placeholders called member variables and behavior is encapsulated through methods. 

Thus member variables value define the state of the object and methods defines the behavior of the class.

 UML class diagram:-



    

General Form of the class:

Class Class_Name
{
      //Properties/Variables

    // Methods
}

For Example :--

Class Person
{
      private String firstName; //Variable to hold the firstName of the Person class object
    
      private String lastName;  //Variable to hold the lastName of the Person class object

     public void display()
     {
            System.out.println("Displaying person name");  //Method defines the behavior of class
      }



  
2. Object :

An object is an instance of the class which represents a real time entity. (Instance means physically happening).

Object is created based on the class definition. An object has some properties and it can perform some actions.

In Java an object of class is created using new operator. For example :   

Person p =  new Person();  // Here an instance of Person class is being created.


3. Encapsulation :

Wrapping up / bundling up data (variables) and methods  in to  a single unit is called Encapsulation. Class is a an example of encapsulation which  holds the variables and function together as a single entity.

Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Encapsulation is the technique of making the fields in a class private and providing access to the fields via methods. If a field is declared private, it cannot be accessed by anyone outside the class.   

Note : Making class variables/fields private and  providing access to fields through methods is not Information Hiding.  

Defining a class with fields and functions is an example of encapsulation.

class Student
{
       private int rollNo;
      private String name;

     void display ()
     {
             System.out.println ("Student Roll Number is: " + rollNo);
             System.out.println ("Student Name is: " + name);
     }
}


4. Abstraction :

Abstraction is a phenomenon of providing essential features without inner details or hiding internal implementation is called Abstraction.  

Abstraction helps in enhancing internal implementation details without affecting outside world and it also provides security. 

A class might contain lots of data and a user might not interested in entire data. So methods in a class can be implemented in such as way that user can do it's processing passing required data as input and without knowing internal details of the method and unnecessary data. For example :--

A bank clerk should see the customer details like account number, name and balance amount in the account. He should not be entitled to see the sensitive data like the staff salaries, profit or loss of the bank etc. So such data can be abstracted from the clerks view.    

class Bank
{
      private int accno;
      private String name;
      private float balance;
      private float profit;
      private float loan;

     void display_to_clerk ()
    {
         System.out.println ("Accno = " + accno);
         System.out.println ("Name = " + name);
         System.out.println ("Balance = " + balance);
    }
}

In the preceding class, inspite of several data items, the display_to_clerk () method is able to access and display only the accno, name and balance values. It cannot access profit and loan of the customer. This means the profit and loan data is hidden from the view of the bank clerk.


5.  Inheritance :

It is a phenomenon by which a class inherit the behavior and data from another class.
                  Or
It is a phenomenon of producing a new class from already existing class is called Inheritance.

Note : -- Private member variables and methods are not inherited by child class.  

UML inheritance diagram :



In Java, extends keyword is used used to implement inheritance.  Inheritance example :--

class Person()
{
      private String firstName;
      private String lastName;

     public void display()
   {
         ---------
         ------
    }
}

class Employee extends Person      /* Here Employee class inherits from  Person class */ 
{
         ------
        -------     
  //private member variables  firstName & lastName from Person class  will  not be inherited      
 //by Employee class but display() method is inherited
}

class Customer extends Person   /* Here Customer class inherits from  Person class */ 
{
 ---------
  --------
  //private member variables  firstName & lastName from Person class  will  not be inherited       
  //by Customer class but display() method is inherited

}

6. Polymorphism :

The word polymorphism came from two Greek words ‘poly’ means ‘many’ and ‘morphos’ means ‘forms’. Thus, polymorphism represents the ability to assume several different forms. The ability to define more than one function with the same name is called Polymorphism.

Method overloading is a common example of polymorphism.

e.g.: int add (int a, int b);
       float add (float a, int b);
       float add (int a , float b);
       void add (float a);
       int add (int a);

 7. Message Passing :

Invoking a method in class is known as message passing. In Java, any public method of  a class is  invoked by using class object with . (dot) operator as:--

Person p1 =  new Person(); //Creating an instance of the class Person

p1.display(); // Invoking the display() method of Person class on p1 instance




No comments:

Post a Comment