Sunday, 17 March 2013

OOPs Basics

Objects

Objects are used to map our programming elements to the real world. For example, In real world we find different objects, like a car which has its own state(Property) and behaviour(Function). Similarly, in programming we try to map these state and behaviour to programming entities.

Example

Lets assume we have to calculate the area of a circle object. The circle will have its state radius. And it will have a behaviour area. If you map it to a car you will find it even better. Lets assume we have a car object that is going to be controlled automatically. It will have states like speed, mileage and so on. It will have behaviours like acceleration, deceleration, stop and so on.

Classes

To put it simple Class is a template for objects. We have same sort of objects and hence we can create a class from which multiple objects can be created. Again from the car example, we know that all cars have same set of components at least for same brand of a car. This is how classes came into existence for re using the same kind of object behaviour.

Inheritance

Lets assume that we have a basic car. Now we want to add AC to that car, So the new AC car is actually the same car but have AC in additional. In such case we will create a new class that inherits from the car class thereby reusing code.

Interface

Interfaces are use to expose the required information of the class without exposing the actual implementation of the class. It will have just the instance variables and public methods associated to the class.

Packages

Packages are namespaces that organizes the set of classes and interfaces. It is used to ensure easy access to thousands of classes. It done by grouping the classes into subgroups. For example util is one of the sub group. Its package consists of classes like linked list, vector and so on. It is accessed by java.util.Vector and so on.