Basic Concepts in Oops - 1

This blog post will teach about the basic concepts in Object oriented programming. I have written this post in the simplest possible way.

1) Object

Objects are primary run-time entities in an object - oriented programming. Every objects has its own features and properties. Objects occupy space in memory. An object refers to an instance in class.

2) Class

Class is a group of objects that have identical properties. Objects are variables of class type. A class is actually a model and a true specimen of object. In other words, class is a blueprint from which objects are created. Every object is defined using a class.


public class Vehicle
{
int vehicleId;
int mileage;
int cost;
}

In the above example, Vehicle is a class and vehicleId, mileage and cost are its attributes.


Vehicle vehicle = new Vehicle();

Here, vehicle is an object of the Class.

3) Instantiation

The process of creating an object from the class is called instantiation. An object is the instance of the class. There are 2 methods through which, we can do the instantiation.

  • Declaration of reference variable of the class which will store a reference to the object

Vehicle car;

Here car is a reference variable for the object Vehicle.

  • Creating an object using new operator.
As seen in the example of Class, vehicle is an object of the Class.

4) Method

Method is a member function of a class. It is same as function in C. It consists of a block of statements that will get executed and may or may not returns a value.

public static int sum(int a, int b)
{
return (a+b);
}

Here, "sum" is the name of the function, which returns the computed sum of 2 variables.

5) Constructor

Creating an object is called constructing the object. Constructors initializes the state of the object.Constructors are invoked when an object is created. It can have default arguments. Following is an example of constructor.


class Vehicle
{
int vehicleId;
int milage;
Vehicle(int a, int b)
{
vehicleId=a;
milage=b;
}
}

class MainClass
{
public static void main(String args[])
{
Vehicle car = new Vehicle(10,60);
Vehicle bike = new Vehicle(15,20);
}
}


Here, the object car is initialized with vehicleId=10 and milage =60, where as  the object bike is initialized with vehicleId =15 and milage =20.

Hope this post gave you clear idea about some of the topics in oops. Check the next blog for more about oops. Comments/suggestions are most welcome.

Similar posts

1) Oops - 2
2) Oops - 3
3) Oops - 4


Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++