View RSS Feed

My Java Tips

Subtyping

Rate this Entry
by , 11-03-2011 at 12:17 PM (646 Views)
Subtyping is very important in Java. One type is said to be subtype of other if they are related by an extend or implements clause.




For instance:

Integer is a subtype of Number
Double is a subtype of Number
ArrayList is a subtype of List
List is a subtype of Collection
Collection is a subtype of Iterable

Subtyping principle says that a variable of type A can be assigned a value of any subtype of A and methods can be invoked as well.
Review the code below:

Java Code:
ArrayList myArrayList = new ArrayList();

String []strArray= new String[10];

for(int i=0;i<10;i++){
   strArray[i] = "Element" + i;
   myArrayList.add(strArray[i]);
}

System.out.println(myArrayList.get(3));

ArrayList myArrayList1 = myArrayList;
System.out.println(myArrayList.get(4));

Submit "Subtyping" to Facebook Submit "Subtyping" to Digg Submit "Subtyping" to del.icio.us Submit "Subtyping" to StumbleUpon Submit "Subtyping" to Google

Tags: None Add / Edit Tags
Categories
Java SE

Comments