Wednesday, 24 April 2013

OOPS Concept With Real Life Example


OOPS Concept With Real Life Example


This is the most asked Question in a technical interview in any domain.. OOPs Concept is very very important.. Today I will explain OOPs concept with real Life Example that will help you to grasp the concept well and excel in the interviews...

Objects: Object is the basic unit of object-oriented programming.Objects are identified by its unique name. An objectrepresents a particular instance of a class. There can be more than one instance of an object. Each instance of an object can hold its own relevant data.

An Object is a collection of data members and associated member functions also known as methods.

Classes: Classes are data types based on which objects are created.Objects with similar properties and methods are grouped together to form a Class. Thus a Class represent a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects becomes functions of the class and is referred to as Methods.

Example #1:

For example consider we have a Class of Cars under which Santro Xing, Alto and WaganR represents individual Objects.In this context each Car Object will have its own, Model,Year of Manufacture, Colour, Top Speed, Engine Power etc.,which form Properties of the Car class and the associated actions i.e., object functions like Start, Move, Stop form the Methods of Car Class.No memory is allocated when a class is created. Memory is
allocated only when an object is created, i.e., when an instance of a class is created.

Example #2:

An architect will have the blueprints for a house....those blueprints will be plans that explain exactly what properties the house will have and how they are all layed out.  However it is just the blueprint, you can't live in it.  Builders will look at the blueprints and use those blueprints to make a physical house.  They can use the same blueprint to make as many houses as they want....each house will have the same layout and properties.  Each house can accommodate it's own families...so one house might have the Smiths live in it, one house might have the Jones live in it.

The blueprint is the class...the house is the object.  The people living in the house are data stored in the object's properties.

Abstraction: Abstraction means showing essential features and hiding non-essential features to the user.

For Eg.  Yahoo Mail...

When you provide the user name and password and click on submit button..It will show Compose,Inbox,Outbox,Sentmails...so and so when you click on compose it will open...but user doesn't
know what are the actions performed internally....It just Opens....that is essential; User doesn't know internal actions ...that is non-essential things...

For Eg. Tv Remote..
Remote is a interface between user and tv..right. which has buttons like 0 to 10 ,on /of etc but we dont know circuits inside remote...User does not  need to know..Just he is using essential thing that is remote.
 

Encapsulation: Encapsulation means which binds the data and code (or) writing operations and methods in single unit (class).
 
For Example:
A car is having multiple parts..like steering,wheels,engine...etc..which binds together to form a single object that is car. So, Here multiple parts of cars encapsulates itself together to form a single object that is Car.

In real time we are using Encapsulation for security purpose...

Encapsulation = Abstraction + Data Hiding.


Inheritance: Deriving a new class from the existing class,is called Inheritance.
Derived(sub class) class is getting all the features from Existing (super class\base class) class and also incorporating some new features to the sub class.

For Eg.,

class Address
{
String name;
Srting H.no;
String Street name;
}
class LatestAddress extends Address
{
String City;
String State;
String Country;
}
public class Vishal
{
{
LatestAddress la = new LatestAddress();
//Assign variable accordingly...
}
}

In the above Example class LatestAddress getting all features from the Address class.
In the LatestAddress class we have total 6 properties..3 are inherited from Address class and 3 properties are
incorporated. So In the class Vishal we are declaring the object of class LatestAddress and then assign new variables using the properties of the previous base classes... So this is a nice example of inheritance..

Polymorphism :

Polymorphism means ability to take more than one form that an operation can exhibit different behavior at different instance depend upon the data passed in the operation.

1>We behave differently in front of elders, and friends. A single person is behaving differently at different time.

2> A software engineer can perform different task at different instance of time depending on the task assigned  to him .He can done coding , testing , analysis and designing depending on the task assign and the requirement.

3> Consider the stadium of common wealth games. Single stadium but it perform multiple task like swimming, lawn tennis etc.

4> If a girl is married and mother of 2 children doing teaching job then  she is a women first ,, teacher in a school when she is in school,,wife of someone at home,, mother of her children,, and obvious daughter of someone & may be girl friend of someone (just kidding) means a woman plays diffent roles at different times dats the polymorphism (many forms).

Summary:

OOPs have following features:

1. Object             - Instance of Class
2. Class               - Blue print of Object
3. Encapsulation    - Protecting our Data
4. Polymorphism   - Different behaviors at different instances
5. Abstraction        - Hiding our irrelevant Data
6. Inheritence        - One property of object is acquiring to another property of object

116 comments:

  1. good examples...
    very useful for beginners....

    ReplyDelete
  2. good and it is easy to learn and understand

    ReplyDelete
  3. Nice job vishal:)
    keep going on!!!

    ReplyDelete
  4. very clear example i have ever seen before...

    ReplyDelete
  5. Good example and good pratices

    ReplyDelete
  6. Very very clear oops concept.......thanks u very much sir.

    ReplyDelete
  7. really very good examples................
    interesting + understanding

    ReplyDelete
  8. Good Explanation and Very clear.
    Thanks a lot.

    ReplyDelete
  9. nice examples particular girl example nice..

    total explanation very clear and awesome

    ReplyDelete
  10. thanks for the simple and clear explanation

    ReplyDelete
  11. Very good explanation...!!! Keep posting...!!!

    ReplyDelete
  12. very nice examples

    ReplyDelete
  13. Thnx sir.....it is really helpful....

    ReplyDelete
  14. All the concepts are well explained....good job!

    ReplyDelete
  15. very good examples .. Nice post ..

    ReplyDelete
  16. Nice examples to understanding...thank you

    ReplyDelete
  17. Mr Kartikeyan asked the following Question:

    ============================================================

    "I read your article about oops concept that is really nice sir.

    I have one doubt sir that is mentioned below plz clear as soon as possible


    class - " The blueprint of house "

    object - " The physical houses which was build using house blueprint"

    attributes - " The people who living in the house are data of the object properties"

    methods - " methods ?"

    Give me one example which comprises of above all?"

    ============================================================

    The answer to Mr Kartikeyan's Question is as follows:

    "Methods are action which an object is able to perform.. For example in the above example.. The people living the house are able to perform certain actions like repair their house, Paint it. So the actions like repairing and Painting are all methods for the objects."

    ReplyDelete
  18. thank you,its very useful and easy to understand.i need the concept of function overloading/overriding with real time example.....

    ReplyDelete
  19. Nice one. Beautifully exampled.Thanks. Keep it going.

    ReplyDelete
  20. now oops concepts seems to be vry simple to understand nd learn..

    ReplyDelete
  21. very interesting to learn and good examples keep on posting such easy ways to learn basics thanq very much

    ReplyDelete
  22. Good Explainaotion of OOPS Concepts

    ReplyDelete
  23. OOPS concepts are explained in such a way that beginners can easily understand and remember

    ReplyDelete
  24. thanks!!!!! really helpful :) :)

    ReplyDelete
  25. really nice examples......easy to understand....

    ReplyDelete
  26. Thank you...its a very clear and nice explanation

    ReplyDelete
  27. Thanku sir,i understand very clearly.

    ReplyDelete
  28. Thanku sir,i understand well with your explanation.

    ReplyDelete
  29. Easy to understand. very good

    ReplyDelete
  30. thanx a billian ....gr8 examples

    ReplyDelete
  31. Good Examples..Thanks for sharing..its realy helpful for me..

    ReplyDelete
  32. Miss Sangeeta Korade asked the following Question:

    "Explain the four major pillars of oops taking keyboard as an example?"

    Answer to her Question:

    The Four major Pillars of OOPs are:

    1. Abstraction
    2. Encapsulation
    3. Inheritance
    4. Polymorphism

    Abstraction:

    For Example, In a keyboard, we have circuits and It is connected to the CPU. So While pressing the key we dont know about the inside happenings of the keyboard. We only know that When the keyboard is pressed It will display a similar letter in the screen incase we are typing. So this is how Abstraction works. Abstraction ie., Exposing necessary data

    Encapsulation:

    Next When the key is pressed some kind of message runs through the circuit of the keyboard which is sent to the CPU.CPU analyses that request and accordingly displays the pressed text in the screen. So here we only know that pressing the key will display the letter but In the meanwhile we are totally unaware of the happening inside the circuits. This is Encapsulation ie.,emphasis on hiding the data

    Inheritance:

    There are many keyboards brands available in the market but the similarity between them is that they follow more or less the same prototype as the Keyboard available in the year 2000. So basically they are inheriting the properties of the older generation of keyboards plus they are modifying the newer keyboards with some extra functionalities..

    Polymorphism:

    Polymorphism means Poly[Many] + Morphism[Forms]. It is the ability of the object to behave differently under different situation.. For Example while writing a word document you will be using the up/down arrows for moving up and down in the document but while playing a car game u will be using the same keys for moving your car in the game. So the same keys are behaving differently on various scenarios and this is called polymorphism.

    ReplyDelete
    Replies
    1. awesome reply sir. you must be the "father of examples" in computer world..thanks again for such a great explanation.

      Delete
    2. very greatly explanations in one example!great!

      Delete
    3. i didn't find explanation like this anywhere...thanku so much

      Delete
    4. Awesome explaination sir... Thank you for your easy explaination.

      Delete
  33. its really superb. tanx for ur post vishal

    ReplyDelete
  34. thank yo sir....it seasy to understand

    ReplyDelete
  35. Very good explanation with Simple Examples..!!
    Keep posting...!!!

    ReplyDelete
  36. nice defination and real life example

    ReplyDelete
  37. Very easy to understand.. great job Vishal. Thank you so much..

    ReplyDelete
  38. really very easy defnition and good examples

    ReplyDelete
  39. explanation is too gud. I'm a beginner so this examples of oops helps me alot...

    ReplyDelete
  40. everything is easy to learn with real world examples. thank you for finding these example, VISHAL...... and easy language... Thank You Very Much..

    ReplyDelete
  41. Very Good Explanation...and easy to understand...Thank U so Much..

    ReplyDelete
  42. Very Good Explanation...and easy to understand...Thank U so Much..

    ReplyDelete
  43. Very Good Explanation...and easy to understand...Thank U so Much..

    ReplyDelete
  44. Awsummmmmmmm vishal!! God bless!!

    ReplyDelete
  45. Great explanation

    ReplyDelete
  46. hai sir,myself perumal, Thank u so much, no one can published this kind of explanation....am asking u once again thank your very much....

    ReplyDelete
  47. Thanks for that.........Great

    ReplyDelete
  48. I am Vijay here. After many years, I understood , what is Object Oriented Programming. Thanks a Ton. A person , who can explain something , through very simple , day to day examples, knows the subject. You are master.

    ReplyDelete
  49. Superb...thanku

    ReplyDelete
  50. Good Explaination....

    ReplyDelete
  51. very nice explanation its useful for me thanks a lot sir..

    ReplyDelete
  52. Awsome post Dear Keep Guiding us :)

    ReplyDelete
  53. Very nice explanation with example.Carry on your good work.

    ReplyDelete
  54. Vishal you have really made a very good effort in putting the examples in layman terms!!!!
    Please continue the good work.

    ReplyDelete
  55. Very nice sir, easily understanding with simple examples...

    ReplyDelete
  56. Master of explaination.For which the average person can understand.

    ReplyDelete
  57. excellent job, sir
    keep up the good work.

    ReplyDelete
  58. good examples to understand oops concept

    ReplyDelete
  59. superbly explain..!!!!! concepts cleared now.

    ReplyDelete
  60. Really superb brother ... nice

    ReplyDelete
  61. Thank you for the example.... Now i really the concept of OOP....

    ReplyDelete
  62. Excellent Explanation
    Really very useful,
    Thank you...

    ReplyDelete
  63. Nice examples helps me lot as I am beginner
    Thanks Vishal sir....

    ReplyDelete
  64. nicely explained

    ReplyDelete
  65. Excellent Explanation
    Really very useful,
    Thank you

    ReplyDelete
  66. Really nice example sir and thankyou this website is very useful for freshers....

    ReplyDelete
  67. good job,simplest way to undersatnd

    ReplyDelete
  68. Nice examples. Thank you so much...

    ReplyDelete
  69. Good Explanation thanks a lot...

    ReplyDelete
  70. Good Explanation Thanks a lot..

    ReplyDelete
  71. Ashwini Meshkar3 August 2017 at 12:11

    thank you sir..this explanation helped alot

    ReplyDelete
  72. thank you vishal. ur explanation is soooo gud

    ReplyDelete
  73. Nice Explanation and especially those examples are very helpful.

    ReplyDelete
  74. Good examples very easy to understand

    ReplyDelete

Thank You for Your Comments. We will get back to you soon.

back to top