Anda di halaman 1dari 5

UNIT-I Object Oriented Thinking

UNIT-I Object Oriented Thinking

Need for Object Oriented paradigm: When computers were first invented, programming was done manually by toggling in a binary machine instructions by use of front panel. As programs began to grow, high level languages were introduced that gives the programmer more tools to handle complexity. The first widespread high level language is FORTRA . Which gave birth to structured programming in !"#$%s. The &ain problem with the high level language was they have no specific structure and programs becomes larger, the problem of complexity also increases. 'o ( became the popular structured oriented language to solve all the above problems. )owever in 'O*, when pro+ect reaches certain si,e its complexity exceeds. 'o in !"-$%s a new way of programming was invented and it was OO*. OO* is a programming methodology that helps to organi,e complex programs through the use of inheritance, encapsulation . polymorphism. OO* is a Revolutionary idea totally unli/e anything that has came before in programming OO* is an evolutionary step following naturally on the heels of earlier programming abstractions.

A way of viewing world:

0t means how we handle the real world situations through OO* and how we could ma/e the computer closely model the techni1ues. 2g3 Ra+u wished to send some flowers to his friend for his birthday . he was living some miles away. )e went to local florist and said the /ind of flowers. )e want to send to his friend%s address. And florist assured those flowers will be sent automatically.

The 'tructure of OO* is similar to that of a community, that consists of agents interacting with each other. These agents are also called as ob+ects. An agent or an ob+ect plays a role of providing a service or performing an action, and other members of this community can access these services or actions. (onsider an eg, ra+u and ravi are good friends who live in two different cities far from each other. 0f Ra+u wants to send flowers to ravi, he can re1uest his local florist 4hari% to send flowers to ravi by giving all the information along with the address. )ari wor/s as an agent 5or ob+ect6 who performs the tas/ of satisfying ra+u%s re1uest. )ari then performs a set of operations or methods to satisfy the re1uest which is actually hidden from ravi, )ari forwards a message to ravi%s local florist. Then local florist as/s a delivery person to deliver those flowers to ravi. The ob+ects or agents helping ra+u in solving the problem of sending flowers to his friend ravi can be shown in figure.



UNIT-I Object Oriented Thinking

A fundamental concept in OO* is to describe behavior in terms of responsibilities. A Re1uest to perform an action denotes the desired result. An ob+ect can use any techni1ue that helps in obtaining the desired result and this process will not have any interference from other ob+ect. The abstraction level will be increased when a problem is evaluated in terms of responsibilities. The ob+ects will thus become independent from each other which will help in solving complex problems. An Ob+ect has a collection of responsibilities related with it which is termed as 4protocol% The Operation of a traditional program depends on how it acts on data structures. Where as an OO* operates by re1uesting data structure to perform a service.

&essages . &ethods3
When a message is passed to an agent 5or ob+ect6 that is capable of performing an action, then that action will be initiated in OO*. An ob+ect which receives the message sent is called 4receiver%. When a receiver accepts a message, it means that the receiver has accepted the responsibility of processing the re1uested action. 0t then performs a method as a response to the message in order to fulfill the re1uest.

(lasses . 0nstances3
A Receiver%s class determines which method is to be invo/ed by the ob+ect in response to a message. When similar messages are re1uested then all the ob+ects of a class will invo/e the same method. All ob+ects are said to be the instances of a class. For e.g., 0f 4flower% is a class then Rose is its instance

(lass )ierarchies 50nheritance63

0t is possible to organi,e classes in the form of a structure that corresponds to hierarchical inheritance. All the child classes can inherit the properties of their parent classes. A parent class that does not have any direct instances is called an abstract class. 0t is used in the creation of subclasses.



UNIT-I Object Oriented Thinking

2.g.3 7et 4)ari% be a florist, but florist more specific form of shot /eeper. Additionally, a shop /eeper is a human and a human is definitely a mammal. 8ut a mammal is an animal . animal is material ob+ect. All these categories along with their relationships can be represented using a graphical techni1ue shown in figure. 2ach category is regarded as a class. The classes at the top of the tree are said to be more abstract classes and the classes at the bottom of the tree are said to be more specific classes. 0nheritance is a principle, according to which /nowledge of a category 5or class6 which is more general can also be applied to a category which is more specific. Method inding: When the method is super class have same name that of the method in sub class, then the subclass method overridden the super9class method. The program will find out a class to which the reference is actually pointing and that class method will be binded. 2.g.
class parent : void print56 : > > class child extends parent : void print56 : 'ystem.out.println5;From (hild<6= > > class 8ind 'ystem.out.println5;From *arent<6=



UNIT-I Object Oriented Thinking

: *ublic static void main5'tring arg?@6 : child obAnew child56= ob.print56= > >


From (hild

The child%s ob+ect 4ob% will point to child class print56 method thus that method will be binded.

Overriding: When the name and type of the method in a subclass is same as that of a method in its super class. Then it is said that the method present in subclass overrides the method present in super class. (alling an overridden method from a subclass will always point to the type of that method as defined by the subclass, where as the type of method defined by super class is hidden. 2.g. 5above %method binding% example6

!"ception#: 2xception is a error condition that occurs in the program execution. There is an ob+ect called 42xception% ob+ect that holds error information. This information includes the type and state of the program when the error occurred. 2.g. 'tac/ overflow, &emory error etc

$%mmary of OO& concept# 'propo#ed by Alan kay(:

!. 2verything is an object. C. (omputation is performed by ob+ects communicating with each other, re1uesting that other ob+ects perform actions. Ob+ects communicate by sending . receiving messages. A message is a re1uest for an action bundled with whatever arguments may be necessary to complete the tas/. D. 2ach ob+ect has its own memory, which consists of other ob+ects. E. 2very Ob+ect is an instance of class. A class simply represents a grouping of similar ob+ects, such as integers or lists. F. The class is the repository for behavior associated with an ob+ect. That is all ob+ects that are instances of same class can perform the same actions. #. (lasses are organi,ed into a singly rooted tree structure, called inheritance hierarchy.



UNIT-I Object Oriented Thinking

)opyng with comple"ity: When programs are larger then complex is more programmers found it difficult to remember all the information they needed to /now in order to develop or debug their software. on97inear behavior of complexity3 When pro+ects become larger, a tas/ that can be solved by a programmer in C months can%t be accomplished by C programmers wor/ing for one month. The reason for this non9linear behavior is complexity. The inter connections between the software components is complicated. The OO* designers illustrated this with a memorable phrase ;The bearing of a child ta/es nine months, no matter how many women are assigned to the tas/< Ab#traction Mechani#m#: 0n general to reduce the complexities in OO* we use abstraction mechanism. Abstraction is the ability to encapsulate, isolate design and execution the information. Ob+ect oriented techni1ues can be seen as a natural outcome of a long historical progression from procedures, to modules, to abstract datatypes and finally to ob+ects.

*rocedures are the functions that allowed tas/s to be executed repeatedly. The procedure gave the
first possibility for information hiding.

&odules are the independent parts of program. A module provides the ability to divide the program
into two parts. The public part is accessible outside the module. The private part is accessible only with in the module.

An abstract data type is a programmer defined datatype that can be manipulated li/e the system
defined datatypes. Ob+ects are instances of a class. Ob+ect oriented programming adds several important new ideas. Among them, message passing, inheritance and polymorphism are important. &essage passing is the idea that the interpretation of a message can vary with different ob+ects. 0nheritance allows different datatypes to share same code, and increase the functionality. *olymorphism allows this shared code to be tailored to fit t he specific circumstances of individual datatypes.