Wednesday, May 29, 2013

Object Oriented Programming In JAVA

OOP stands for Object Oriented Programming. This is a technique used to create programs around the real world entities. In OOPs programming model, programs are developed around objects and data rather than actions and logics. In OOPs, every real life object has properties and behavior. This feature is achieved in java through the class and object creation. They contain properties (variables of some type) and behavior (methods). OOPs provide better flexibility and compatibility for developing large applications.

To say, any language to be an OOP language, it must be satisfying the principles of OOPS. OOPS contains 8 principles. They are:
                        1. Class                                    2. Object
                        3. Data Abstraction      4. Data Encapsulation
                        5. Inheritance               6. Polymorphism
                        7. Dynamic Binding     8. Message Passing
Object Oriented programming concepts are given by OMG i.e. Object Management Group.

1.      Class:
o        A class defines the properties and behavior (variables and methods) that is shared by all its objects. It is a blue print for the creation of objects.
o        Definition: Class is a way of binding the Data Members and Methods in a single unit.
o        Data Members of a class are also known as properties or attributes.
o        Methods of a class are also known as behaviors or accessories or functions.
o   In java, every program must be defined or developed with respect to class only i.e. No java program can be developed without the features of class.
o   In Object-Oriented Programming, we can have two types of methods. They are:
            (a) Member Methods   (b) Non-member Methods
o   Member Methods: A member method is one, which will come under the scope of a class.
o   Non-member Method: A non-member method is one, which will not come under the scope of a class.
o   Java does not allow non-member methods it allows only member-methods.
o   All the methods in java must be defined within the class only, i.e. Java Environment does not allow outside method definition.
o   Whenever we define a class, the data members of the class do not occupy the memory space. They occupy the memory space, when we create an object.
o   Syntax to define a User-defined Data type: Class
class<class-name>
{
            Variable / Data member declaration
            Method definition
}
Explanation:
Ø  A keyword “class” is used for creating/developing user-defined data type.
Ø  <class-name> represents a valid java variable name and it is treated as the name of the class. Class name is basically used to create objects.
Ø  Whenever, we create a class, no memory space is occupied for the data members of the class.
Ø  Class contains two parts:
            (a) Variable Declaration or the Data Members
            (b) Method Definition
Ø  Variables-declaration represent, what type of data members, we use as a part of the class.
Ø  Methods definitions represent what type of methods, we use as a part of the class to perform specific operations.
Ø  Every class must start with opening curly bracket ({) and terminates with closing curly bracket (}).
2.      Object:
o   It is an instance of class i.e. Instantiation is a process of allocating/creating sufficient memory space for the data members of the class.

o   Whenever we create an object, we get sufficient memory space for the data members of the class.

o   Class variable I known as an object.

o   Grouped item is known as object i.e. if any variable is holding more than one value then that variable is known as an object.

o   Logical runtime entity is known as an object.

o   Real world entities are called objects.

o   In order to allocate memory space dynamically, we must follow Dynamic Memory Allocation. To allocate the memory space dynamically, we have an operator called ‘new’. The ‘new’ operator is also known as dynamic memory allocation operator.

o   Object creation:
Syntax:
                  <ClassName> objName = new <ClassName()>;
e.g.:            Student s = new Student ();
·                                                                                             Here, <ClassName> represents the name of the class which is used for creating the object.
·         The ‘objName’ represents the java valid variable name and is treated as object which contains the collection of similar or different type of values.
·         The ‘new’ is a dynamic memory allocation operator which is used for allocating memory space dynamically.
·         The ‘new’ operator has the following functionalities:
o       It allocates sufficient memory space for the data members of the class which we specify.
o       It takes an address of the class and places it to its left hand side variable i.e. objName.
Difference between Class and Object:
Sr#
Class
Object
1.
A class is a way of binding the data members and the associated methods into single unit.
Class variable is known as an object.
2.
Whenever we start execution of a java application, JVM will load the class into the main memory of the computer only once with the help of Class Loader Sub System (a JVM Tool).
After loading the class, we can create n# of objects as per requirements.
3.
When the class is defined, there is no memory space for the data members of the class.
When the object is defined, data members of the class will get the memory space.
4.
In every java application, class will exist only once.
In a java application, there is a possibility of creating either single or multiple objects, depending upon the requirements.
  
  1.  The memory space for the data members of the class will be created in Heap Memory.
  2.  The memory space for the method execution will be created in Stack Memory.
  3.   The memory space for the constants/final variables in java, will be created in Associative Memory.

