Google+

35. Object Oriented Programming







What is Object Oriented Programming ?

Object Oriented Programming organizes a program around its data (that is objects) and a set of well defined interfaces to that data. Object Oriented Program can be characterized as data controlling access to code.

Object Oriented Programming methodology is to design a program using classes and objects.It simplifies the software development and maintenance by providing some concepts:
  •  Object
  •  Class 
  •  Inheritance
  •  Polymorphism
  •  Abstraction
  •  Encapsulation

Object 

Object is an instance of a class. i.e. In Java program we're going to use object as an instance of a class as shown below:

class Pen   // Pen is a class. It is a template for creating objects.
{
    public static void main(String args[])
    {
       Pen Reynolds = new Pen();  // Reynolds and Parker are objects. These objects are just instances of the class Pen.
       Pen Parker = new Pen();
     }
}

Class

Any concept you wish to implement in Java must be encapsulated in a Class. And also the class is a template or blueprint from which objects are created.

class Pen   // Pen is a class. It is a template for creating objects.
{
    public static void main(String args[])
    {
       Pen Reynolds = new Pen();  // Reynolds and Parker are objects. These objects are just instances of the class Pen.
       Pen Parker = new Pen();
     }
}

Abstraction

Abstraction in java is used to hide certain details and only show the essential features of the object.

For example: - People don't think of a car as set of tens of thousands of individual parts, instead think car as a well defined object with its own unique behaviors like Speed, Mileage etc.

Encapsulation

Encapsulation is a protective wrapper that prevents the code and data from being arbitrarily accessed by the other code defined outside the wrapper. Access to code and data inside the wrapper is tightly controlled through a well defined interface.

Class is the protective wrapper that prevents the code and data from being arbitrarily accessed by the other code defined outside the wrapper . The Code and  Data are called as the members of a class. The data defined by the class are referred to as instance variables. The code that operates on the defined data is called as method. Methods define how the instance variables/data can be used (i.e. Behavior and interface of a class are defined by methods that operate on  its instance variables/data). Since the purpose of the class is to encapsulate complexity, there are mechanisms for hiding the complexity of the implementation  inside the class. Each method or variable in a class can be marked as public or private. The public method or variable inside a class can be accessed from the outside of the class, where as the private method or variable inside a class cant be accessed outside of the class.

i.e. In Java, code (i.e. Methods) and data (i.e. instance variables) are protected inside a wrapper called  class.

Inheritance

Inheritance is a mechanism in which one object acquires the properties of another object. The following example will show you how the objects are hierarchically organized and how the child objects inherit the properties from the parent objects


In programming like Java, the idea behind using Inheritance concept is to create a new classes built upon existing classes. i.e. The new classes reuses the methods and data by inheriting them from the existing classes.

Polymorphism

Polymorphism means of having many forms. In programming languages like Java, polymorphism concept is subjected to methods, where the code inside the same method can perform differently on different conditions.

Example: - Suppose there a method called 'Smell' under class 'Dog'. The output of the 'Smell' method can take several forms i.e. 1. When a dog smells a cat -> The output of the 'Smell' method will be 'The dog barks and runs after the cat' 2. When a dog smells food -> The output of the 'Smell' method will be 'The dog will salivate and run after its bowl'. Hence in this example I want to conclude that the same  method 'Smell' will take different forms based on what the dog smells.

As this is not the correct time to explain the above concepts with Java examples before actually knowing the Java programming basics. Lets go through all the above concepts with examples when the time arrives. 




Please comment below to feedback or ask questions.

Lets go through a Simple Java Program in the next post.




No comments: