Generic Methods And Generic Classes Java Mcq

Posted by admin

Let us take the idea of generics from a simple real life example. I am sure all of us have had the misfortune of opening a box of cookies and discovering a bunch of sewing supplies, at least once. So, let us say that the cookie box is a generic instance that can take different parameters(cookies or sewing supplies) and behaves differently(as a cookie box or a box of sewing supplies) based on the type of parameter received from the user.Generics are used mostly for compile type safety and avoid chances of ClassCasteException.Let’s say we have below code which does not use Generics.We need to create a list which should contain only Integer type value. As you can see, we don’t need to cast at line no.4. We are not able to add String to integerList, that’s great.That was the requirement.As a result of this, we won’t get classCastException anymore. Why Generics?Let us say, that we must sort a list of numbers, characters, and strings based on the input provided by the user.

Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used. It is possible to use both generic methods and wildcards in tandem. Here is the method Collections.copy.

Invoking the class with IntegerInteger Data: 5Invoking the class with StringInteger Data: Five Methods/Functions as GenericsAs we discussed in the previous section, a single generic method can be written which can be invoked using different types of arguments. Depending on the type of arguments passed to the method, the compiler handles each method call as a separate call.Writing Generic Methods in Java is not as an insurmountable task as it seems.

On the contrary, it is quite simple. It is just necessary to remember certain key points while declaring and using generics. How do we declare a Generic Method?A Generic Method can be declared like any other method declaration in Java, keeping in mind the following rules:.

They must have a type parameter part of the method signature that is marked by a pair of angular brackets ( ) and this part is placed right before the method return type declaration. Every type parameter section of the declaration can contain more than one type variables and they are delimited by the use of commas. It is an identifier that defines a generic type name. Type parameters can be used as the return type of the method too and tend to funtion like Placeholders based on the type of arguments passed to the method.

Java generic method example

Generic method type parameters deal with Reference Types(Objects) only, and cannot support primitive type data(int, long, float, double etc)So, a sample method prototype for a Generic Method would be. Working with ‘Bounded Type Parameters’Now that we have discussed Generics and Type Parameters, we have understood that using Generic Type Parameters allows the programmer to set any reference type to a type parameter.

Java Generic Method Return

  1. As we all know that constructors are like methods but without return types. Like methods, constructors also can be generic. Even non-generic class can have generic constructors. Here is an example in which constructor of a non-generic class is defined as generic.
  2. Hello all, In the previous part of this generics series, we learned about The WildCard Operator. In this part, we are going to see how to create custom generic classes. But what to create custom generic classes? Consider a Library. We can have a book library of audio library or cd library, whatever.Library class is.

Java Generic Method Example

But what if we wanted to restrict that freedom and only allow certain types?That is taken care of by using Bounded Type Parameters.Since Java is governed by objects, each reference type has a parent object to which it belongs. To bind the type of parameters, we declare the type parameter followed by the extends keyword (or implements in case of interfaces ) followed by the upper bound class or interface.Let us see an example of using bounded type with the following code which finds the minimum of two numbers.