I have stopped posting on this blog due to some reasons.
Kindly Visit the following blog for the updates.
http://learnjavaeasily.blogspot.com/
Sorry for the Inconvenience.....
Shriniwas
Monday, March 5, 2007
Saturday, February 10, 2007
Open Secret
The title of the post is intentionally non technical ........and it has some meaning....How?
The reason is quite simple....as the name suggests .......you already know the stuff that will be presented in the current post.....only difference would be we will look this entire stuff from different view point.....
This post exclusively deals with answer asked in the last part of the previous post.
We will be thinking of one real world entity (object) & we will try to find out all the major pillars of OOPs in it..which will be a proof of concept for the discussion we had in earlier post.
Ready? Get...Set...Go.....
I am thinking of Personal Computer as real world entity.
Now biggest question is how Abstraction, Encapsulation, Inheritance , Polymorphism and Modularity applies to this object?
I am sure you must have considered other objects as well & must have done some research on this...right?
Personal Computer (PC) as an object is a very good example to understand these major pillars.
If i have to purchase a PC , very obvious step i would take is i will consult my friends, relatives and colleagues who know about it. I am sure they will ask me for What reason you are purchasing it...meaning is what you are going to do with that? or How you will make use of PC? isn't it?
If i say, I am going to use for my personal use most of them would suggest me some configuration (Memory Size, CD Drive, Speakers etc..)
If i say , I am going to use this as a high end server in my office or wherever this configuration would change (bit obvious that there would be more memory, speakers won't be there.)
If i am going to gift this PC to a School for some educational purpose , the changes in configuration would be more multimedia features (to Show Educational CDs) and may be more multimedia related software (which were missing when i am supposed to use PC as a High End Server)
In these 3 examples i am sure you will appreciate the fact that, PC remains the PC ....Object is same , however we changed the configuration i.e. attributes of PC object according to our need and usability. We selected few attributes and dropped few one as usability changes, This is Abstraction. How we define Abstraction:
Abstraction refers to identifying key aspects (and obviously ignoring/ dropping the rest) of an object/entity for the specific application.
Now you know why the title of this post is "Open Secret "............
Can i consider various electronic components on motherboard of computer or in general PC as a data members and the way we can operate the PC as a behavior of PC?
Let me explain....
I am a user ....I could start the PC as well as i could shutdown the PC ...when i do this there is some typical behavior shown by PC... right?......this is behavior of PC as an object......and i could do much more than this with PC...
When i turn PC on / off do you agree that electronic components behind the scenes are at work..(Some capacitor gets charged and discharged & so on...)....This is Encapsulation...
How Encapsulation is defined:
Encapsulation is a process where data(attributes) & methods (behavior) is rolled into a single entity known as object. It protects data within an object & ensures that it could be accessed only by its methods.
As a user i never need to understand the details of how PC operates but to know how i should operate it or what are the possible operations i could do with PC?...Hiding the complex details of an object is an aspect of Encapsulation.
I turn on & off the PC and do some other stuff (behavior of the PC) as well and behind the scenes many Electronics parts of PC undergo electrical change(change in attributes).And end user never knows these details. This is Encapsulation
I am sure everybody is aware of PCs with Intel's Pentium IV processor , what was there earlier to Pentium IV processors? Pentium III ....earlier to this ? Pentium II .....earlier to this? Pentium I/Pentium in general.......prior to that 80486...80386....80286.........80186.......8086...8085 (In early 70's)...so on...
So each time new processor for PC comes to market there are few enhancements....No need to think that new Processor is 100% brand new...it has same structure / architecture of the previous processor and there is something to more to it in new version
e.g. 80186 is-a 8086 but there were few enhancements compared to 8086...so we normally use the term code written on 80186 is "backward-compatible" with 8086 & so on for other processors ..isn't it?
This backward-compatibility is achieved because some part in older processor is carried forward to next processor & there are advancements on top of it.
I am talking about Inheritance (Definition: Inheritance basically refers to hierarchy to reduce complexity)
So if you are following the posts then, i would simply say that,
Pentium IV is Subclass of Pentium III .....Let me know....whether this statement is true
I am repeating the part of my earlier post to explain Polymorphism
Answer these simple questions (you can do the steps side by side to have a fun) at the end we will revisit the definition
Do you use mouse while using your PC?
Have you ever used a right click button of a mouse?
Have you right clicked on Files, Folders, Desktop etc..?
Have you observed that response (pop-up contents) you get when you right click on Files , Folders and Desktop is different ? ...If not please check it now....
Sure they are different?
Hey, you have now understood Polymorphism....nothing great right?
Definition : It is the ability of different objects to respond to the same message differently
Ability of different objects (Files, Folders, Desktop etc..) to respond to the same message (right click) differently(different contents on pop-up) is Polymorphism.
PC is composed of many loosely coupled components (they are object within themselves)...so different components like Keyboard, Mouse, Motherboard, Monitor, connectors and cables constitute the whole object PC however these are loosely coupled so that if keyboard or mouse or anything else is not working properly you can easily replace them individually without affecting other components say While replacing Keyboard you don't have to do anything with Monitor or motherboard etc...
This is an example of Modularity (In OOPs modularity refers to breaking up the system in small modules which are loosely coupled to each other & act as a single system or as a Cohesive system. This way it is easy yo maintain & easy to extend) in case of a PC....
This brings me to end of this post ... I hope you have enjoyed the journey so far.........
Stay tuned for more........
The reason is quite simple....as the name suggests .......you already know the stuff that will be presented in the current post.....only difference would be we will look this entire stuff from different view point.....
This post exclusively deals with answer asked in the last part of the previous post.
We will be thinking of one real world entity (object) & we will try to find out all the major pillars of OOPs in it..which will be a proof of concept for the discussion we had in earlier post.
Ready? Get...Set...Go.....
I am thinking of Personal Computer as real world entity.
Now biggest question is how Abstraction, Encapsulation, Inheritance , Polymorphism and Modularity applies to this object?
I am sure you must have considered other objects as well & must have done some research on this...right?
Personal Computer (PC) as an object is a very good example to understand these major pillars.
If i have to purchase a PC , very obvious step i would take is i will consult my friends, relatives and colleagues who know about it. I am sure they will ask me for What reason you are purchasing it...meaning is what you are going to do with that? or How you will make use of PC? isn't it?
If i say, I am going to use for my personal use most of them would suggest me some configuration (Memory Size, CD Drive, Speakers etc..)
If i say , I am going to use this as a high end server in my office or wherever this configuration would change (bit obvious that there would be more memory, speakers won't be there.)
If i am going to gift this PC to a School for some educational purpose , the changes in configuration would be more multimedia features (to Show Educational CDs) and may be more multimedia related software (which were missing when i am supposed to use PC as a High End Server)
In these 3 examples i am sure you will appreciate the fact that, PC remains the PC ....Object is same , however we changed the configuration i.e. attributes of PC object according to our need and usability. We selected few attributes and dropped few one as usability changes, This is Abstraction. How we define Abstraction:
Abstraction refers to identifying key aspects (and obviously ignoring/ dropping the rest) of an object/entity for the specific application.
Now you know why the title of this post is "Open Secret "............
Can i consider various electronic components on motherboard of computer or in general PC as a data members and the way we can operate the PC as a behavior of PC?
Let me explain....
I am a user ....I could start the PC as well as i could shutdown the PC ...when i do this there is some typical behavior shown by PC... right?......this is behavior of PC as an object......and i could do much more than this with PC...
When i turn PC on / off do you agree that electronic components behind the scenes are at work..(Some capacitor gets charged and discharged & so on...)....This is Encapsulation...
How Encapsulation is defined:
Encapsulation is a process where data(attributes) & methods (behavior) is rolled into a single entity known as object. It protects data within an object & ensures that it could be accessed only by its methods.
As a user i never need to understand the details of how PC operates but to know how i should operate it or what are the possible operations i could do with PC?...Hiding the complex details of an object is an aspect of Encapsulation.
I turn on & off the PC and do some other stuff (behavior of the PC) as well and behind the scenes many Electronics parts of PC undergo electrical change(change in attributes).And end user never knows these details. This is Encapsulation
I am sure everybody is aware of PCs with Intel's Pentium IV processor , what was there earlier to Pentium IV processors? Pentium III ....earlier to this ? Pentium II .....earlier to this? Pentium I/Pentium in general.......prior to that 80486...80386....80286.........80186.......8086...8085 (In early 70's)...so on...
So each time new processor for PC comes to market there are few enhancements....No need to think that new Processor is 100% brand new...it has same structure / architecture of the previous processor and there is something to more to it in new version
e.g. 80186 is-a 8086 but there were few enhancements compared to 8086...so we normally use the term code written on 80186 is "backward-compatible" with 8086 & so on for other processors ..isn't it?
This backward-compatibility is achieved because some part in older processor is carried forward to next processor & there are advancements on top of it.
I am talking about Inheritance (Definition: Inheritance basically refers to hierarchy to reduce complexity)
So if you are following the posts then, i would simply say that,
Pentium IV is Subclass of Pentium III .....Let me know....whether this statement is true
I am repeating the part of my earlier post to explain Polymorphism
Answer these simple questions (you can do the steps side by side to have a fun) at the end we will revisit the definition
Do you use mouse while using your PC?
Have you ever used a right click button of a mouse?
Have you right clicked on Files, Folders, Desktop etc..?
Have you observed that response (pop-up contents) you get when you right click on Files , Folders and Desktop is different ? ...If not please check it now....
Sure they are different?
Hey, you have now understood Polymorphism....nothing great right?
Definition : It is the ability of different objects to respond to the same message differently
Ability of different objects (Files, Folders, Desktop etc..) to respond to the same message (right click) differently(different contents on pop-up) is Polymorphism.
PC is composed of many loosely coupled components (they are object within themselves)...so different components like Keyboard, Mouse, Motherboard, Monitor, connectors and cables constitute the whole object PC however these are loosely coupled so that if keyboard or mouse or anything else is not working properly you can easily replace them individually without affecting other components say While replacing Keyboard you don't have to do anything with Monitor or motherboard etc...
This is an example of Modularity (In OOPs modularity refers to breaking up the system in small modules which are loosely coupled to each other & act as a single system or as a Cohesive system. This way it is easy yo maintain & easy to extend) in case of a PC....
This brings me to end of this post ... I hope you have enjoyed the journey so far.........
Stay tuned for more........
Saturday, February 3, 2007
Object Model -Buzzwords & Reality


