Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Generics using Bounded Wildcards

Previously we have seen that type parameter can be bounded. In the same way, wildcards can also be bounded.

For better understanding consider a class hierarchy given below:

Java Programming Language Generics using Bounded Wildcards

Here, Father is a superclass of Son, and Son is a superclass of GrandSon.

 class Father 
 {
	int bankBalance = 350000;
	String name;
	
	public void setName(String s) 
	{
		name = s;
	}
 }

 class Son extends Father
 {

 }

 class GrandSon extends Son
 {

 }

Suppose we want that bank balance should be accessed only by Father & his Son, not by Grandson, then we should use bounded wildcard argument to restrict Grandson from getting to access to bank balance.

Let’s see how

 class BoundedWC<T extends Father> 
 {
     T account;
	
     public BoundedWC(T acc)
     {
         account = acc;
     }
	
     public static void privacy(BoundedWC<? super Son> A)  // bounded wildcard used
     {
         System.out.println(A.account.name+" accessed bank balance
                                                           "+A.account.bankBalance);
     }
	
     public static void main(String args[])
     {
         Father f = new Father();
         Son s = new Son();
         GrandSon gs = new GrandSon();
		
         f.setName("Father");
         s.setName("Son");
         gs.setName("GrandSon");
		
         BoundedWC<Father> fth = new BoundedWC<Father>(f);
         BoundedWC<Son> sn = new BoundedWC<Son>(s);
         BoundedWC<GrandSon> gsn = new BoundedWC<GrandSon>(gs);

         privacy(fth);
         privacy(sn);
         //privacy(gsn);
     }
 }

Output

Father accessed bank balance 350000
Son accessed bank balance 350000

Here, if removed the comment at the line,

 privacy(gsn);

We will get compile time error saying,

The method privacy(BoundedWC<? super Son>) in the type BoundedWC<T> is not applicable for the arguments (BoundedWC<GrandSon>)

Because we have bounded the wild card used in the privacy() method.

This is known as a lower bounding of a wildcard.
In Lower bound, only declared subclass type and its superclasses are allowed to operate on a method.

Lower bound is achieved by syntax as given below:

<? super subclass>

Subclass is the name of the class that decides the lower limit.

Since we have used Lower Bound syntax in the privacy() method of above program, privacy() method can be accessed only by Son & its superclass Father.

Similarly, Generics allows us to use Upper Bound.

In Upper Bound, only declared superclass type and its subclasses are allowed to operate on a method.

Upper bound is achieved by syntax as given below:

<? extends superclass>

Superclass is the name of the class that decides the upper limit.

Suppose we want that bank balance should be accessed only by Son & GrandSon, not by Father, then we should use Upper Bound wildcard argument in the privacy() method to restrict Father from getting to access to bank balance.

Let’s check

 class BoundedWC<T extends Father> 
 {
     T account;
	
     public BoundedWC(T acc)
     {
         account = acc;
     }
	
     public static void privacy(BoundedWC<? extends Son> A) // bounded wildcard used
     {
         System.out.println(A.account.name+" accessed bank balance
                                                          "+A.account.bankBalance);
     }
	
     public static void main(String args[])
     {
         Father f = new Father();
         Son s = new Son();
         GrandSon gs = new GrandSon();
		
         f.setName("Father");
         s.setName("Son");
         gs.setName("GrandSon");
		
         BoundedWC<Father> fth = new BoundedWC<Father>(f);
         BoundedWC<Son> sn = new BoundedWC<Son>(s);
         BoundedWC<GrandSon> gsn = new BoundedWC<GrandSon>(gs);

         //privacy(fth);
         privacy(sn);
         privacy(gsn);
     }
 }

Output

Son accessed bank balance 350000
GrandSon accessed bank balance 350000

Here, if removed the comment at the line,

 privacy(fth);

We will get compile time error saying,

The method privacy(BoundedWC<? extends Son>) in the type BoundedWC<T> is not applicable for the arguments (BoundedWC<Father>)

Because we have used Upper B0und wild card argument in the privacy() method.

Therefore it will allow to access privacy() method only by Son & its subclass GrandSon, not Father.

 

Leave a reply