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
Friday, December 22, 2006
Today we will make a entry into the world of Object Oriented Programming (We will call it as OOPs henceforth)
Whenever people discuss anything seriously about Software & Software Development for that matter, somebody surely makes use of buzzwords like OOPs, Moularity, Reusability,Abstraction, Inheritance, Polymorphism & what not. Use of a buzzword is not a big deal however, Knowing the meaning of these Buzzwords is really going to make a difference. This is not only true for discussions but for successful Software Design & Development.
Those who are intrested in making career in IT Industry should concentrate more on these concepts rather absorb these concepts. Thses concepts could be used for cracking Technical Interviews with any company in this world as well as they make sense when we are deeply involved in Software Development(initially) & Software Design (later on as a Software Architect/ Product Architect). Those who are alredy aware of these concepts could read this post as a refreshment.
Let's Start our bussiness without wasting much time. I hope you will enjoy this tour with me.
When we say "Object Oriented Programming", normally there is a question Why we need it?, What do you mean by Object? Is it very different from what we are doing (normally afrom "C" programmers). Let's tackle these questions one by one......................
Need for OOPs arises as , In real world each entity is an "Object" .What do i mean by this?. It's really simple, Look around yourself whatever you are looking at is an real world object. you find many such objects in everyday life, e.g. Car, Fan, Home, Any Vehicle, Bank acccount,Humans & On & On &On.... when we say software development we talk about software counterparts of such a objetcts. Software objects model real world objects. I know it's bit confusing....Let's make it very simple.......Any object(Real World/Software) has
a) State & b) Behavior
Car has a State e.g. Name of the car, Color, price, Fuel type,Speed etc. as well as car has a behavior e.g. Starting, Stopping (after applying breaks), Travelling with certail speed(with change in gear) etc. This could be applied to any object which comes to your mind & if you want you can chalk out those objetcs their state & behavior.
Now if you are thinking that you have received some valuable information from above paragraphs then whenever you think of any object ask two simple questions to yourself
1) What are the possible states of this object?
2) What possible behavior this object could perform?
As an assignment, Think of few objects & right down your observations on a piece of paper, I will take a break for 5-10 mins.
Done?
Hopefully "Yes"
Now see, If i am going to drive a car can i say in generic fashion that "A Person object is going to drive a Car Object"(I know its too boring, However if you replace "Person Object" with my name & "Car Object" with Car's name, sentence becomes specific), Coming back to our original discussion, this shows how two objects could interact. If i am driving a car if i press accelarator obviously its car behavior to speed up (intern changing its State: How? As speed is one of the parameter in a car's State, right?). if a t some point of time if i want to stop i will apply the breaks & again car will show its behavior of stopping/ Speed reuction (again state of the car is changed).
An Interesting thing to add is some parameters defined for car (in genral for any object rather) are static(they don't change frequently e.g. Color of the Car, Fuel type, Make etc.) & few are dynamic (Speed, Fuel level , Tyre Pressure etc.)
If you apply some thought on this you will find that entire world is full of objects exhibiting some state & showing some behavior. In software domain we should have exact replica of what we do normally in real world & Object Oriented Design does exactly the same.
In software development , Software objets are conceptually similar to real world objetcs. They also consisits of state & related behavior.
We will go into in depth discussion of "What are objetcs?" , "How they are represented?" & many more questions very shortly.
Whenever people discuss anything seriously about Software & Software Development for that matter, somebody surely makes use of buzzwords like OOPs, Moularity, Reusability,Abstraction, Inheritance, Polymorphism & what not. Use of a buzzword is not a big deal however, Knowing the meaning of these Buzzwords is really going to make a difference. This is not only true for discussions but for successful Software Design & Development.
Those who are intrested in making career in IT Industry should concentrate more on these concepts rather absorb these concepts. Thses concepts could be used for cracking Technical Interviews with any company in this world as well as they make sense when we are deeply involved in Software Development(initially) & Software Design (later on as a Software Architect/ Product Architect). Those who are alredy aware of these concepts could read this post as a refreshment.
Let's Start our bussiness without wasting much time. I hope you will enjoy this tour with me.
When we say "Object Oriented Programming", normally there is a question Why we need it?, What do you mean by Object? Is it very different from what we are doing (normally afrom "C" programmers). Let's tackle these questions one by one......................
Need for OOPs arises as , In real world each entity is an "Object" .What do i mean by this?. It's really simple, Look around yourself whatever you are looking at is an real world object. you find many such objects in everyday life, e.g. Car, Fan, Home, Any Vehicle, Bank acccount,Humans & On & On &On.... when we say software development we talk about software counterparts of such a objetcts. Software objects model real world objects. I know it's bit confusing....Let's make it very simple.......Any object(Real World/Software) has
a) State & b) Behavior
Car has a State e.g. Name of the car, Color, price, Fuel type,Speed etc. as well as car has a behavior e.g. Starting, Stopping (after applying breaks), Travelling with certail speed(with change in gear) etc. This could be applied to any object which comes to your mind & if you want you can chalk out those objetcs their state & behavior.
Now if you are thinking that you have received some valuable information from above paragraphs then whenever you think of any object ask two simple questions to yourself
1) What are the possible states of this object?
2) What possible behavior this object could perform?
As an assignment, Think of few objects & right down your observations on a piece of paper, I will take a break for 5-10 mins.
Done?
Hopefully "Yes"
Now see, If i am going to drive a car can i say in generic fashion that "A Person object is going to drive a Car Object"(I know its too boring, However if you replace "Person Object" with my name & "Car Object" with Car's name, sentence becomes specific), Coming back to our original discussion, this shows how two objects could interact. If i am driving a car if i press accelarator obviously its car behavior to speed up (intern changing its State: How? As speed is one of the parameter in a car's State, right?). if a t some point of time if i want to stop i will apply the breaks & again car will show its behavior of stopping/ Speed reuction (again state of the car is changed).
An Interesting thing to add is some parameters defined for car (in genral for any object rather) are static(they don't change frequently e.g. Color of the Car, Fuel type, Make etc.) & few are dynamic (Speed, Fuel level , Tyre Pressure etc.)
If you apply some thought on this you will find that entire world is full of objects exhibiting some state & showing some behavior. In software domain we should have exact replica of what we do normally in real world & Object Oriented Design does exactly the same.
In software development , Software objets are conceptually similar to real world objetcs. They also consisits of state & related behavior.
We will go into in depth discussion of "What are objetcs?" , "How they are represented?" & many more questions very shortly.
Java is an Object Oriented Programming Language & it's very essential to learn Object Oriented Programming Concepts before we jump into actual Java Programming. In my subsequent posts we will address different issues related to Object Oriented Programming aspects.
We will have following Objectives when we start exploring world of Object oriented Programming
1. Need of an Object Oriented Programming
2. What is a Class?
3. What is an Object?
4. Object Model -Buzzwords & Reality
5. Difference between Procedural Approach & Object Oriented Programming
We will have following Objectives when we start exploring world of Object oriented Programming
1. Need of an Object Oriented Programming
2. What is a Class?
3. What is an Object?
4. Object Model -Buzzwords & Reality
5. Difference between Procedural Approach & Object Oriented Programming
Subscribe to:
Posts (Atom)