Monday, July 5, 2010

Object Oriented Programming Principles

1. Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. Java implements this by the use of classes and objects.
Data hiding /abstraction: is the ability of objects to shield variables from external access. It is a useful consequence of the encapsulation principle. Java implements this by the use of access specifiers, namely public, private, protected, and default.
2. Inheritance is the process by which one object acquires the properties of another object. The concept of inheritance greatly enhances code reuse and makes hierarchical classification possible. Java implements this by the ability of classes to inherit methods and variables from parent classes and interfaces.

3. Polymorphism (from the Greek, meaning “many forms”) is the ability of a single entity to take multiple forms.

Java implements this by:

Method Overloading (Methods with the same name but different signatures)

Method Overriding (Methods with the same name & same signature but one in the parent class and the other in the child class)

Dynamic Method Dispatch (The mechanism by which a call to an overridden method is resolved at run time, rather than compile time.)

Class Fundamentals

Classes define a new data type that can be used as a template for creation of an object.

Objects are instances of a class.

Declaring Objects

First, you must declare a variable of the class type. This variable does not define an object. Instead, it is simply a variable that can refer to an object. Second, you must acquire an actual, physical copy of the object and assign it to that variable. You can do this using the new operator. The new operator dynamically allocates memory for an object and returns a reference to it.

Assigning Object Reference Variables

When you assign one object reference variable to another object reference variable, you are not creating a copy of the object; you are only making a copy of the reference.


Box b1 = new Box();
Box b2 = b1;

Constructors

A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. The return type of a class’ constructor is the class type itself. It is called immediately after the object is created, before the new operator completes.

The “this” Keyword

“this” keyword is used by a method when it needs to refer to the object that invoked it. this can be used inside any method to refer to the current object.

Class Variable, Instance Variable, Local Variable

Class Variable
Static variables declared inside a class are referred to as class variables. They are global to all objects created by the class.

Instance Variable
Instance variables are variables defined in a class body which are created when objects are instantiated. They take different values for each object.

Local Variable
Variables declared and used inside methods are called local variables.