Object Oriented Programming - A Primer
Object Oriented Programming is at the core of Java. Following is a brief description of OOP.
Two Paradigms :
Programs consist of two elements : code & data . Some programs are written around "what is happening" ( process-oriented model ) & others are written around "who is being affected"( OOP ).The first way characterizes a program as a series of linear steps . It can be thought of as code acting on data . A good example for this would be C .But the problem's of this approach appear as the program get larger & more complex .
The second approach , called OOP was conceived to manage increasing complexity .OOPing organizes a program around its data (objects ) .OOPing can be characterized as data controlling access to code .
Abstraction :
Abstraction is an essential element of OOP . Complexity is mananged by abstraction . For example , we never think of a car as a set of thousands of individual parts . We think of it as a well-defined object .This abstarction allows us to drive a car without having to worry 'bout the details of how the engine , transmission & braking work . We r free to utilize the object as a whole .
When applied to a computer system the data from a process-oriented program can be transformed by abstraction into it's component objects . A sequence of process steps can become a collection of messages between these objects. Each of these objects then describe their own behavior & can hence be treated as concrete entities that respond to messages telling them to do something .
All OOPing languages provide mechanisms that help to implement the object-oriented model . They are
- Encapsulation
- Inheritance
- Polymorphism
Encapsulation :
Encapsulation is the mechanism that binds together code & the data it manipulates & keeps both safe from outside interference & misuse .Think 'bout encapsulation as a protective wrapper that prevents the code & data from being arbitrarily accessed by other code defined outside the wrapper . Access to the code & data inside the wrapper is tightly controlled thru' a well-defined interface . A real world analogy would be for instance that U can't affect the transmission by using the wipers .As a user to affect the transmission U have only one method , the gear-shift lever . The gear-shift lever is a well-defined interface to the transmission .What occurs inside the transmission does not affect objects outside the transmission .
Inheritance :
Inheritance is the process by which an object acquires the properties of another object . This is important because it supports the concept of hierarchical classification . Most knowledge is made manageable by hierarchical classification . For example an Alsatian is a part of classification dog , which is in turn a part of mammal class , which is under the larger class mammals . Without the use of hierarchies , would need to define all of it's characteristics explicitly . However , by the use of inheritance , an object need only define those qualities that make it unique within it's class . It can inherit it's general attributes from it's parent . This mechanism makes it possible for one object to be a specific instance of a more general case.
Java Ex : Overloaded constructors.
Polymorphism :
Polymorphism is a feature that allows one interface to be used for ageneral class of actions . Consider a stack . U might have a program that requires three types of stacks .One stack is used for integer values , one for floating-point values & one for characters . The algorithm that implements each stack is the same , even though the data being stored is different . In a non-OOP language , U would be required to create three different sets of stack routines with each set using a different names . In Java because of polymorphism U can specify a general set of stack routines that all share the same names . The concept is expressed by the phrase "one interface , multiple methods".
Java Ex : Overloaded constructors.