Tuesday, December 14, 2010

Lesson3

BASIC CONCEPTS Of OOP (Object Oriented Programming)

Objects - You can say that Objects are representations of physical elements. For eg. Bicycle. A complete status or Objects will have properties and states. This is similar to objects in pl/sql; in pl/sql we define the objects in terms of the attributes or columns.

Bicycle states (current speed, current pedal cadence, and current gear).

Methods - Method is the process by which the states are changed. This is analogous to a procedure in pl/sql.

Classes - You can think of class as a collection of methods and object properties.
For eg. In the below Bicycle class pedal cadence, speed and gear are the properties/states.
changeCadence, changeGear, etc are methods to bring about changes in the
states or properties.
class Bicycle {         int cadence = 0;        int speed = 0;        int gear = 1;         void changeCadence(int newValue) {             cadence = newValue;        }         void changeGear(int newValue) {             gear = newValue;        }         void speedUp(int increment) {             speed = speed + increment;           }         void applyBrakes(int decrement) {             speed = speed - decrement;        }         void printStates() {             System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);        } }

Inheritance - In reality there are many kinds of bikes like mountain bikes, dirt bikes, etc ... Each of the above can be classified as a class in its own due to some unique features while some features would be same in all the bikes. Say the properties of the class Bicycle would apply to both MountainBikes and DirtBikes hence you would like to extend the properties and methods of class Bicycle to Classed MountainBikes and DirtBikes - this lending of a properties and methods of a class to another is called inheritance.

Syntax ...
class MountainBike extends Bicycle {       // new fields and methods defining a mountain bike would go here 
}

Interfaces - collection of methods without bodies. In analogy with pl/sql you can say its a package specification.
interface Bicycle {         void changeCadence(int newValue);   // wheel revolutions per minute         void changeGear(int newValue);         void speedUp(int increment);         void applyBrakes(int decrement); } 
To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration:
class ACMEBicycle implements Bicycle {     // remainder of this class implemented as before  } 
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

Packages - A package is a namespace that organizes a set of related classes and interfaces.


  • Real-world objects contain state and behavior.

  • A software object's state is stored in fields.

  • A software object's behavior is exposed through methods.

  • Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data encapsulation.

  • A blueprint for a software object is called a class.

  • Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword.

  • A collection of methods with no implementation is called an interface.

  • A namespace that organizes classes and interfaces by functionality is called a package.

  • The term API stands for Application Programming Interface.
  • No comments: