Menu
Question Index
...

What will be the output of the following program?

public class GenericsLowerBoundedWildCardDemo1 {
    public static void main(String args[]) {
        PrintClassNameHelper<Shape> shapeClass = new PrintClassNameHelper<Shape>(new Shape());
        print(shapeClass);
    }
    
    static void print(PrintClassNameHelper<? super Rectangle> printName) {
        printName.print();
    }
}
class Shape {
    void printName() {
        System.out.println("This is shape class");
    }
}

class Rectangle extends Shape {
    void printName() {
        System.out.println("This is Rectangle class");
    }
}
class Square extends Rectangle {
    void printName() {
        System.out.println("This is Square class");
    }
}
class PrintClassNameHelper<T extends Shape> {
    T shape;
    PrintClassNameHelper(T shape) {
        this.shape = shape;
    }
    void print() {
        shape.printName();
    }
}


This is shape class
This is Rectangle class
This is Square class
Compilation Error
Runtime Error

Doubts

Problems

Topic: Generics Wildcards In Java With Examples

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App