Wednesday, 12 March 2014

Generics And Inheritance

If types are compatible then, we can assign one type to the other.

class CompatibleEx{
 public static void main(String args[]){
  Number num1 = new Integer(10);
  System.out.println(num1);
 }
}

Output
10
Since Integer is the sub class for Number, so Any Integer object is also a Number object. So Assigning an Integer object to a Number reference is valid.

Generics is some what tricky as compared to normal inheritance. Will see it by detailed Example.


As you see in the above figure, DataStructure<T> is a Generic Interface, LinearStructure<T> is another interface which extends DataStructure interface. Stack<T> is a generic class which implements the LinearStructure interface.

interface DataStructure<T>{
 void display();
 boolean search(T o);
}

interface LinearStructure<T> extends DataStructure<T>{
 boolean pushEle(T o);
 T pop();
}
  
class Stack<T> implements LinearStructure<T>{
 int size;
 int top = -1;
 Object stk[];

 Stack(int size){
  this.size = size;
  stk = new Object[size];
 }

 public boolean pushEle(T obj){
  if(size <= 0 || top == (size-1)){
   System.out.println("Stack is full or stack size is 0");
   return false;
  }
  else{
   top++;
   stk[top] = obj;
   return true;
  }
 }

 public T pop(){
  if(top == -1){
   System.out.println("Stack is Empty");
   return null;
  }
  else{
   T val = (T)stk[top];
   stk[top] = null;
   top--;
   return val;
  }
 }

 public void display(){
  System.out.println("\nElements in the stack are");
  for(int i=0; i <= top; i++){
   System.out.print(stk[i] +" ");
  }
 }

 public boolean search(Object obj){
  for(int i=0; i < top; i++){
   if(obj.equals(stk[i]))
   return true;
  }
   return false;
 }

 int getTop(){
  return top;
 }
}
   
class StackTest{
 public static void main(String args[]){
  Stack<Integer> s1 = new Stack<Integer> (10);
  Stack<Integer> s2 = s1;
  s2.pushEle(10);
  s2.pushEle(20);
  s2.display();
 }
}
  
Output
Elements in the stack are
10 20
   

As you observe the statements

   Stack<Integer> s1 = new Stack<Integer> (10);
   Stack<Integer> s2 = s1;

Both s1 and s2 are of type Stack<Integer> so s2=s1 is legal. See the below code.
   
class StackTest{
 public static void main(String args[]){
  Stack<Integer> s1 = new Stack<Integer> (10);
  Stack<Number> s2 = s1;

  s2.pushEle(10);
  s2.pushEle(20);
  s2.display();
 }
}
  
When you tries to compile the above program compiler throws the below error.

StackTest.java:4: error: incompatible types
Stack<Number> s2 = s1;
^
required: Stack<Number>
found: Stack<Integer>
1 error
Even though Integer is a sub class of Number, Stack<Integer> is not a sub class Of Stack<Number> above error thrown.



Stack<Integer> is a sub class of LinearStructure<Integer> and LinearStructure<Integer> is a sub class of DataStructure<Integer>. But Stack<Integer> is not a sub class of Stack<Number>.



class StackTest{
 public static void main(String args[]){
  LinearStructure<Integer> s1 = new Stack<Integer> (10);
  DataStructure<Integer> s2 = s1;

  s1.pushEle(10);
  s1.pushEle(20);
  s2.display();
 }
}
   
Output
Elements in the stack are
10 20
   
 
Similarly, Stack<Number> is a sub class of LinearStructure<Number> and LinearStructure<Number> is a sub class of DataStructure<Number>. But Stack<Integer> is not a sub class of Stack<Number>.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment