In earlier post we have emphasized on "Need of an Object Oriented Programming". In this post & subsequently we will deal with very basic concepts of OOPs viz Class and Object.
If you have understood earlier discussion of Need of OOPs , It won't be very difficult to understand what is a class. if we quickly summarize earlier discussion we said that "Every entity in real world is an object(having State & Behavior) & Software objects model / represent real world objects. "
If you are with me, can we say by simple observation that In real world we often come across many objects of same kind. e.g. Thousands of Cars of same company, make, model right? Extend this for any other object you have thought of earlier & you will find plenty of such cases...am i right? do you agree?
One more example could be many houses in certain project with same look, built etc..In case of a Car or House (or for any other object for that matter) they are built from certain blueprint. There would be a drawing available for a certain car model/house & so on and ultimately Automobile company / builder etc. would use this blueprint to built car or house etc..
So without complicating much i will say(referring to above exapmples) " Blueprint is a Class & built car or house is an Object." using same Blueprint one can built many cars or houses etc..right?
Similar objects therefore contains same components. In object oriented programming terminology It is said that Your car is an instance of a class of objects (or simply class)Car or your House is an instance of a class of objets (or simply class)House.
Let's have a look at one of the simple possible implementation(in Java) of a Car
class Car
{
//Single line Java Comment--Same as C++
//data members- State of a Car
String name;
int speed;
int gear;
/*
Block Comment in Java same as C, C++
*/
/*
Methods representing behavior of Car
*/
public void setName(String carName)
{
name=carName;
}
public void changeGear(int newGear)
{
gear=newGear;
}
public void accelarate(int increment)
{
speed=speed + increment;
}
public void applyBreaks(int decrement)
{
speed=speed - increment;
}
public void printState()
{
System.out.println("Name :"+name+" Speed :"+speed+ "Gear :"+gear)
}
}//end of a class Car
The syntax of the Java programming language will look new to you, but the design of this class is based on the previous discussion of Car object.
In this class speed, gear, name represent State of a Car object whereas changeGear, accelarate,applyBreaks,printState are behavior of Car object. It's the behavior of an object through which one can judge how object is going to interact with outside world.
If you observe carefully this car class doesn't contain main method. Simple reason for this is Car is simply a blueprint for creating many car objets & not a complete application. Creating & using a new Car is a responsibility of some other class in the application.
Let's right one more class which will make a use of this Car class. We will create wo different cars & play with them a bit.
class CarDemo
{
public static void main(String args[])
{
//Create two different Car Objects
Car carOne=new Car();
Car carTwo=new Car();
//Invoke methods on first Car object
carOne.setName("Car One")
carOne.changeGear(2);
carOne.accelarate(20);
carOne.printState();
//Invoke methods on first Car object
carTwo.setName("Car Two");
carTwo.changeGear(2);
carTwo.accelarate(20);
carTwo.printState();
carTwo.changeGear(3);
carTwo.accelarate(35);
carTwo.printState();
carTwo.applyBreaks(15);
carTwo.changeGear(2);
carTwo.printState();
}//end of main method
}//end of class CarDemo
Output of this CarDemo class will be:
Name : Car One Speed : 20 Gear : 2
Name : Car Two Speed : 20 Gear : 2
Name : Car Two Speed : 35 Gear : 3
Name : Car Two Speed : 20 Gear : 2
Note:
1) In Java everything you write inside a class.Even main method is inside a class (See CarDemo class). Those who are aware with C++ should make a note of this point.
2) You can't execute a class unless it has a main method as main is a starting point of an application. Normally we don't write main method in each class but we write it in a class which acts as a application starting point.
3) Signature of main method is totally different from C & C++ counterparts (more on this later). String datatype represents character Strings (similar to char* arr[]). System.out.println() prints output on console (similar to printf(..) in C , cout<< in C++ ). println(..) prints on a newline so there is no need to add "\n" character.
We have seen What is a Class , How to write a simple Java class , How to create an Object(without knowing much about it's details).
In Next post we will jump into more details of What is an Object, its chatracteristics etc...
Till then Bye Bye.....
Happy New Year 2007
Friday, December 29, 2006
Subscribe to:
Post Comments (Atom)
3 comments:
DearShrinivas,
You are on a right track. Good Job. You know your Subject and passion.
Iam Happy to see the blog.
Add The adsense on the right panel , vertical. You will have better visibility and Clicks.
Then post atleast 3 posts every week. more the better.
promote your blog to your Address book contacts and ask to comment.
The Submit it to search engines.
more later.
regards
all the best
Jayant Hudar
Great Going Pranav!!
Excellent , Keep up the good work.
Srini,
Thanks for your effort. Expecting more in the coming time.
-Vivek
Post a Comment