Wednesday, March 9, 2016

Classes and Objects in C++/Java

Dear Folks !
Classes and Objects form basis of object oriented programming(OOP), without this concept hardly any one can implement other features (like Data Abstraction, Encapsulation, Inheritance etc) of OOP. 
In the context of OOP, a Class is a group/collection of objects or things which exhibit similar characteristics. For instance a class of mobiles, class of birds etc.

In programming context, it represents a  factory of objects, i.e using a class one can create objects with required specifications(behavior),So, put in other words it is template or blueprint kind of thing using which you can create objects of certain behavior. The following image makes it clear how it can act as factory for objects...
Normally, class has three parameters. Namely
1. A unique class name
2. Attributes
3. Methods

A class must have unique name, it can not share it's name with any other class. For example, the name of class can be  Car, 
A class has a list of attributes,which are used to describe behavior of an object. Examples:
A class Mobile can have attributes like
RAM size, internal memory, battery life, camera pixels etc.
Similarly a class Account can have attributes like, cust_id, acc_no, acc_type, balance etc. 
The methods of class help to control the behavior of an object, there by allowing to modify the states of an object. Let's explore more about attribute and methods of class car. Look at the image ...


So here for class Car we have attributes like fuel(quantity/level) and maxspeed, so through these attributes we control the behavior of car.
For example, Through method setSpeed(),  if we set maxspeed as 120km/hr, then when we drive it, the maximum speed available would be 120km/hr and we can drive fast. where as if we set maxspeed as 30km/hr, then we drive slowly. You can notice here the behavior of an object car is controlled through methods of that class...
From the above discussion it is clear that an object is an entity/thing which has certain behavior, that can be controlled. It has states corresponding to attributes of class. The states of an object could be changed/modified through methods of class.This is how one can set the desired behavior to an object.

In a nutshell, we can say, object has three parameters, Namely
1. Identity( obtains from the class name)
2. States( obtains through the attributes of a class)
3. Behavior( can be controlled through the defined methods/functions of class).

Following example illustrates this concept in Java

class Account
{
 private String name;
 private double balance;
 void init(String sx, double x) { name=sx; balance=x;}
 void setNewBalance(double x){ balance=x;}
 void show(){ System.out.println(name+" has balance "+balance;}
}

class DemoAccount
{
 public static void main(String args[])
 {
  Account a1=new Account();
  Account a2=new Account();
  a1.init("Tom",200000);
  a2.init("Jerry",100);
  a1.show();
  a2.show();
 /* Now Tom is rich, behaving arrogantly, but Jerry is poor, waiting  for an opportunity, let's give him an opportunity to Jerry and  make him rich, let's change their behavior, */
 a1.setNewBalance(50);
 a2.setNewBalance(300000);
 a1.show();
 a2.show();
 /* Now you can see Jerry has good money, and has become rich, so his behavior changed from poorness to richness, but still Jerry is Humble and polite.*/
 }
}
Output
Tom has balance 200000
Jerry has balance 100
Tom has balance 50
Jerry has balance 300000

In conclusion, it is a backbone of OOP paradigm, and helps in Data Abstraction, Data Encapsulation and run-time polymorphism, which in turn provide extensiblity  of software easily.

How did you find this stuff ? Please comment and suggest if any improvements needed here...

No comments:

Post a Comment