Skip to main content

Java generics ? super Type and ? extends Type

Hy All, in this post, going to tell you real-time scenario when you use super and extends

? super C

This indicates the lower bound, what exactly means that it can hold a list of object which is super to type C or C

? extends A

This indicates the upper bound, what exactly means that it can hold a list of object which is a child to type A or A.

Below is provided an example for the same,

package com.vinay.stock.poc;

import java.util.ArrayList;
import java.util.List;

/** * @author 912vi */public class GenericDemo {

    public static void main(String[] args) {

        List<A> list = new ArrayList<>();
        list.add(new A());
        list.add(new B());
        list.add(new C());
        test2(list);


        List<B> list1 = new ArrayList<>();
        list1.add(new B());
        list1.add(new C());
        test2(list1);

     /*   List<AA> list2 = new ArrayList<>();        test2(list2);// compile time error because upper bound is A type        */

        List<C> list3 = new ArrayList<>();
        list3.add(new C());
        test3(list3);

        List<B> list4 = new ArrayList<>();
        list4.add(new B());
        test3(list4);


      /*  List<D> list5 = new ArrayList<>();        list5.add(new D());        test3(list5); // compile time error , due to lower bound is C type
        */
    }

    public static void test2(List<? extends A> list) {
        System.out.println(list);
    }

    public static void test3(List<? super C> list) {
        System.out.println(list);
    }
}

class AA {

}

class A extends AA {

}

class B extends A {

}

class C extends B {

}

class D extends C {

}


Comments

Popular posts from this blog

Advanced Kafka Resilience: Dead-Letter Queues, Circuit Breakers, and Exactly-Once Delivery

Introduction In distributed systems, failures are inevitable—network partitions, broker crashes, or consumer lag can disrupt data flow. While retries help recover from transient issues, you need stronger guarantees for mission-critical systems. This guide covers three advanced Kafka resilience patterns: Dead-Letter Queues (DLQs) – Handle poison pills and unprocessable messages. Circuit Breakers – Prevent cascading failures when Kafka is unhealthy. Exactly-Once Delivery – Avoid duplicates in financial/transactional systems. Let's dive in! 1. Dead-Letter Queues (DLQs) in Kafka What is a DLQ? A dedicated Kafka topic where "failed" messages are sent after max retries (e.g., malformed payloads, unrecoverable errors). ...

Project Reactor Important Methods Cheat Sheet

🔹 1️⃣ subscribeOn – "Decides WHERE the Pipeline Starts" 📝 Definition: subscribeOn influences the thread where the data source (upstream) (e.g., data generation, API calls) runs . It affects the source and everything downstream (until a publishOn switches it). Flux<Integer> flux = Flux.range(1, 3) .doOnNext(i -> System.out.println("[Generating] " + i + " on " + Thread.currentThread().getName())) .subscribeOn(Schedulers.boundedElastic()) // Change starting thread .map(i -> { System.out.println("[Processing] " + i + " on " + Thread.currentThread().getName()); return i * 10; }); flux.blockLast(); Output: [Generating] 1 on boundedElastic-1 [Processing] 1 on boundedElastic-1 [Generating] 2 on boundedElastic-1 [Processing] 2 on boundedElastic-1 [Generating] 3 on boundedElastic-1 [Processing] 3 on boundedElastic-1 📢 Key Insight: ...

🔄 Kafka Producer Internals: send() Explained with Delivery Semantics and Transactions

Kafka Producer Internal Working Apache Kafka is known for its high-throughput, fault-tolerant message streaming system. At the heart of Kafka's data pipeline is the Producer —responsible for publishing data to Kafka topics. This blog dives deep into the internal workings of the Kafka Producer, especially what happens under the hood when send() is called. We'll also break down different delivery guarantees and transactional semantics with diagrams. 🧠 Table of Contents Kafka Producer Architecture Overview What Happens When send() is Called Delivery Semantics Kafka Transactions & Idempotence Error Handling and Retries Diagram: Kafka Producer Internals Conclusion 🏗️ Kafka Producer Architecture Overview Kafka Producer is composed of the following core components: Serializer : Converts key/value to bytes. Partitioner : Determines which partition a record should go to. Accumulator : Buffers the records in memory be...