Thursday 24 December 2015

What is Object-Oriented Programming in java

Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects.




Object :

Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

It is a bundle of related variables and functions (also known methods).

Objects share two characteristics: 


  1. State: State is a well defined condition of an item. A state captures the relevant aspects of an object
  2. Behavior: Behavior is the observable effects of an operation or event,

Example

Object: – Car

State: Color, Make

Behavior: Climb Uphill, Accelerate, SlowDown etc




Characteristics of Objects
  • Abstraction
  • Encapsulation
  • Message passing


1. Class


A class is a prototype that defines the variables and the methods common to all objects of a certain kind. 
Member Functions operate upon the member variables of the class. Collection of objects is called class. It is a logical entity.
2. Inheritance :
When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. 

It provides code reusability. It is used to achieve runtime polymorphism.

3. Polymorphism :

Polymorphism is a feature that allows one interface to be used for a general class of actions.When one task is performed by different ways i.e. known as polymorphism.  

The behavior depends on the types of data used in the operation. It plays an important role in allowing objects having different internal structures to share the same external interface.

In java, we use method overloading and method overriding to achieve polymorphism. e.g. cat speaks meaw, dog barks woof etc.


Types of Polymorphism :

  1. Static Polymorphism
  2. Dynamic Polymorphism
1. Static Polymorphism (Function Overloading) – Function overloading if we have more than one function having same name with different prototypes (parameters) then it is also called function overloading.

2. Dynamic Polymorphism (Function Overriding) – Function overriding is just easy to say if sub class or child class has the same method or function declared in the parent class that types of method is called function or method overriding.

Declaring a method in subclass (child class) which function is already  declared in  parent class is known as method overriding.

4. Abstraction :
The purpose of abstraction is to hide information that is not relevant or rather show only relevant information and to simplify it by comparing it to something similar in the real world. 
Hiding internal details and showing functionality is known as abstraction. 
For example: phone call, we don't know the internal processing.


5. Encapsulation  :

Encapsulation is the whole idea behind encapsulation is to hide the implementation details from the users. 

It just like capsule, Its wrapping code and data together into a single unit.






According to figure encapsulation class is just making all the data members of the class private.

It is also known as data hiding.


Methods of Encapsulation

  1. setter( )
  2. getter( )

1. setter( ) - setter is used for set the data and 
2. getter( ) - getter is use for get the data.


**Advantage of Object Oriented Programming:
  1. Objects are modeled on real world entities.
  2. This enables modeling complex systems of real world into manageable software solutions.
  3. OOPs makes development and maintenance easier where as in Procedure-oriented programming language it is not easy to manage if code grows as project size grows.)
  4. OOPs provides data hiding whereas in Procedure-oriented prgramming language a global data can be accessed from anywhere.



No comments:

Post a Comment