CHAPTER-2
*) Two Paradigms:- All computer programs consists of 2 elements
!) Code 2) Data
>) A program can coneceptually organized around its code or around its data.
>) Programs written around "what is happening" is called process-oriented
model.
*) The process-oriented model can be thought of as code acting on data.
*) This approach characterizes a program as a series of linear steps
(that is,code).
*) Procedural languages such as "C" employ this model to considerable
success.
Disadvantage:- Problems with this approach appear as programs grow
larger and more complex.
>) Object Oriented Programming:- To manage increasing complexity this
approach organizes a program around its data(that is,objects)
and a set of well-defined interfaces to that data.
*) OOP program can be characterized as data controlling access to code,by
switching the controlling entity to data,you can achieve several
organizational benefits.
>) Abstraction :- This is an important element of OOP,complexity is
managed through abstraction.
Example:- We think Car is a well-defined object with its own unique behavior.
not as a thousands of individual parts.Instead ,they are free to utilize
the object as a whole.
*) Hierarchical classifications is a powerful way to manage abstraction.It layer
the semantics(the study of language meaning) of complex systems,breaking
them into more manageable pieces.
*) Data from a process-oriented program can be transformed into its component
objects by abstraction.
*) Each of these objects describes its own unique behavior.A sequence of
process steps can become a collection of messages between these objects.
> The Three OOP Principles:- All OOP languages provide a
mechanisms that help us implement the object-oriented model.
1) Encapsulation 2) Inheritance 3) Polymorphism
1) Encapsulation:- It binds together Code & Data it manipulates(handles),
and keep both safe from outside interference and misuse.
@) It acts like a protective wrapper that prevents the code and data from being
arbitrarily(Based on random choice or codes)accessed by other code defined
outside the wrapper.Access to the code and data inside the wrapper is
tightly controlled through a well-defined
interface(A point where two systems interact).
*) Example :-In the automatic transmission on an automobile.It encapsulate
hundreds of bits of information about your engine,such as how
much you are accelerating,the pitch of the surface you are on
and the position of the shift lever.
*) We as the user have only one method of affecting this complex
encapsulation;by moving the gear-shift lever.We can't affect
the transmission by using the turn signal or windshield wipers.
Thus,the gear-shift lever is a well-defined interface to the
transmission.
*) What occurs inside the transmission does not affect objects
outside the transmission.
Example:- Shifting gears does not turn on the horn,because
an automatic transmission is encapsulated.
@) The power of encapsulated code is that everyone knows how to access it and
thus can use it regardless of the implementation details and without fear of
unexpected side effects.This same idea can be applied to programming.
@) In java,basis of encapsulation is the CLASS
Class:- A class defines the structure and behavior(data and code) that will
shared by the set of Objects.
>>Each object of a given class contains the structure and behavior
defined by the class.Objects sometimes referred to as
instances(a particular case) of the class
>> A Class is a logical construct(Form or an idea);an object has
physical reality.
@) The code and data that constitute the class.Collectively,these elements
are called members of the class
>>Member variables or instance variables:- The data defined by the class.
>>Member Methods or Methods :- The code that operates on that data.
(*) Note:- Method in Java is similar to that of function in C/C++.
@) The methods define the behavior and interface of a class that operate on
its instance data and how the member variables can be used.
@) Each method or variable in a class may be market private or public.
@) The public interface of a class represents everything that external users
of the class need to know.
@) The private methods and data can only be accessed by code that is a
member of the class.
@) Code which is not a member of a class cannot access a private method
or variable.
> Inheritance :- This is the process by which one object acquires
the properties of another object.
@) This is important because it supports the concept and most knowledge
is made manageable by hierarchical(that is,top-down) classifications.
@) Without hierarchies,each object would need to define all of its charac-
teristics explicitly.
@) However,by use of inheritance,an object need only define those
qualities that make it unique within its class.It can inherit it's general
attributes from its parent.
@) Thus,it is the inheritance mechanism that makes it possible for one
object to be a specific instance of a more general case.
> Example:- If order to describe animals in an abstract way,you would
say they have some attributes,such as *) size
*) intelligence,and
*) skeletal system
@) Animals also have certain behavioral aspects;they *) eat
*) breathe,and
*) sleep
@) This description of attributes and behavior is the class definitions for
animals.
@) If you wanted to describe a more specific class of animals,such as
mammals,they would have more specific attributes;such as type of
*) teeth and *) mammary glands
@) This is known as a subclass of animals,where animals are referred to
as mammals superclass.
@) Since mammals are simply more precisely specified animals,they inherit
all of the attributed from animals.
@) A deeply inherited subclass inherits all of the attributes from each of its
ancestors in the class hierarchy.
*) Inheritance interacts with encapsulation as well.If a given class encapsulates
some attributes,then any subclass will have the same attributes plus any that
it adds as part of its specialization.
*) This is a key concept that lets object-oriented programs grow in complexity
linearly rather than geometrically.
*) A new subclass inherits all of the attributes from all of its ancestors and does
not have unpredictable interactions with the majority of the rest of the code
in the system.
> Polymorphism:- A feature that allows one interface to be used for a
a general class of actions is called polymorphism(Many forms)
@) The specific action is determined by the exact nature of the situation.
@) Consider three type of stacks(which is a last-in,first-out list)
*) integral values
*) floating-point and
*) one for characters.
The algorithm that implements each stack is the same,even though data
being stored differs.
@) In a non-object-oriented language,you would be required to create three
different sets of stack routines,with each set using different names.
@) However,because of polymorphism,in Java you can specify a general set
of stack routines that all share the same names
@) Polymorphism is often expressed by the phrase " one interface,multiple
methods."This means that it is possible to design a generic interface to
a group of related activities.
@) This helps reduce complexity by allowing the same interface to be used to
specify a general class of action.
@) It is the compiler's job to select the specific action(i.e,method) as it applies
to each situation.We as a programmer do not need to make this selection
manually.You need only remember and utilize the general interface.
*) Example:- A dog sense of smell is polymorphic.
>> If the dog smells a cat,it will bark and run after it.
>> If the dog smells its food,it will salivate and run to its bowl.
The same sense of smell is at work in both situations.The difference is what
being smelled,i.e, the type of data being operated upon by the dog's nose!This
same general concept can be implemented in Java as it applies to methods
within a Java program.
*) Polymorphism,Encapsulation,and Inheritance work
together:-
@) When this 3 mechanism properly combine to produce a programming
environment that supports the development of far more robust and
scalable programs than does the process-oriented model.
@) A well-designed hierarchy of classes is the basis for reusing the code in
which you have invested time and effort developing and testing.
@) Encapsulation allows you to migrate your implementations over time
without breaking the code that depends on the public interface of your
classes.
@) Polymorphism allows you to create clean,sensible ,readable,and resilient
code.
*) Inheritance Example :- All drivers rely on inheritance to drive
different types of vehicles,more or less find and operate the steering wheel,
the brakes,and the accelerator.
>>After a bit of gear grinding most people can even manage the difference
between a stick shift and an automatic,because they fundamentally
understand their common superclass,the transmission.
*) Encapsulated Example:- People interface with encapsulated
features on cars all the time.
>> The brake and gas pedals hide an incredible array of complexity with an
interface so simple you can operate them with your feet.
>> The implementation of the engine,the style of brakes,and the size of the
tires have no effect on how you interface with the class definition of pedals.
*) Polymorphism Example:- It is clearly reflected in the ability of
car manufacturers to offer a wide array of options on basically the same
vehicle.
*) For Example:- We can get an anti-lock breaking system or traditional brakes
power or rack-and-pinion steering,and 4-,6-,or 8-cylinder engines.
>> Either way,you will still press the break pedal to stop,turn the
steering wheel to change direction,and press the accelerator
when you want to move.The same interface can be used to control
a number of different implementations.
@) It is through this 3 mechanism that the individual parts are transformed into
the object known as a car.
@) The same is also true of computer programs.By the application of object-
oriented principles,the various parts of a complex program can be bought
together to form a cohesive,robust,maintainable whole.
@) Every Java program is object-oriented
*) Two Paradigms:- All computer programs consists of 2 elements
!) Code 2) Data
>) A program can coneceptually organized around its code or around its data.
>) Programs written around "what is happening" is called process-oriented
model.
*) The process-oriented model can be thought of as code acting on data.
*) This approach characterizes a program as a series of linear steps
(that is,code).
*) Procedural languages such as "C" employ this model to considerable
success.
Disadvantage:- Problems with this approach appear as programs grow
larger and more complex.
>) Object Oriented Programming:- To manage increasing complexity this
approach organizes a program around its data(that is,objects)
and a set of well-defined interfaces to that data.
*) OOP program can be characterized as data controlling access to code,by
switching the controlling entity to data,you can achieve several
organizational benefits.
>) Abstraction :- This is an important element of OOP,complexity is
managed through abstraction.
Example:- We think Car is a well-defined object with its own unique behavior.
not as a thousands of individual parts.Instead ,they are free to utilize
the object as a whole.
*) Hierarchical classifications is a powerful way to manage abstraction.It layer
the semantics(the study of language meaning) of complex systems,breaking
them into more manageable pieces.
*) Data from a process-oriented program can be transformed into its component
objects by abstraction.
*) Each of these objects describes its own unique behavior.A sequence of
process steps can become a collection of messages between these objects.
> The Three OOP Principles:- All OOP languages provide a
mechanisms that help us implement the object-oriented model.
1) Encapsulation 2) Inheritance 3) Polymorphism
1) Encapsulation:- It binds together Code & Data it manipulates(handles),
and keep both safe from outside interference and misuse.
@) It acts like a protective wrapper that prevents the code and data from being
arbitrarily(Based on random choice or codes)accessed by other code defined
outside the wrapper.Access to the code and data inside the wrapper is
tightly controlled through a well-defined
interface(A point where two systems interact).
*) Example :-In the automatic transmission on an automobile.It encapsulate
hundreds of bits of information about your engine,such as how
much you are accelerating,the pitch of the surface you are on
and the position of the shift lever.
*) We as the user have only one method of affecting this complex
encapsulation;by moving the gear-shift lever.We can't affect
the transmission by using the turn signal or windshield wipers.
Thus,the gear-shift lever is a well-defined interface to the
transmission.
*) What occurs inside the transmission does not affect objects
outside the transmission.
Example:- Shifting gears does not turn on the horn,because
an automatic transmission is encapsulated.
@) The power of encapsulated code is that everyone knows how to access it and
thus can use it regardless of the implementation details and without fear of
unexpected side effects.This same idea can be applied to programming.
@) In java,basis of encapsulation is the CLASS
Class:- A class defines the structure and behavior(data and code) that will
shared by the set of Objects.
>>Each object of a given class contains the structure and behavior
defined by the class.Objects sometimes referred to as
instances(a particular case) of the class
>> A Class is a logical construct(Form or an idea);an object has
physical reality.
@) The code and data that constitute the class.Collectively,these elements
are called members of the class
>>Member variables or instance variables:- The data defined by the class.
>>Member Methods or Methods :- The code that operates on that data.
(*) Note:- Method in Java is similar to that of function in C/C++.
@) The methods define the behavior and interface of a class that operate on
its instance data and how the member variables can be used.
@) Each method or variable in a class may be market private or public.
@) The public interface of a class represents everything that external users
of the class need to know.
@) The private methods and data can only be accessed by code that is a
member of the class.
@) Code which is not a member of a class cannot access a private method
or variable.
> Inheritance :- This is the process by which one object acquires
the properties of another object.
@) This is important because it supports the concept and most knowledge
is made manageable by hierarchical(that is,top-down) classifications.
@) Without hierarchies,each object would need to define all of its charac-
teristics explicitly.
@) However,by use of inheritance,an object need only define those
qualities that make it unique within its class.It can inherit it's general
attributes from its parent.
@) Thus,it is the inheritance mechanism that makes it possible for one
object to be a specific instance of a more general case.
> Example:- If order to describe animals in an abstract way,you would
say they have some attributes,such as *) size
*) intelligence,and
*) skeletal system
@) Animals also have certain behavioral aspects;they *) eat
*) breathe,and
*) sleep
@) This description of attributes and behavior is the class definitions for
animals.
@) If you wanted to describe a more specific class of animals,such as
mammals,they would have more specific attributes;such as type of
*) teeth and *) mammary glands
@) This is known as a subclass of animals,where animals are referred to
as mammals superclass.
@) Since mammals are simply more precisely specified animals,they inherit
all of the attributed from animals.
@) A deeply inherited subclass inherits all of the attributes from each of its
ancestors in the class hierarchy.
*) Inheritance interacts with encapsulation as well.If a given class encapsulates
some attributes,then any subclass will have the same attributes plus any that
it adds as part of its specialization.
*) This is a key concept that lets object-oriented programs grow in complexity
linearly rather than geometrically.
*) A new subclass inherits all of the attributes from all of its ancestors and does
not have unpredictable interactions with the majority of the rest of the code
in the system.
> Polymorphism:- A feature that allows one interface to be used for a
a general class of actions is called polymorphism(Many forms)
@) The specific action is determined by the exact nature of the situation.
@) Consider three type of stacks(which is a last-in,first-out list)
*) integral values
*) floating-point and
*) one for characters.
The algorithm that implements each stack is the same,even though data
being stored differs.
@) In a non-object-oriented language,you would be required to create three
different sets of stack routines,with each set using different names.
@) However,because of polymorphism,in Java you can specify a general set
of stack routines that all share the same names
@) Polymorphism is often expressed by the phrase " one interface,multiple
methods."This means that it is possible to design a generic interface to
a group of related activities.
@) This helps reduce complexity by allowing the same interface to be used to
specify a general class of action.
@) It is the compiler's job to select the specific action(i.e,method) as it applies
to each situation.We as a programmer do not need to make this selection
manually.You need only remember and utilize the general interface.
*) Example:- A dog sense of smell is polymorphic.
>> If the dog smells a cat,it will bark and run after it.
>> If the dog smells its food,it will salivate and run to its bowl.
The same sense of smell is at work in both situations.The difference is what
being smelled,i.e, the type of data being operated upon by the dog's nose!This
same general concept can be implemented in Java as it applies to methods
within a Java program.
*) Polymorphism,Encapsulation,and Inheritance work
together:-
@) When this 3 mechanism properly combine to produce a programming
environment that supports the development of far more robust and
scalable programs than does the process-oriented model.
@) A well-designed hierarchy of classes is the basis for reusing the code in
which you have invested time and effort developing and testing.
@) Encapsulation allows you to migrate your implementations over time
without breaking the code that depends on the public interface of your
classes.
@) Polymorphism allows you to create clean,sensible ,readable,and resilient
code.
*) Inheritance Example :- All drivers rely on inheritance to drive
different types of vehicles,more or less find and operate the steering wheel,
the brakes,and the accelerator.
>>After a bit of gear grinding most people can even manage the difference
between a stick shift and an automatic,because they fundamentally
understand their common superclass,the transmission.
*) Encapsulated Example:- People interface with encapsulated
features on cars all the time.
>> The brake and gas pedals hide an incredible array of complexity with an
interface so simple you can operate them with your feet.
>> The implementation of the engine,the style of brakes,and the size of the
tires have no effect on how you interface with the class definition of pedals.
*) Polymorphism Example:- It is clearly reflected in the ability of
car manufacturers to offer a wide array of options on basically the same
vehicle.
*) For Example:- We can get an anti-lock breaking system or traditional brakes
power or rack-and-pinion steering,and 4-,6-,or 8-cylinder engines.
>> Either way,you will still press the break pedal to stop,turn the
steering wheel to change direction,and press the accelerator
when you want to move.The same interface can be used to control
a number of different implementations.
@) It is through this 3 mechanism that the individual parts are transformed into
the object known as a car.
@) The same is also true of computer programs.By the application of object-
oriented principles,the various parts of a complex program can be bought
together to form a cohesive,robust,maintainable whole.
@) Every Java program is object-oriented