java wildcard_Java Collections using wildcard

Java Generics : Wild Cards in Collections

extends

super

?

Today I am going to explain you how the wild cards are useful. To understand this concept is bit difficult

Now Suppose you have abstract class and in that you have abstract method called paintObject().

Now you want to use different type of collection in every child class.

This below is AbstractMain Method.

Here Steps we have taken for this Abstract Main method

1. We have created abstract class

2. In Parameter we have define T(you can use any character)

--In this case whichever class implement this method it can used any type of class.

ex. Class can implement method like

public void paintObject(ArrayList object) or public void paintObject(HashSet object)

3. And We have also used E extends MainColorTO

-- In this case E extends MainColorTo

-- It's clearly means whichever class you want to use that must be sub class of MainColorTo

4. We have define abstract method called paintObject(T object,E objectTO)

--Now here whichever class is implement method that method can use any class on first argument and second parameter that method has to use type of MainColorTO

public abstract class AbstractMain {

public abstract void paintObject(T Object,E TO);

}

Now we will extend above abstract class and implement method on below class

ex.

public class MainColorTO {

public void paintColor(){

System.out.println("Paint Color........");

}

}

public class RedTO extends MainColorTO {

@Override

public void paintColor() {

System.out.println("RedTO......");

}

}

public class WhiteTO extends MainColorTO {

@Override

public void paintColor() {

System.out.println("White TO......");

}

}

Now we will take two example.

1.PaintHome.java

public class PaintHome extends AbstractMain {

@Override

public void paintObject(ArrayList arrayList,RedTO red) {

System.out.println(arrayList);

}

}

Now in above PaintHome.java you can check that we have used ArrayList in first argument(As we can take any class) and in second argument we have used RedTO(Which is extending MainColorTO)

2.PaintCar.java

public class PaintCar extends AbstractMain{

@Override

public void paintObject(HashSet Object,WhiteTO white) {

System.out.println(Object);

}

}

Now in above PaintCar.java you can check that we have used HashSet in first argument(As We Can take any class) and in second argument we have used WhiteTO(Which is extending MainColorTO)

Ponint to Remember

You can not use super keyword at class level you can only use extends keyword at class level defination

public abstract class AbstractMain

{

public abstract void paintObject(P Object,E TO);

}

Above code will give you compiler error.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值