Hello All,
Meeting after very long time really.......
Today we will spend sometime understanding buzzwords in Object Model or in general Object Oriented Programming Paradigm.
We will talk about:
Abstraction
Encapsulation
Inheritance
Polymorphism &
Modularity
These terms are very frequently used by many people and really these are buzzwords in OOP. few people call these as Major Pillars in OOPs. obviously there are Minor Pillars as well(More on this later).
We will define these terms one by one & individually we will understand how simple they are in reality. At the end for sure we will understand all the terms with a simple example. Rather we will consider one object and we will apply all these Buzzwords to that object.
Let's go step by step
1) Abstraction
Abstraction refers to identifying key aspects (and obviously ignoring/ dropping the rest) of an object/entity for the specific application.
e.g. If we consider Person as a object , then how you will represent Person ?
You will write a class Person right? In this class you will go on adding attributes (to represent State of an object) and behaviors (methods) right?
Simple question is whcih attributes you will add in this class? answer is "DEPENDS".
Depends on what?
Depends on for what purpose (In which application to be specific) you are going to use this person object. I do agree that few attributes will be common across the applications like firstname, lastname, age, gender etc..
Meeting after very long time really.......
Today we will spend sometime understanding buzzwords in Object Model or in general Object Oriented Programming Paradigm.
We will talk about:
Abstraction
Encapsulation
Inheritance
Polymorphism &
Modularity
These terms are very frequently used by many people and really these are buzzwords in OOP. few people call these as Major Pillars in OOPs. obviously there are Minor Pillars as well(More on this later).
We will define these terms one by one & individually we will understand how simple they are in reality. At the end for sure we will understand all the terms with a simple example. Rather we will consider one object and we will apply all these Buzzwords to that object.
Let's go step by step
1) Abstraction
Abstraction refers to identifying key aspects (and obviously ignoring/ dropping the rest) of an object/entity for the specific application.
e.g. If we consider Person as a object , then how you will represent Person ?
You will write a class Person right? In this class you will go on adding attributes (to represent State of an object) and behaviors (methods) right?
Simple question is whcih attributes you will add in this class? answer is "DEPENDS".
Depends on what?
Depends on for what purpose (In which application to be specific) you are going to use this person object. I do agree that few attributes will be common across the applications like firstname, lastname, age, gender etc..
So if you are going to design a object Person for different applications like say healthcare, Employe Management System, Social Survey related software etc... there could be few attributes which may not appear everywhere.
e.g. 'religion' will not appear at all in a HealthCare System where as it may appear in Social Survey related system. similarly, 'Salary' attribute will be definitely present in Employee Management System/ HRMS applicationh but bit obvious it will not appear in Healthcare System and 'Weight' will be an attribute in Health care application whereas it will be missing in Employee Management System / HRMS applications & so on.....
The bottom line is selecting proper attribute of an object for a specific application is Abstraction.
2) Encapsulation
Encapsulation is a process where data(attributes) & methods (behavior) is rolled into a single entity known as object. It protects data within an object & ensures that it could be accessed only by its methods.
e.g. Considering above example of a Person object, HR of the company wants to know the salary details of a Person named ABC. what HR does is on an application he/she will go & search the salary details of Person name 'ABC'. In a fraction of second all the data is available on the screen. HR as a user never understands which attribute is accessed or where it is stored (in database/ in file etc..) as well as end user has no idea how it is fetched from a source? what kind of processing is done on it ?(if any) & how it was displayed?
OOPs ensures that data is not accessed accidentally/intentionally directly but only through methods (behavior).
If i present a class Calculator in front of you with a method written inside it which accepts an integer & does a job of finding factorial of a number say its name is
int calculateFactorial(int number)
So you are the end user & now you know that passing an integer to this method will result in producing factorial of that specific number right?
As most of us know that there are two ways of finding out factorial of a number:
In a Recurssive fashion and
In a Non- Recurssive fashion..........right?
Now as a end user you don't have to worry what type of implementation is used inside a method as long as you are getting correct results ....am i correct?
This is one more example of Encapsulation
So , Encapsulation does a sepration between interface (methods you see as a class user) & it' s implementation (how exactly these methods are written inside a class).
Finding it easier?
3) Inheritance
Inheritance basically refers to hierarchy to reduce complexity.
In OOPs it's a class hierarchy. You will have some class "A" doing some stuff for you & later on you would realize that you need one more class say Class "B" which will be lot similar to A(not saying identical) and B has something extra in it. so B itself is a A plus something extra(more specialized). so in this case what we will do is we will write a class which will extend class A will put some additional functionality in this new class & we will call that Class as Class B. so we are creating a hierarchy of classes which represents Generalization-Specialization hierrachy
e.g.
Vehicle is parked on a Road doesn't make much sense or doesn't carry much information as opposed to Two Wheeler (little bit specific) or Four Wheeler is parked on a Road does. Honda bike Parked on a Road is more specfic compared to all the above statements.
You can have a hierarhy say Vehicle as a generic class and TwoWheeler, ThreeWheeler, FourWheeler extending from them. again you can have a classification that would go on say for two wheeler as Honda bike and Suzuki bikes & so on same applies for any Vehicle for that matter. In the diagram as you move downwards you will come across more specific classes/entities and vice versa.
In Java Vehicle class will be called as a Superclass (will be called as Base Class in C++) where as TwoWheeler,ThreeWheeler and Fourwheeler will be individyually called as Subclass of Vehicle. Just to add twist , TwoWheeler class is a Superclass of Class Honda or Honda is a Subclass of class TwoWheeler.
Inheritance of this kind is also known as a "is-a" kind of relationship.
In this hierarchy to express the relationship between Superclass & Subclass you have to use "is-a" word in your sentense.
e.g.
Honda is a TwoWheeler
TwoWheeler is a Vehicle
FourWheeler is a Vehicle
etc..
There is something known as "has-a" kind of relationship as well also known as containment. (more on this later)
4) Polymorphism
Poly means many & morph means forms
In typical technical stuff definition of Polymorphism is:
It is the ability of different objects to respond to the same message differently.
i am sure nothing is understood by the definition if you are totally new to the world of OOPs.
Let's simplify it:
Answer these simple questions (you can do the steps side by side to have a fun) at the end we will revisit the definition
Do you use mouse while using your PC?
Have you ever used a right click button of a mouse?
Have you right clicked on Files, Foldres, Desktop etc..?
Have you observed that response (pop-up contents) you get when you right click on Files , Folders and Desktop is different ? ...If not please check it now....
Sure they are different?
Hey, you understood polymorphism....nothing great right?
Still Puzzled ....Let me explain by revisiting the definition that we have started with
Ability of different objects (Files, Folders, Desktop etc..) to respond to the same message (right click) differently(different contents on pop-up) is Polymorphism.
simple isn't it?
Note: Message is equivalent to calling a method on a object.
5) Modularity
You might have guessed from the name itself what modularity means....Still let's have a look
In OOPs modularity refers to breaking up the system in small modules which are loosely coupled to each other & act as a single system or as a Cohesive system. This way it is easy yo maintan & easy to extend.
e.g.
Think about a Car which is made up of a small subsystems like Engine , Wheels, Tyre etc..
If tyre gets punctured you can replace it without touching engine right?
So all subsystems are loosely coupled to built a big System known as a car.
Can you do a simple assignment ?
Think of any Object & try to find out all these Pillers in that objet.
In the next post i will do the same, We will consider one object & i will show you how Abstraction,Encapsulation,Inheritance, Polymorphism and Modularity are part of it.......
Till then............Bye Bye
Friday, January 5, 2007
What is an Object
In continuation with earlier discussion, Let's try to explore "What is an Object?". I am sure you will correctly answer it as "It's an entity with State & Behavior". In earlier discussion we have explored what is a class & how to use that class in actual programming with simple (although bit non-realistic) example of Car & CarDemo classes. If you have understood the usage of a class, then you have unknowingly dealt with object as well, How? See, In CarDemo class when we said:
Car carOne=new Car();
Car carTwo=new Car();
Don't worry too much how these Java classes are compiled & executed
Note for C Programmers: For the time being assume 'new' similar to 'malloc(...) ' function
carOne & carTwo objects of type Car.carOne has some state(values of data members) & carTwo has some other State although behavior(methods) is common as they are of type Car.
Few important things to note from example are
1) We are changing State of any object by "Invoking methods" & do not change data members directly.
In CarDemo class we have created object carOne, carTwo & then we have invoked method to set the name of a car , change a gear, accelerate, applying breaks which intern changes name, gear, speed etc. data member values which is revealed through output.
2) Hiding internal state of an object & allowing other objects to change the state through method interaction is known as Data Encapsulation(Very important aspect of Object Oriented Programming ....More on this later)
Apart from state & behavior object has two additional characteristics namely Identity & Responsibility.
So intern any Object has four distinct Characteristics:
1) State
2) Behavior
3) Identity
4) Responsibility
We will understand this with same exapmle 'Car'
State:
When we say Car has a certain state (travelling with certain speed or at rest), we are actually specifying current values of its attributes/data members (Speed, Fuel Level,Make, Type of Fuel, Tyre Pressure etc..), So In OOPs paradignm we will define State of an Object as "State of an object is values of its data member at any point of time". State I hope this is clear as we have discussed this number of times & i don't want to hang you on the same point too long.
Behavior:
Object has an behavior which interrn changes State of an Object.In our Car example when we talk about accelarate as a behavior of an object (we represent behavior as a method in a class) it increases value of 'speed' data member & reduces value of Fuel Level (for the sake of example... In our class we don't have this attribute). Behavior could be expressed as "How certain object acts/reacts in terms of its State change/operations performed on it"
In summary , For a car object what are the possible operations we can perform & how car will react by means of its state change defines Behavior of a car.
Identity:
Term is self explanatory i guess, Identity is the attribute/property of an object which will uniquely distinguish it from all other similar objects. e.g. If there are 10 cars of same company, same model, same color are parked in front of a building how one could distinguish them?......Yes you are right by their Number Plates....So registration number/ some number given by state authority/whomsever is a property which distinguishes one car from other even if all other attributes are same.....right? Kindly don't assume that Identity has to be a single property in some cases it could be group of properties which will act as identity of an object. e.g. If i consider Bank object perhaps only account number can't act as identity we have to consider bank name as well, Since there is a high chance that there will be a bank account number '25000' existing in Bank A & Bank B.
Responsibility:
In our example of Car, we could design the entire Car class which will represent actual car object of real world.....right?...In that case we could have say that Car as a whole system is composed of several objects e.g. Engine, Windows,Doors,Fuel tank etc...Ok? so in this whole system each object has some responsibility so that Car as a Cohesive unit/system.
"Responsibility of an object is a role it performs in a overall system". e.g. Fuel Tank has a responsibility of holding fuel.
This concludes discussion about an object for the time being, In next post we will explore the Buzzwords in OOPs & their actual meaning. Subsequently we will see what are common interview questions on this topic as we are in the final stages in the discussion on OOPs .Till then...Bye Bye...Stay tuned.............
Car carOne=new Car();
Car carTwo=new Car();
Don't worry too much how these Java classes are compiled & executed
Note for C Programmers: For the time being assume 'new' similar to 'malloc(...) ' function
carOne & carTwo objects of type Car.carOne has some state(values of data members) & carTwo has some other State although behavior(methods) is common as they are of type Car.
Few important things to note from example are
1) We are changing State of any object by "Invoking methods" & do not change data members directly.
In CarDemo class we have created object carOne, carTwo & then we have invoked method to set the name of a car , change a gear, accelerate, applying breaks which intern changes name, gear, speed etc. data member values which is revealed through output.
2) Hiding internal state of an object & allowing other objects to change the state through method interaction is known as Data Encapsulation(Very important aspect of Object Oriented Programming ....More on this later)
Apart from state & behavior object has two additional characteristics namely Identity & Responsibility.
So intern any Object has four distinct Characteristics:
1) State
2) Behavior
3) Identity
4) Responsibility
We will understand this with same exapmle 'Car'
State:
When we say Car has a certain state (travelling with certain speed or at rest), we are actually specifying current values of its attributes/data members (Speed, Fuel Level,Make, Type of Fuel, Tyre Pressure etc..), So In OOPs paradignm we will define State of an Object as "State of an object is values of its data member at any point of time". State I hope this is clear as we have discussed this number of times & i don't want to hang you on the same point too long.
Behavior:
Object has an behavior which interrn changes State of an Object.In our Car example when we talk about accelarate as a behavior of an object (we represent behavior as a method in a class) it increases value of 'speed' data member & reduces value of Fuel Level (for the sake of example... In our class we don't have this attribute). Behavior could be expressed as "How certain object acts/reacts in terms of its State change/operations performed on it"
In summary , For a car object what are the possible operations we can perform & how car will react by means of its state change defines Behavior of a car.
Identity:
Term is self explanatory i guess, Identity is the attribute/property of an object which will uniquely distinguish it from all other similar objects. e.g. If there are 10 cars of same company, same model, same color are parked in front of a building how one could distinguish them?......Yes you are right by their Number Plates....So registration number/ some number given by state authority/whomsever is a property which distinguishes one car from other even if all other attributes are same.....right? Kindly don't assume that Identity has to be a single property in some cases it could be group of properties which will act as identity of an object. e.g. If i consider Bank object perhaps only account number can't act as identity we have to consider bank name as well, Since there is a high chance that there will be a bank account number '25000' existing in Bank A & Bank B.
Responsibility:
In our example of Car, we could design the entire Car class which will represent actual car object of real world.....right?...In that case we could have say that Car as a whole system is composed of several objects e.g. Engine, Windows,Doors,Fuel tank etc...Ok? so in this whole system each object has some responsibility so that Car as a Cohesive unit/system.
"Responsibility of an object is a role it performs in a overall system". e.g. Fuel Tank has a responsibility of holding fuel.
This concludes discussion about an object for the time being, In next post we will explore the Buzzwords in OOPs & their actual meaning. Subsequently we will see what are common interview questions on this topic as we are in the final stages in the discussion on OOPs .Till then...Bye Bye...Stay tuned.............
Friday, December 29, 2006
What is a Class?
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
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 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.
Subscribe to:
Posts (Atom)