Friday, January 02, 2009

OOP Principles

In the process-oriented model, programs are written around "what is happening". This approach characterizes a program as a series of linear steps. This can be thought of as code acting on data.

But, object-oriented programming organizes a program around its data, i.e., objects, and a set of well-defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.

Following are the three mechanisms that are required to implement the object-oriented model:

Encapsulation
  • It is the mechanism that binds together code and the data it manipulates
  • It keeps both code and data safe from outside interference and misuse
  • The basis of encapsulation is the class
  • A class defines the structure and behavior (of code and data) that will be shared by a set of objects
  • Each object of a given class contains the structure and behavior defined by the class
  • A class is a logical construct, whereas an object has physical reality
  • The purpose of a class is to encapsulate complexity
  • There are mechanisms for hiding the complexity of the implementation inside the class
  • Each method or variable in a class may be marked private or public
  • The public interface of a class represents everything that external users of the class need to know, or may know
  • The private methods and data can only be accessed by code that is a member of the class
  • Any other code that is not a member of the class cannot access a private method or variable

Ineritance
  • It is the process by which one object acquires the properties of another object
  • It supports the concept of hierarchical classification
  • Without the use of hierarchies, each object would need to define all of its characteristics explicitly
  • However, by use of inheritance, an object need only define those qualities that make it unique within its class
  • It can inherit its general attributes from its parent
  • It is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case
  • 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 lets object-oriented programs grow in complexity linearly rather than geometrically

Polymorphism
  • Polymorphism is a feature that allows one interface to be used for a general class of actions
  • The specific action is determined by the exact nature of the situation
  • It can be expressed by the phrase - one interface, multiple methods
  • 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 as it applies to each situation
  • There are three distinct forms of polymorphism
    • Method overloading
    • Method overriding through inheritance
    • Method overriding through the Java interface
  • Which overloaded method to call is decided at compile time through the reference type passed
  • The other two forms are part of runtime polymorphism
  • With runtime polymorphism, the decision as to which version of a method will be executed is based on the actual type of object whose reference is stored in the reference variable, and not on the type of the reference variable on which the method is invoked
  • The decision as to which version of the method to invoke cannot be made at compile time
  • That decision must be deferred and made at runtime
  • This is sometimes referred to as late binding or dynamic method binding
Another essential element of object-oriented programming is abstraction.
  • A powerful way to manage abstraction is through the use of hierarchical classifications
  • This allows us to layer the semantics of complex systems, breaking them into more manageable pieces
  • The complicated implementation details are hidden from the user
  • The user is given a simple interface to interact with, whereas inside the actual implementation of the interface there may be many complicated details


(Compiled from Java 2: The Complete Reference)

No comments: