Thursday, May 19, 2011

Some real world java examples

Just thought about some real world examples.

1. Polymorphism:

In simple terms polymorphism means one interface - multiple implementations. Users can choose the
implementation based on their own interests. For example Vehicle could be the interface and travel
is the method. There could be n number of implementations for Vehicle interface. i.e Van, Car, Bus etc.
Uses can choose their own Vehicle implementation based on their need i.e capacity, cost etc. Polymorphism can
be used for grouping.

2. Inheritance:

In object oriented programming, users can map real world entities into java objects. Through inheritance
parent object's properties are passed to children object properties. This reduces the amount of code
to be written and also increases the maintainability of the code. For example employee is the class that could
have details as name, id, start date of employment, address etc. Also Lead is also an employee but he has
more details as module he owns, review permission etc. Manager is also an employee who has additional responsibilites
like performance review etc., along with the default employee details.

3. Encapsulation:

Encapsulation means providing right access details for the right entities. For example in a hotel,customers are not allowed
inside cooking room. Servers are not allowed inside owner's cabin.

4. Queue:

Queue means first in first out. Railway booking counters.

4. Stack:

Stack means last in first out. Assume a movie theatre has 1000 seats which has only one gate in which people are allowed based on the seat number. Once the
show is over person who sat in 1000th seat, can come out as a first person.

5. Deque:

Deque means addition/removal is supported on both ends. Same theatre example could be applied here where it has two gates. One front gate and one back gate, visitors are allowed in both the gates.

Monday, May 16, 2011

ClassCastException while trying to cast to the same object

Recently I wanted to know how application servers are reloading jsp file.  So thought of finding it myself. Tried writing a ClassLoader that extends java.lang.ClassLoader which always loads class files from the disc and not from the memory.

public class PrintObj
{
         public void print()
         {
                  System.out.println(" one ");
          }
}


public class MyClassLoader extends ClassLoader
{
           public Class findClass()
           {
                      //load it from the disc and return the Class object 
            }
}


public class ClassLoaderTest
{
       public static void main(String a[])
       {
                 ClassLoader sysLoader = DynamicClassLoader.class.getClassLoader();
                 //it loads it from the memory
                  PrintObj l1 = (PrintObj)sysLoader.loadClass("PrintObj").newInstance();
                  l1.print();
                 

                 MyClassLoader loader = new MyClassLoader();   
                 PrintObj l2 = (PrintObj)loader.loadClass("PrintObj").newInstance();
                 l2.print();
                 

        }
}

When I am trying to execute the below code, it throws
Exception in thread "main" java.lang.ClassCastException: PrintObj cannot be cast to PrintObj. Bolded line shows ClassCastException. Interested to know that it collides with the existing PrintObj loaded by the system class loader. Then had to create an interface call Print and then made PrintObj implements that Print interface.

public interface Print
{
       public void print();
}


public class PrintObj implements Print
{
       System.out.println(" one ");
}


After doing the above change below code runs with out any problem.

ClassLoader sysLoader = DynamicClassLoader.class.getClassLoader();
//it loads it from the memory
Print l1 = (PrintObj)sysLoader.loadClass("PrintObj").newInstance();
l1.print();

MyClassLoader loader = new MyClassLoader();   
Print l2 = (PrintObj)loader.loadClass("PrintObj").newInstance();
l2.print();


Now other issue when I tried to load the class second time it throws ,

MyClassLoader loader = new MyClassLoader();   
 Print l2 = (PrintObj)loader.loadClass("PrintObj").newInstance();
  l2.print();

 Print l3 = loader.loadClass("PrintObj").newInstance();

Exception in thread "main" java.lang.LinkageError: loader (instance of  MyClassLoader): attempted  duplicate class definition for name: "PrintObj". Then finally realized that a new ClassLoader() instance has to be created to reload a class every time from the disc.