Pages

Monday 30 September 2013

Basic Principles of Java

            
              Java is an object-oriented programming language. It is also a development environment, an application environment and a deployment environment.
              The basic principles of Java are: 

·        Encapsulation
·        Inheritance
·        Polymorphism
1.     Encapsulation: 

         The mechanism which wraps data and code together into a single unit so that it can be protected from outside interference. The access to the data and code that are encapsulated are controlled through the well-defined interfaces.
          The access specifiers present in Java are public, private and protected. They are used to specify access to data and code in program.
           The basis of encapsulation in Java is class.
           For example, encapsulation can be thought as the protective bricks and cement barrier around a house. A person can access the house only through the entrance or gate provided in the barrier. All the unknown persons are not allowed to access the house.
           More relevant example may be the automobile, in which all the components of it are wrapped together to form less complex single unit like car, bus, bike, etc.

2.     Inheritance:
                
     It is the process by which one object inherits the properties of other object. In java almost everything is considered as objects that are related to each other with specific relations.

How to know which object inherits the other?

     Inheritance works on IS-A principle

     Extends keyword is used to implement inheritance in Java.

Example showing how to use IS-A relation:

·        Cat IS-A Omnivorous, principle holds good that means cat inherits omnivorous or cat extends omnivorous.
·        Dog IS-A Animal, principle holds good that means dog inherits animals or dog extends animals.
·        Now check for this, page IS-A book, no not here!! . Principle does not hold good for this relation. So page can’t inherit book. It must be book HAS-A page. 

When it comes to programming we may many classes in our application and these classes may be related with each other in hierarchical manner.
 Considering example:
 A and B be two classes in our program and A extends B or A inherits B or A IS-A B.
Here A is called the subclass of B and B is called the superclass of A. 



3.     Polymorphism:

                   It is the feature of java which allows one interface to be used for multiple methods. It is often referred with the phrase “one interface, multiple methods”.
                   For example, there might me requirement in a program that three types of stacks are required. One stack to store integer, one for float and one for double. The procedure used to implement all the three stacks is one and the same i.e. using push and pop methods. If you are using structural programming language then you have to write three different stack routines. In case of Java you can write a general set of stack routines that all can share. This is possible due to power feature of Java, the polymorphism.  
                  Let me show you some program fragment which will be used in Java every time
          
                               Cat cat1=new Cat();  here Cat is a class, and cat1 is the reference to the object Cat. 

                  The above instruction in Java creates a new cat object, this cat object can be used with the help of reference, cat1. If you are a beginner in java you may feel it complicated, but trust me when you go deep into java and are much interested to learn you feel it very easy and fun programming with java.  

                 Ok! Lets’ come back to example, the object cat which is created is of the same type as the reference (cat1) that is cat type.
                Let me create one more object of the type animal,
                        Animal animal=new Animal();here Animal is a class, and ani1 is the reference to the object Animal.

                  We know Cat inherits Animal class. 

                        Animal cat1=new Cat();  this is possible with polymorphism.

                  With the polymorphism, the reference to the object can be of different type.