Showing posts with label OOD. Show all posts
Showing posts with label OOD. Show all posts

Friday, March 18, 2011

What is a static member in Java? - OOD 101

The keyword 'static' preceding a variable(field) or a method makes its static.

static means, one per class and not one for each instance of the class (or object). This means that you can use them without creating an instance of a class.

Static variables (class variables) VS Instance variables:
When a number of objects are created from a class, each object gets gets its own copy of the instance variables.
Static variables are however associated with the class, rather than with any object of the class. Every instance of the class shares the same class variable, which is in one fixed location in memory. The class variable can be accessed without creating an instance of the class and any object can change the value of a class variable.

Static  methods (class methods) VS Instance methods:
Instance methods are associated with an object and use the instance variables of that object.
Static methods use no instance variables of any object of the class they are defined in. A Static method can only access static variables and cannot access the instance variables. Static methods typically take all they data from parameters and compute something from those parameters, with no reference to variables. This is typical of methods which do some kind of generic calculation. A good example of this are the many utility methods in the predefined Math class.

What is the difference between an Interface and an Abstract Class?

Interface:
An interface is a collection of method prototypes, i.e. method signatures only, no body.
The class that implements the interface must provide an implementation to the methods in the interface.
So a interface is used to enforce a contract.

Abstract class:
An abstract class is one that has one or more abstract methods. An abstract method is one that has only a signature and no implementation. An abstract class can have implemented methods. An abstract class cannot be instantiated, but can be sub-classed/extended. The class that extends the abstract class must provide a definition for the abstract methods.

So, an abstract class is used to provide an initial implementation, that needs to be extended by a sub class. It is used to propagate 'a kind of' relationship.

What are the three pillars of OOD?

(1) Encapsulation:
The ability to combine attributes (data) and functionality (methods) into a single entity (class).

(2) Inheritance:
The ability to extend (subclass) a base class, i.e. the capability of a class to use the properties and methods of another class while adding its own functionality.

(3) Polymorphism:
From the biological term 'many forms'

The capability of an action or method to do different things based on the object that it is acting upon.

Example:

(i) Overloaded methods - methods with the same name but different signatures i.e. either a different number or type of parameters or a different return type.

(ii) Overridden methods - methods that exist in a base class and then redefined within an inherited or subclass. They have the same signature and the subclass definition is used i.e. the subclass definition overrides the definition in the base class.