Java Generic Objects Behavior in Non Generic Area | Generic Class | Generic Methods.
Hello Java Lovers in this article we are going to see Java Generic objects behavior in Non-Generic area, and Java Non-Generic object behavior in Java Generic area.
So lets begin...
First See This Example Of ArrayList With Non-Generic Concept And Try To Understand Problem With This Example.
Example of Java Non-Generic ArrayList.
import java.util.*;
public class MyClass{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add("Matin");
al.add("Sharavan");
al.add(10);
System.out.println(al);
String s=(String)al.get(2);
System.out.println(s);
}
}
output:
[Matin,Shravan,10]
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at com.JavaTpoint.TestMain.main(MyClass.java:line no)
-So in the above example we added two String and one Integer elements to ArrayList,after that we are printing the elements present inside ArrayList Ok.
-After that we used get() method to get 2nd index element,but here we don't know the type of elements,we are holding 2nd index element in String then we will get compile time error but after Type Casting error will be resolved but at run time we will get Class Cast Exception.
Now Example of Java Generic ArrayList.
[Matin,Shravan,Sagar]
Sagar
-Now here we can add only String type of elements,so we can give the guarantee the Type of Elements present inside ArrayList.
-So Here Type Casting is not required because we know the type of elements.
-Achieved Type Safety.
-Compile time checking,See line number 8,at line number 8 i am trying to add Integer element then immediately we are getting compile time error saying,The method add(int, String) in the type ArrayList<String> is not applicable for the arguments (int).
-Compile time error is better than Run Time.
Now Java Generic Object Behavior in Non-Generic Method/Area.
Consider the example given below,read it carefully.
Real Life Example:
-One day in morning i went to my friends home (he is pure vegetarian) to discuss something,while discussing i asked him do you eat Non-Veg,then in front of his parent he said no no how i can eat Non-Veg as i am vegetarian.
-After sometime me and my friend came back to my home,i am Non-Vegetarian,I took a plate and served chicken and i start eating my friend is also there with me,he asked me hey what you are eating then i told this is chicken,suddenly in middle my friend took one piece of chicken and start eating,then i asked him oye you are vegetarian then how you can eat chicken then he told usually i eats Non-Veg's then i asked him but in the morning when i asked do you eat Non-Veg's then you said no no i am vegetarian then he said no no i also eat Non-Veg's too but when i am outside from my home.
-Again in the evening i went to my same friend's home to play chess then at that time intentionally i asked him do you eat Non-Veg's,again he told no no i am pure vegetarian how i can eat Non-Veg's in front of his parent's ;).
-So here my friend is vegetarian at his home in front of his parent's.
But outside he can eat Non-Veg's too,means he is changing his behavior accordingly.
Now see below practical example...
import java.util.*;
public class MyClass{
public static void myFriendatMyHome(ArrayList myfriends) {
myfriends.add(1);
myfriends.add(4.1f);
myfriends.add(66.45);
myfriends.add("Chicken");
}
public static void main(String[] args){
ArrayList <String> myFriendatHishome=new ArrayList <String>();
myFriendatHishome.add("Patato");
myFriendatHishome.add("Tomato");
myFriendatHishome.add("Onion");
//myFriendatHishome.add(1);//Compile time error
//Consider 1 as chicken
System.out.println("My Friend can eat/add only "+myFriendatHishome+" at his home in front of his parent's");
myFriendatMyHome(myFriendatHishome);
//passing Generic ArrayList Object to Non-Generic Area/Method
System.out.println("But At My Home he can eat/add "+myFriendatHishome+" also");
}
}
Output:
My Friend can eat/add only [Patato, Tomato, Onion] at his home in front of his parent's
But At My Home he can eat/add [Patato, Tomato, Onion, 1, 4.1, 66.45, Chicken] also.
-So from the above example myFriendatHishome he can eat/add only vegetables but after coming at myFriendatMyHome(myFriendatHishome); he can eat/add Chicken/Mutton etc.
-Means behavior of My Friend changing accordingly.
-Communication of Generic Code with Non-Generic Code.
-When we passed Generic ArrayLsit object myFriendatHishome to Non-Generic Area/Method myFriendatMyHome(myFriendatHishome) then My Friend i.e. myFriendatHishome behaving like Non-Generic i.e.(Behavior from Vegetarian to Non-Vegetarian).
Hello Java Lovers in this article we are going to see Java Generic objects behavior in Non-Generic area, and Java Non-Generic object behavior in Java Generic area.
So lets begin...
First See This Example Of ArrayList With Non-Generic Concept And Try To Understand Problem With This Example.
Example of Java Non-Generic ArrayList.
import java.util.*;
public class MyClass{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add("Matin");
al.add("Sharavan");
al.add(10);
System.out.println(al);
String s=(String)al.get(2);
System.out.println(s);
}
}
output:
[Matin,Shravan,10]
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at com.JavaTpoint.TestMain.main(MyClass.java:line no)
-So in the above example we added two String and one Integer elements to ArrayList,after that we are printing the elements present inside ArrayList Ok.
-After that we used get() method to get 2nd index element,but here we don't know the type of elements,we are holding 2nd index element in String then we will get compile time error but after Type Casting error will be resolved but at run time we will get Class Cast Exception.
-So in the above example Type Safety and Type Casting problem raised,to resolve these problems SUN People came with Generic Concept.See below example...
Now Example of Java Generic ArrayList.
- import java.util.*;
- public class MyClass{
- public static void main(String args[]){
- ArrayList<String> al=new ArrayList<String>();
- al.add("Matin");
- al.add("Sharavan");
- al.add("Sagar");
- //al.add(10);//compile time error
- System.out.println(al);
- String s=al.get(2);
- System.out.println(s);
- }//end of main
- }//end of class
[Matin,Shravan,Sagar]
Sagar
-Now here we can add only String type of elements,so we can give the guarantee the Type of Elements present inside ArrayList.
-So Here Type Casting is not required because we know the type of elements.
-Achieved Type Safety.
-Compile time checking,See line number 8,at line number 8 i am trying to add Integer element then immediately we are getting compile time error saying,The method add(int, String) in the type ArrayList<String> is not applicable for the arguments (int).
-Compile time error is better than Run Time.
Now Java Generic Object Behavior in Non-Generic Method/Area.
Consider the example given below,read it carefully.
Real Life Example:
-One day in morning i went to my friends home (he is pure vegetarian) to discuss something,while discussing i asked him do you eat Non-Veg,then in front of his parent he said no no how i can eat Non-Veg as i am vegetarian.
-After sometime me and my friend came back to my home,i am Non-Vegetarian,I took a plate and served chicken and i start eating my friend is also there with me,he asked me hey what you are eating then i told this is chicken,suddenly in middle my friend took one piece of chicken and start eating,then i asked him oye you are vegetarian then how you can eat chicken then he told usually i eats Non-Veg's then i asked him but in the morning when i asked do you eat Non-Veg's then you said no no i am vegetarian then he said no no i also eat Non-Veg's too but when i am outside from my home.
-Again in the evening i went to my same friend's home to play chess then at that time intentionally i asked him do you eat Non-Veg's,again he told no no i am pure vegetarian how i can eat Non-Veg's in front of his parent's ;).
-So here my friend is vegetarian at his home in front of his parent's.
But outside he can eat Non-Veg's too,means he is changing his behavior accordingly.
Now see below practical example...
import java.util.*;
public class MyClass{
public static void myFriendatMyHome(ArrayList myfriends) {
myfriends.add(1);
myfriends.add(4.1f);
myfriends.add(66.45);
myfriends.add("Chicken");
}
public static void main(String[] args){
ArrayList <String> myFriendatHishome=new ArrayList <String>();
myFriendatHishome.add("Patato");
myFriendatHishome.add("Tomato");
myFriendatHishome.add("Onion");
//myFriendatHishome.add(1);//Compile time error
//Consider 1 as chicken
System.out.println("My Friend can eat/add only "+myFriendatHishome+" at his home in front of his parent's");
myFriendatMyHome(myFriendatHishome);
//passing Generic ArrayList Object to Non-Generic Area/Method
System.out.println("But At My Home he can eat/add "+myFriendatHishome+" also");
}
}
Output:
My Friend can eat/add only [Patato, Tomato, Onion] at his home in front of his parent's
But At My Home he can eat/add [Patato, Tomato, Onion, 1, 4.1, 66.45, Chicken] also.
-So from the above example myFriendatHishome he can eat/add only vegetables but after coming at myFriendatMyHome(myFriendatHishome); he can eat/add Chicken/Mutton etc.
-Means behavior of My Friend changing accordingly.
-Communication of Generic Code with Non-Generic Code.
-When we passed Generic ArrayLsit object myFriendatHishome to Non-Generic Area/Method myFriendatMyHome(myFriendatHishome) then My Friend i.e. myFriendatHishome behaving like Non-Generic i.e.(Behavior from Vegetarian to Non-Vegetarian).
Similarly a Non-Generic Object in will behave like Generic in Generic Method/Area.
Consider Below Example...
import java.util.*;
public class MyClass{
public static void genericArea(ArrayList<String> generic) {
generic.add("Virat");
generic.add("Rohit");
generic.add("Cricket");
//generic.add(1);//Compile Time Error
}
public static void main(String[] args){
ArrayList nonGeneric=new ArrayList();
nonGeneric.add(1);
nonGeneric.add("Tomato");
nonGeneric.add(66.45);
nonGeneric.add(5.6f);
System.out.println("Non-Generic "+nonGeneric);
genericArea(nonGeneric); //passing generic object to non generic //area genericArea(nonGeneric)
System.out.println("After passing non generic obj to generic area/method "+nonGeneric);
}//end of main
}//end of class
Output:
Non-Generic [1, Tomato, 66.45, 5.6]
After passing non generic obj to generic area/method [1, Tomato, 66.45, 5.6, Virat, Rohit, Cricket]
In the above example we are passing a non generic object
nonGeneric to generic method/area,so the behavior of non generic obj will be changed to generic behavior.
public class MyClass{
public static void genericArea(ArrayList<String> generic) {
generic.add("Virat");
generic.add("Rohit");
generic.add("Cricket");
//generic.add(1);//Compile Time Error
}
public static void main(String[] args){
ArrayList nonGeneric=new ArrayList();
nonGeneric.add(1);
nonGeneric.add("Tomato");
nonGeneric.add(66.45);
nonGeneric.add(5.6f);
System.out.println("Non-Generic "+nonGeneric);
genericArea(nonGeneric); //passing generic object to non generic //area genericArea(nonGeneric)
System.out.println("After passing non generic obj to generic area/method "+nonGeneric);
}//end of main
}//end of class
Output:
Non-Generic [1, Tomato, 66.45, 5.6]
After passing non generic obj to generic area/method [1, Tomato, 66.45, 5.6, Virat, Rohit, Cricket]
In the above example we are passing a non generic object
nonGeneric to generic method/area,so the behavior of non generic obj will be changed to generic behavior.
great explanation...great job.
ReplyDelete