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

Learning How to Map One-to-Many Relationships in JPA Spring Boot with PostgreSQL

  Introduction In this blog post, we explore how to effectively map one-to-many relationships using Spring Boot and PostgreSQL. This relationship type is common in database design, where one entity (e.g., a post) can have multiple related entities (e.g., comments). We'll dive into the implementation details with code snippets and provide insights into best practices. Understanding One-to-Many Relationships A one-to-many relationship signifies that one entity instance can be associated with multiple instances of another entity. In our case: Post Entity : Represents a blog post with fields such as id , title , content , and a collection of comments . Comment Entity : Represents comments on posts, including fields like id , content , and a reference to the post it belongs to. Mapping with Spring Boot and PostgreSQL Let's examine how we define and manage this relationship in our Spring Boot application: Post Entity  @Entity @Getter @Setter @Builder @AllArgsConstructor @NoArgsCon...

Understanding the Advertisement Domain: A Comprehensive Overview Part 2

 The advertisement domain is a complex and dynamic ecosystem that involves various technologies and platforms working together to deliver ads to users in a targeted and efficient manner. The primary goal is to connect advertisers with their target audience, increasing brand visibility, user engagement, and revenue generation. In this blog, we will delve into the different components of the advertisement ecosystem, key concepts like programmatic advertising and real-time bidding (RTB), and provide a practical example to illustrate how it all works. Key Components of the Advertisement Domain The advertisement domain broadly consists of the following components: Advertisers : These are brands or companies that want to promote their products or services through advertisements. They set up ad campaigns targeting specific user segments. Publishers : These are websites, mobile apps, or digital platforms that display ads to users. Publishers monetize their content by selling ad space to ad...

Tree Based Common problems and patterns

  Find the height of the tree. public class BinaryTreeHeight { public static int heightOfBinaryTree (TreeNode root) { if (root == null ) { return - 1 ; // Height of an empty tree is -1 } int leftHeight = heightOfBinaryTree(root.left); int rightHeight = heightOfBinaryTree(root.right); // Height of the tree is the maximum of left and right subtree heights plus 1 for the root return Math.max(leftHeight, rightHeight) + 1 ; } Find the Level of the Node. private static int findLevel (TreeNode root, TreeNode node, int level) { if (root == null ) { return - 1 ; // Node not found, return -1 } if (root == node) { return level; // Node found, return current level } // Check left subtree int leftLevel = findLevel(root.left, node, level + 1 ); if (leftLevel != - 1 ) { return leftLevel; // Node found ...