Tuesday, 10 July 2012

OOPs concepts with real time examples



OOPS Concepts!!
Structures Vs Classes:
You must be familiar with the above terms but may not know exactly how it differs.
-->Both are used as a container to store variables.'
-->Structures use stack allocation and Classes use Heap allocation.
-->Structures cannot be extended so use accordingly.
-->It is better to use reference type in classes rather than passing the entire class itself,but structures are passed as such.
-->"Protected" is absent in structures.
-->Structure default mode is PUBLIC.And for class it is PRIVATE.
Guess all think you are well versed in this OOPS,but are u really???!!U all mst be well versed with the definition and stuff but what is it actually?
OOPS is all used to map the real life objects in to a language which the computer can interpret.
Consider,Like mapping the shop in real time to a "shop class".The shop may be a hotel,textile shop or anything fr that matter .This mapping and understanding is what comes in OOPS.
Lets begin with Classes
Class-A class is something in real life which can be bundled together as a group of attributes and associated behaviour.
Eg)A shop consists of attributes owner,no_of_emp,area,no_of_floors etc,all this can be bundles together to a shop class.
Object-Class Shop is just a model,you cannot access attributes without an entity to access it.
"Shop class" does not specify anything  like a hotel or textile shop.In order to access the attributes mentioned we have to create an OBJECT of the class.
Eg in this case must be Hotel,textile shop objects.
Now u can access hotel.area,hotel.no_of_emp etc.
Aggregation And Composition:
Aggregation :"HAS A" relation.It may or may not have.
Eg)A company has a free snack booth--------------A company can exist even without a snack booth.This is an aggregation
Composition:"HAS A"relation.It is compulsory.For instance
Eg)A company has employees-----------------------A company cannot exist without employees.Employees are inseperable frm company.
Abstraction:
--->Show only the necessary details to the client of your object.
--->So the client who is using your class need not to be aware of the inner details like how your class do the operations.
Consider you are starting your scooty,all you need is to press the starter the rider need not know about how the engine triggers and starts.
Encapsulation:
   Encapsulation is covering one thing completely within another.Class enables encapsulation in OOPS.For instance consider a "Paying guest" class which encapsulates the details(attributes )like name,address,ph_no,rent,no_of_mem,owner etc.All the attributes are covered within the Paying Guest class and can be accessed with its objects..
Polymorphism:
polymorphism is a phenomenon in which one object say "A" can appear to be like another object "B".
eg)consider a company which can also be considered as outsourcing company,service based,product based company.This exists as a result of Inheritance in OOPS.
Another case where polymorphism exists is when the same fn can be implemented in a completely different way yet does the same function at the end of the day.
Eg)Consider i am logging into the company,i can either use the access card as per the new technology or i can register the "IN" and "OUT" time in a register as is the traditional way.But the function of keeping a record of in and out time with the emp id is done successfully.
login(access card)  and login(pendown_in_register) are polymorphic.

Association:

It is the relation that exists between the object of one class with the object of another.No object can exist independently without any relation to another object.Such an independent object has no value in OOPS as far as im concerned.So association plays an important role.
Association can either be
-->One to One
-->One to Many
-->many-many
Consider this eg
Suppose Employee is a class and Cab is a class.Objects of employee are e1,e2,e3,and that of cabs are c1,c2,c3.
If e1 can use c1,c2,c3 .Then from the e1 point of view it is one-many,from c1 point of view it is one-one----------OBJECT POINT OF VIEW.
In employee point of view it is one -many and cab point of view it is many-one-------------------------------------------CLASS POINT OF VIEW.
If e1,e1,e2 can use c1,c2,c3 then from the cab and employee pt of vw it is many to many,but frm the objects point of vw it is ONE-MANY.
Thus in object point of view it can either be only 1-many or 1-1 only because it is with respect to 1 object.
This completely explains association.

Inheritance:

  It is the process of deriving classes from a base class.The derived class has an additional functionality other than the base class functionality.

-->Enables polymorphism

Types
-->single
-->multilevel
-->multiple
-->hierarchial
Eg)Consider 2 classes teachers_knowledge and students_knowledge
Here the student_knowledge is extended or inherited from the teachers_knowledge.In that case the student acquires teachers knowledge+all the knowledge he acquired thro his life,experience,web etc.
This goes as follows:
class teachers_knowledge
{
};
class student_knowledge:public class teachers_knowledge
{
};
Hope you got its real picture..



- Sourcebits University
Cloud Computing
www.sourcebits.com

1 comment:

  1. AvaHost is ultimately one of the best hosting provider for any hosting services you require.

    ReplyDelete