Vous êtes sur la page 1sur 6

Unit I

Learning Objectives:
(Object Oriented Thinking: - Need for oop paradigm. A way of viewing world- Agents !esponsibility
messages methods classes instances class hierarchies"#nheritance$ method binding overriding and
e%ceptions s&mmary of oop concepts coping with comple%ity abstraction mechanisms.)
Procedural programming can sometimes be &sed as a synonym for imperative
programming "specifying the steps the program m&st take to reach the desired state$ b&t can also refer
"as in this article$ to a programming paradigm derived from str&ct&red programming based &pon the
concept of the procedure call. 'roced&res also known as ro&tines s&bro&tines methods or f&nctions
"not to be conf&sed with mathematical f&nctions b&t similar to those &sed in f&nctional programming$
simply contain a series of comp&tational steps to be carried o&t. Any given proced&re might be called at
any point d&ring a program(s e%ec&tion incl&ding by other proced&res or itself.
Object-oriented programming "OOP$ is a programming paradigm &sing )objects) * data
str&ct&res consisting of data fields and methods together with their interactions * to design applications
and comp&ter programs. 'rogramming techni+&es may incl&de feat&res s&ch as data
abstraction encaps&lation messaging mod&larity polymorphism and inheritance. ,any
modern programming lang&ages now s&pport OO' at least as an option.
Comparison between Procedural Programming and Object Oriented Programming
The foc&s of proced&ral programming is to break down a programming task into a collection
of variables data str&ct&res and s&bro&tines whereas in object-oriented programming it is to break
down a programming task into data types "classes$ that associate behavior "methods$ with data
"members or attrib&tes$. The most important distinction is whereas proced&ral programming &ses
proced&res to operate on data str&ct&res object-oriented programming b&ndles the two together so an
)object) which is an instance of a class operates on its )own) data str&ct&re.
Nomenclat&re varies between the two altho&gh they have similar semantics:
Procedural Object-oriented
proced&re method
record object
mod&le class
proced&re call message
Agent:
Agent is a b&--word that(s been making the ro&nds a lot lately. .asically an agent is a mobile &nit of
code and data that(s shipped aro&nd to vario&s machines.
There are Four Agent Classes in Java.
Agent which is the java class that(s shipped back and forth over the network. Note that this class
doesn(t &se a remote interface/ the class itself and its data are seriali-ed and sent to the client.
AgentFactory which is the remote interface for the Agent0actory#mpl class. This interface is invoked
by the client and the calls are passed to the server via !,#.
AgentFactoryImpl which is the server-side implementation class. #t creates instances of the Agent class
and sends them to the client on re+&est and also accepts instances of the Agent class as arg&ments and
interrogates it for changes to data.
AgentClient which is the client program. #t looks &p an instance of the Agent0actory interface on a
remote server "well act&ally #(ve hardcoded )localhost) in this e%ample b&t really it co&ld point to any
remote server yo& like$ calls getAgent"$ to get an instance of the agent calls the agent(s comp&te"$
method to perform whatever calc&lations the agent wants to do "note that the client doesn(t have to
know e%actly what the agent does/ it j&st has to r&n it$ and then sends the &pdated agent back to the
server.
esponsibilit!:
1. The whole concept of an object is that it contains both attrib&tes and behaviors. #n str&ct&red
programming there is code and there is data and the two are tho&ght of as separate entities. The power
of an object is that these two separate entities are encaps&lated together to form a single atomic entity. #t
is in this f&ndamental concept of an encaps&lated atomic entity that yo& come to reali-e how and why
an object is responsible for itself.
2. A straightforward definition for object-responsibility is this: An object must contain the data (attributes)
and code (methods) necessary to perform any and all services that are reuired by the object. This
means that the object m&st have the capability to perform re+&ired services itself or at least know how to
find and invoke these services. !ather than attempt to f&rther refine the definition take a look at an
e%ample that ill&strates this responsibility concept.
"essage:
A message in its most general meaning is an object of comm&nication. #t is a vessel which provides
information.
There are two main senses of the word )message) in comp&ter science: messages passed within software which
may or may not be h&man-readable and h&man-readable messages delivered via comp&ter software for person-
to-person comm&nication.
,essage passing is a form of comm&nication &sed in conc&rrent and parallel comp&ting object-
oriented programming and interprocess comm&nication where comm&nication is made by sending
messages to recipients. #n a related &se of this sense of a message in object-oriented programming
lang&ages s&ch as 3malltalk or 4ava a message is sent to an object specifying a reuest for action.
#nstant messaging and e-mail are e%amples of comp&ter software designed for delivering
h&man-readable messages in formatted or &nformatted te%t from one person to another.
"et#od:
,ethod "comp&ter programming$ is a piece of code associated with a class or object to perform a task.
#n object Oriented 'rogramming each Object has some properties and behavior ",ethod$.
0or e%ample 5og is an object for Animal 6lass. #t has properties like color height c&te etc. and has
some behavior that it .arks"$.
7ere is .ark"$ is a method of an object 5og. 6lear description abo&t ,ethods will be fo&nd in coming
&nits.
Class:
3imply we can say that the 6lass is a 6ollection of Objects.
#n object-oriented programming a class is a constr&ct that is &sed as a bl&eprint to create instances of itself *
referred to as class instances class objects instance objects or simply objects. A class defines constit&ent
members which enable these class instances to have state and behavior. 5ata field members "member
variables or instance variables$ enable a class object to maintain state. Other kinds of members
especially methods enable a class object(s behavior. 6lass instances are of the type of the associated class.
A class &s&ally represents a no&n s&ch as a person place or "possibly +&ite abstract$ thing or
something nominali-ed. 0or e%ample ).anana) is an instance of the class )0r&it) "a )0r&it) object$. #t is of the
type )0r&it).
Object or Instance:
3imply we can say that Object is an instance of a 6lass.
#n comp&ter science an object is any entity that can be manip&lated by the commands of
a programming lang&age s&ch as a val&e variable f&nction or data str&ct&re. "8ith the later
introd&ction of object-oriented programming the same word )object) refers to a partic&lar instance of
a class$
Three properties characteri-e objects:
1. #dentity : the property of an object that disting&ishes it from other objects
2. 3tate: describes the data stored in the object
9. .ehavior: describes the methods in the object(s interface by which the object can be &sed
Class #ierarc#ies (In#eritance):
#n object-oriented programming "OO'$ in#eritance is a way to re&se code of e%isting objects
establish a s&btype from an e%isting object or both depending &pon programming lang&age s&pport. #n
classical inheritance where objects are defined by classes classes can inherit attrib&tes and behavior
"i.e. previo&sly coded algorithms associated with a class$ from pre-e%isting classes called base
classes or superclasses or parent classes or ancestor classes. The new classes are known as derived
classes or subclasses or child classes. The relationships of classes thro&gh inheritance gives rise to
a hierarchy. #n prototype-based programming objects can be defined directly from other objects
witho&t the need to define any classes in which case this feat&re is called differential inheritance.
$ubclasses and superclasses
A superclass base class or parent class is a class from which other classes have been derived. The classes that
are derived from a s&perclass are known as subclasses or derived classes or child classes.
A s&perclass allows for a generic interface to incl&de speciali-ed f&nctionality thro&gh the &se of virt&al
f&nctions.
The s&perclass mechanism is e%tensively &sed in object-oriented programming beca&se of the re&sability that
can be achieved: common feat&res are encaps&lated in mod&lar objects. 3&bclasses that wish to implement
special behavior can do so via virt&al methods witho&t having to reimplement the s&perclass(s behavior.
A subclass is a class that inherits some properties from its s&perclass.
3&bclasses and s&perclasses are often referred to as derived and base classes respectively
"et#od O%erriding:
"et#od o%erriding in object oriented programming is a lang&age feat&re that allows a s&bclass or
child class to provide a specific implementation of a method that is already provided by one of its
s&perclasses or parent classes. The implementation in the s&bclass overrides "replaces$ the
implementation in the s&perclass by providing a method that has same name same parameters or
signat&re and same ret&rn type as the method in the parent class. The version of a method that is
e%ec&ted will be determined by the object that is &sed to invoke it. #f an object of a parent class is &sed
to invoke the method then the version in the parent class will be e%ec&ted b&t if an object of the
s&bclass is &sed to invoke the method then the version in the child class will be e%ec&ted.3ome
lang&ages allow a programmer to prevent a method from being overridden.
#n 4ava when a s&bclass contains a method that overrides a method of the s&perclass it can also invoke the
s&perclass method by &sing the keyword super
:%ample:
public class Tho&ght ;
public %oid message"$ ;
3ystem.o&t.println")# feel like # am diagonally parked in a parallel &niverse.)$/
<
<
public class Advice e&tends Tho&ght ;
=Override !! "#verride annotation in Java $ is optional but helpful.
public %oid message"$ ;
3ystem.o&t.println")8arning: 5ates in calendar are closer than they appear.)$/
<
<
6lass Tho&ght represents the s&perclass and implements a method call message"$. The s&bclass
called Advice inherits every method that co&ld be in the Tho&ght class. 7owever classAdvice overrides the
method message"$ replacing its f&nctionality from Tho&ght.
'&ceptions:
An e%ception is an event which occ&rs d&ring the e%ec&tion of a program that disr&pts the normal flow
of the program(s instr&ctions.
8hen an error occ&rs within a method the method creates an object and hands it off to the r&ntime
system. The object called an e%ception object contains information abo&t the error incl&ding its type
and the state of the program when the error occ&rred. 6reating an e%ception object and handing it to the
r&ntime system is called thro&ing an e%ception.
After a method throws an e%ception the r&ntime system attempts to find something to handle it. The
set of possible )somethings) to handle the e%ception is the ordered list of methods that had been called
to get to the method where the error occ&rred.
The r&ntime system searches the call stack for a method that contains a block of code that can handle
the e%ception. This block of code is called an e%ception handler. The search begins with the method in
which the error occ&rred and proceeds thro&gh the call stack in the reverse order in which the methods
were called. 8hen an appropriate handler is fo&nd the r&ntime system passes the e%ception to the
handler. An e%ception handler is considered appropriate if the type of the e%ception object thrown
matches the type that can be handled by the handler.
The e%ception handler chosen is said to catch the e%ception. #f the r&ntime system e%ha&stively
searches all the methods on the call stack witho&t finding an appropriate e%ception handler as shown
in the ne%t fig&re the r&ntime system "and conse+&ently the program$ terminates.
Abstraction(In(ormation )iding):
Abstraction in 4ava allows the &ser to hide non-essential details relevant to &ser.
-- #t allows only to show the essential feat&res of the object to the end &ser.
-- #n other sense we can say it deals with the o&tside view of an object "interface$.
*a%a Abstract Class:
Abstraction refers to the ability to make a class abstract in OO'. An abstract class is one that cannot be
instantiated. All other f&nctionality of the class still e%ists and its fields methods and constr&ctors are all
accessed in the same manner. >o& j&st cannot create an instance of the abstract class.
#f a class is abstract and cannot be instantiated the class does not have m&ch &se &nless it is s&bclassed. This is
typically how abstract classes come abo&t d&ring the design phase. A parent class contains the common
f&nctionality of a collection of child classes b&t the parent class itself is too abstract to be &sed on its own.
Important +uestions:
1. 8rite a program that will comp&te the following series:
"a$ 1?1 @ 1?2 @ 1?9 @ ...... @ 1?n
"b$ 1?1 @ 1?2 @ 1?22@ ....... @ 1?2n.
2. "a$ 8hat are conventional styles for class names method names constants and variablesA
"b$ 6an a java r&n on any machineA 8hat is needed to r&n java on a comp&terA
"c$ :%plain the concept of keywords. List some java keywords.
9. "a$ 5escribe the genesis of java. Also write brief overview of java
"b$ List and e%plain the control statements &sed in java. Also describe the synta% of the control
statements with s&itable ill&stration.
B. "a$ java is freeform lang&age. 6omment
"b$ 5escribe in detail the steps involved in implementing a stand-alone program.
"c$ 8hat are command line arg&mentsA 7ow are they &sef&lA
C. "a$ 8hen dealing with very small or very large n&mbers what steps wo&ld yo& take to improve the
acc&racy of the calc&lations
"b$ 8hat are symbolic constantsA 7ow are they &sef&l in developing programsA
"c$ 8rite a program to determine the s&m of the following harmonic series for a given val&e 1 @ 1?2 @
1?9 @ ..... @ 1?n The val&e of n sho&ld be given interactively thro&gh the keyboard.

Vous aimerez peut-être aussi