Thursday, November 26, 2009

What is Object Oriented Programming

Object Oriented Programming can be defined as a technique by which a computer program is designed and written around objects. In this article we will introduce some of the fundamental concepts behind OOP.


What is object in Object Oriented Programming


Object is the basic run-time entities in an Object Oriented Programming. Here problem is analyzed in terms of objects and nature of communication between them. When a program is executed, objects interact with each other without knowing the details of their data or code.

The real life example of an object can be anything. A car, pen, you, me all are objects.

What is Class in Object Oriented Programming

A class is the template of objects. Classes act as some kind of blueprint of the objects created out of the class. A class defines the nature and behavior of the objects to be created. Nature of an object is defined by the data members and behavior is defined by the methods defined in the class.
Real life example of class is type of animals, type of birds etc.

In java class is created by using the keyword ‘class’. Here is an example of Java code that defines a class.

class MyClass
{
int i;
void setData()
{
i=10;
}
}

Here ‘int i’ is the data member and ‘void setData()’ is the method or function member.
In Java the following conventions are followed:
Class name starts with upper case character and method names starts with lower case character.

What is Constructor

Concept of Constructor in object oriented programming is another interesting and important topic. Constructors are used to initialize properties and methods when an object is instantiated. Constructors are special type of methods whose execution is a must. While creating an object, constructor is invoked using ‘new’ operator.

Constructor has the following important features:

1. It’s name and the class name must be same
2. Constructor has no return type because the implicit return type of a constructor is the class type itself.
3. If Constructor is not defined in a class, compiler will supply default constructor which means a constructor with no argument and empty body.
4. It is the constructor’s job to initialize the internal state of an object making the object usable.

If you have any other suggestions, feel free and leave a comment

2 comments:

  1. could you give an example of coding using constructor and the normal one???

    ReplyDelete
  2. Here is the program:


    class MyClass
    {
    int i;
    void setData()
    {
    i=10;
    }
    MyClass()
    {
    i=0;
    }
    void display()
    {
    System.out.println("Value of 'i'=" + i);
    }
    public static void main(String args[])
    {
    MyClass ob=new MyClass();
    ob.setData();
    ob.display();
    }
    }

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner