Infolink

 

Search This Blog

Nov 16, 2012

OOPS Concept with Real-world example

Introduction

OOP is Nothing but Object Oriented Programming.According to Wikipedia,  Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs.


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 diff. instances
5. abstraction    - Hidding our irrelavance data
6. inheritence    - one property of object is aquring to another property of object

1. Object
Basically an object is anything that is identifiable as an single material item. You can see around and find many objects like Camera, Monitor, Laptop etc. In OOP perspective, an object is nothing but an instance of a class that contains real values instead of variables

2. Class

A class is a template definition of the methods and variables for a particular kind of object. In other words, class is the blue print from which an individual objects are created.

every human has eye ,so eyecolor can be considered as the property of human being which can be encapsulted as a data in our class Human

class Human
{
    private:
    EyeColor IColor;
    NAME personname;
};

Consider object of class of

                Human myhuman;

we want set myhuman's name as "linto" and IColor as "black", For that we want methods to do that task.

So need methods for class to do a particular task on the data

class Human
{
    private:
    EyeColor IColor;
    NAME personname;
    public:
    void SetName(NAME anyName);
    void SetIColor(EyeColor eyecolor);
};

3. Abstraction

Abstraction is a process of identifying the relevant qualities and behvaiors an object should possess. Lets take an example to understand abstraction. A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antena, web camera, usb ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antena, speakers works.  You just need to know how to operate the laptop by switching it on. The intrinsic details are invisitble. Think about if you would have to call to the engineer  who knows all internal details of the laptop before operating it. This would have highly expensive as well as not easy to use everywhere by everyone. So here the Laptop is an object that is designed to hide its complexity.

Think If you need to write a piece of software to track the students details of a school, you may probably need to create Students objects. People comes in all different backgrounds, educational qualifications, locations, hobbies, ages and have multiple religion, language but in terms of application, an student is just a name, age, class and roll number, while the other qualities are not relevant to the application. Determining what other qualities (background, qualifications, location, hobbiels etc) are in terms of this application is abstraction.

In object-oriented software, complexity is managed by using abstraction. Abstraction is a process that involves identifying the critical behavior of an object and eliminating irrelevant and complex detilals. A well thought-out abstraction is usually simple, and easy to use in the perspective of the user, the person who is using your object.

4. Encapsulation

Encapsulation is a method for protecting data from unwanted access or alteration by packaging it in an object where it is only accessible through the object's interface. Encapsulation are often referred to as information hiding. But both are different. Infact information hiding is actually the result of Encapsulation. Encapsulation makes it possible to separate an object's implementation from its orgiinal behavior - to restrict access of its internal data. This restriction facilitate certains detiails of an object;s behavior to be hidden. This allows to protect an object's interal state from corruption by its user.

It is the mechanism by which Abstraction is implemented. In other words you can say that it is the result of the Encapsulation. For example,  the Laptop is an object that encapsulates many technologies/hardwares that might not be understood clearly by most people who use it.
Inheritance

5. Inheritance

Inheritance is the ability to define a new class or object that inherits the behaviour and its functionality of an existing class. The new class or object is called a child or subclass or derived class while the original class is called parent or base class. For example, in a software company Software Engineers, Sr. Software Engineers, Module Lead, Technical Lead, Project Lead, Project Manager, Program Manager, Directors all are the employees of the company but their work, perks, roles, responsibilities differs. So in OOP, the Employee base class would provide the common behaviours of all types/level of employee and also some behaviours properties that all employee must have for that company. The particular sub class or child class of the employee would implement behaviours specific to that level of the employee. So by above example you can notice that the main concept behind inheritance are extensibility and code reuse (in this case you are extending the Employee class and using its code into sub class or derived class).

6. Polymorphism

As name suggests, Polymorphism means an ability to assume different forms at different places. In OOP, it is a language's ability to handle objects differently based on their runtime type and use. Polymorphism is briefly described as "one interface, many implementations".Ppolymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

There are two types of polymorphism.
  •     Compile time polymorphism - It is achieved by overloading functions and operators
  •     Run time polymorphism - It is achieved by overriding virtual functions
Lets say you have a class that have many Load methods having different parameters, this is called Compile time polymorphism. Lets take another example where you have a virtual method in the base class called Load with one parameter and you have redefined its functioanlity in your sub class by overriding base class Load method,  this is called Run time polymorphism.

33 comments:

  1. This is very helpful in understanding the simple concepts of OOP

    ReplyDelete
  2. Thanks for finding this article helpful. please comment for other topics that are not in my blog.

    ReplyDelete
  3. inheritance:
    --> parent child relationship is one of the example for inheritance..
    --> child has been derived from parent..

    encapsulation:
    --> cpu holds all the parts like hard-disk, floppy disk, USB drive bla bla....

    abstraction:
    --> for example vehicle has start and stop that is an abstract..but all the vehicles(car, bike) can have to start or stop..

    polymorphism:
    --> you are ordering the pizza for that you having two options for paying one is online payment(compile time) and another one is payment at delivering (run time)

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Thanks..its really helpful for interview....could you provide explanation about what is difference between
    Encapsulation and Abstraction.

    ReplyDelete
  6. Abstraction Encapsulation
    1. Abstraction solves the problem in the design level. 1. Encapsulation solves the problem in the implementation level.
    2. Abstraction is used for hiding the unwanted data and giving relevant data. 2. Encapsulation means hiding the code and data into a single unit to protect the data from outside world.
    3. Abstraction lets you focus on what the object does instead of how it does it. 3. Encapsulation means hiding the internal details or mechanics of how an object does something.
    4. Abstraction- Outer layout, used in terms of design.
    For example: Outer Look of a Mobile Phone, like it has a display screen and keypad buttons to dial a number. 4. Encapsulation - Inner layout, used in terms of implementation.
    For example: Inner Implementation detail of a Mobile Phone, how keypad button and Display Screen are connect with each other using circuits.

    ReplyDelete
  7. Explain the OOPS Concept with easy Example.................

    ReplyDelete
  8. Really nice and informative thanks for sharing this.

    Web Development Company

    ReplyDelete
  9. Good Post. Easy To Use. THANX.

    ReplyDelete
  10. Hi I’m Rahul doing my final year BE in computer. I have not got placed in any campus recruitment, so planning to go for Selenium Training in Chennai. So kindly help me by guiding on where I could do Selenium Training in Chennai which also helps in placement services.

    ReplyDelete
  11. I found some useful information in your blog, It was awesome to read, thanks for sharing this great content to my vision, keep sharing.. Oracle Training in chennai

    ReplyDelete
  12. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    Oracle Training In Chennai

    ReplyDelete
  13. Real life example of polymorphism



    Suppose you are in home at that time you behave like son or father if you are in mall at that time you behave like customer and if you are in school then behave like student.

    ReplyDelete
  14. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
    Informatica Training In Chennai
    Hadoop Training In Chennai
    Oracle Training In Chennai
    SAS Training In Chennai

    ReplyDelete
  15. It Was really Helpful thank you...!

    ReplyDelete
  16. explain encapsulation with simple real time example

    ReplyDelete
  17. thanku for sharing ths nice posts..

    Informatica training, in the recent times has acquired a wide scope of popularity amongst the youngsters at the forefront of their career.
    Informatica online training in hyderabad


    ReplyDelete
  18. Nice and it is really good and very useful information. Thank you for sharing it.
    eCommerce Solution Provider India

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...