3.      Data Abstraction:
o   It is a mechanism of retrieving the required/essential information by hiding background details.
o   There are 3 levels of abstraction:
(a)    Physical Level Abstraction: It always deals with physical architecture of the application.

(b)   Logical / Conceptual Level Abstraction: it always deals with the type of data/coding/testing policies, which we follow to develop the application effectively.

(c)    View Level Abstraction: It always deals with the retrieval of the required data without dealing with the entered data and the application’s physical structure.

e.g.: In the development of a software, its planning part comes in physical level abstraction coding come in Physical level abstraction coding comes in logical level abstraction and when we load it on any server, then it can be viewed by everyone, so it comes in view level Abstraction.

4.      Data Encapsulation:
o   Data Encapsulation is a process of wrapping up of data in a single unit.
o   With data encapsulation, we can achieve security to the data (data hiding or information hiding).
o   Whenever, we want to send the data from client to server over the network, it must be sent in the form of Java object only, since by default, the data which is encapsulated with an object is encrypted.

5.      Inheritance:
o   It’s a process of acquiring the features (data members and member methods) of one class into another class.
o   Inheritance process is also known as Extendibility or Reusability or Derivation or Sub-classing.
o   Whichever class is giving its features to another class is known as Base Class or Parent Class or Super Class.
o   Whichever class is acquiring the features from its base class is known as Derived class or Child Class or Sub Class.
o   It is strongly recommended to create an object of bottom most Derived Class by which we can access the features of derived class as well as the features of its base class.

ADVANTAGES:
o   Application Development time is very less.
o   Repetition/Redundancy of the codes will get reduced. Hence, Occupation of memory space will be reduced.
o   Investment cost towards project will be reduced.
o   The performance of overall project will be improved.
TYPES OF INHERITANCE:

(i)  SINGLE INHERITANCE: In this, there exists one Base Class and One Derived Class.


 


(ii)MULTILEVEL INHERITANCE: In this, there exists one Base Class, one derived class and n # Intermediate Base Classes.

Intermediate Base Class is one which acts as a derived class in one context and as a Base Class is some other context.
(iii)  MULTIPLE INHERITANCEs: In this, there exists n # base classes and one derived class.
Java doesn’t support multiple Inheritances to the classes directly but it supports indirectly through the Interface.


(iv)  HIERARCHICAL INHERITANCE: In this, there exists one base class and n # derived class.

 

           
(v)   HYBRID INHERITANCE: It is a combination of two valid Inheritances.



6.      POLYMORPHISM:
o   It is a mechanism to represent one form in many forms.
o   We can have two types of Polymorphism:
o   Static or Compile Time Polymorphism.
o   Dynamic or Run Time Polymorphism.
o   Java supports only Dynamic Polymorphism,
o   To understand the concept of Polymorphism we use a term Dynamic Binding.
o   Dynamic Binding is used to execute Polymorphic applications.
o   Dynamic Binding is used to minimize the memory space occupation.

7.      DYNAMIC BINDING: Dynamic Binding is a way of binding the appropriate versions of derived classes which are inherited from same base class with Base class object.

Consider the following example:


 





 

Now, to call functions of all the 3 classes, we have to make object of each class, as follows:

C1 o1 =new C1();
o1.add();

C2 o2=new C2();
o2.add();

C3 o3=new C3();
o3.add();

Here, the “new” keyword has 2 functionalities:
o   It helps to refer/saves/initializes the address of its RHS class to its LHS variable.
o   It allocates sufficient memory space for the data members of the class.

To minimize the memory space & interface efficiency, we use the concept of dynamic binding as follows:

(1)   o1= new C1();
(2)   o1.add();
(3)   o1=new C2();
(4)   o1 add();
(5)   o1=new C3();
(6)   o1.add();

    • In this example, we can see in line #1, #3 and #5, the object o1 is an object of all the three classes C1, C2 and C3 respectively. So, this type of object is known as Polymorphic Object.

    • In Line #2, #4 and #6, the statement “o1.add()” seems similar, but in every line the statement is executing from different scopes and having different results. Such types of statements are known as Polymorphic statements.

    • In Line #2, #4 and #6, add () is polymorphic method.

8.      MESSAGE PASSING:
o   Message passing is a process of exchanging the data between multiple objects either locally or globally.
o   Message passing is a process of exchanging the data between two remote objects/machines, for a period of time across the network for generating multiple requests for obtaining multiple responses either locally or globally for performing a meaningful operation.


For More On OOPs.........go through the examples
:-)

No comments:

Post a Comment