0

In the code below:

class A<T extends InterfaceA & InterfaceB>

what does it mean "T should be a type of InterfaceA"?

for example the next code:

class A<T extends Number>

means that T can be an int, a double or any other numeric types. can anyone give me an example to explain the first code?

5
  • Have you tried it in your IDE and see what you get from it? Commented Jun 28, 2014 at 9:21
  • 2
    Please follow Java naming conventions, InterfaceA not interfaceA. Commented Jun 28, 2014 at 9:25
  • yes i have tried it, but I don't understand what does it mean? does an interface has any type that T extend from it? Commented Jun 28, 2014 at 9:25
  • @Shakiba-Check my code,I hope it helps you understand why only a specific type can be passed which implements both interfaces, interfaceA and interfaceB in your case!If you don't understand,please point to me! Commented Jun 28, 2014 at 10:02
  • @shekharsuman- It was clear, I got the code! Commented Jun 28, 2014 at 10:34

4 Answers 4

2

It says T must be a type that implements both interfaceA and interfaceB interfaces.

Your second example says that T must be of any type that implements the Number interface only.

Sign up to request clarification or add additional context in comments.

Comments

1

class A<T extends interfaceA & interfaceB> means

that T is bounded by two interfaces. Thus,any type argument passed to T must implement interfaceA and interfaceB.

Sample program for your understanding :-

interface X{
public void eat();
}

interface Y{
public void drink();
}

class XYZ implements X,Y{
@Override
public void eat(){
    System.out.println("I am eating.");   
}
@Override
public void drink(){
    System.out.println("I am drinkin too!");
}    
}

class A<T extends X & Y> {
public void display(){
     XYZ x=new XYZ();
     x.eat();
     x.drink();
     System.out.println("Type of XYZ is "+x.getClass().getName());
    }  
}

public class Sample1{
public static void main(String[] args) {
 A<XYZ> a=new A<>();
 a.display();
}    
}

This means that type which argument passed to T must implement interfaces X and Y.

Like shown in the given code :-

A<XYZ> a=new A<>(); //here only XYZ(substituted in place of T) can be passed because it implements both interface X and interface Y

I hope this much helps you understand and point out the differences!!!

4 Comments

I tried it in my IDE, but it has a problem with the line A<XYZ> a=new A<>();
@Shakiba-Which version of Java are you using? And also,which IDE+ which line or what error are you facing???
@Shakiba-Would you mind accepting the answer so that both of us can earn reputations!Please consider only if you were satisfied with my answer!
@Shakiba-You can upvote my answers too,if satisfied,please!THANKS.
1

I would go with @shekhar suman's example, but i would change last class and main:

class A<T extends X & Y> {
  public void display(T t){
       t.eat();
       t.drink();
       System.out.println("Type of T is "+t.getClass().getName());
  }  
}

public static void main(String[] args) {
   A<XYZ> a=new A<>();
   a.display(new XYZ());
}    

Comments

0

If there was no extends like this: Class A<T> Class A was a generic class, accepting any class T.

However in case of Class A<T extends InterfaceA & InterfaceB> T can be any class that implements both interfaceA and interfaceB.

In other words you want to make sure T is not any random class, and satisfies some conditions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.