
- Groovy Tutorial
- Groovy - Home
- Groovy - Overview
- Groovy - Environment
- Groovy - Basic Syntax
- Groovy - Data Types
- Groovy - Variables
- Groovy - Optionals
- Groovy - Numbers
- Groovy - Strings
- Groovy - Ranges
- Groovy - Lists
- Groovy - Maps
- Groovy - Dates & Times
Groovy Operators
- Groovy - Operators
- Groovy - Arithmetic Operators
- Groovy - Assignment Operators
- Groovy - Relational Operators
- Groovy - Logical Operators
- Groovy - Bitwise Operators
- Groovy - Spaceship Operator
- Groovy - in Operator
- Groovy - Elvis Operator
- Groovy - Safe Navigation Operator
- Groovy Operator Precedence & Associativity
Control Statements
- Groovy - Decision Making
- Groovy - If Else Statement
- Groovy - Switch Statement
- Groovy - Loops
- Groovy - For Loop
- Groovy - For-in Loop
- Groovy - While Loop
- Groovy - Do While Loop
- Groovy - Break Statement
- Groovy - Continue Statement
Groovy File Handling
- Groovy - File I/O
- Java - Create a File
- Java - Write to File
- Java - Append to File
- Java - Read Files
- Java - Delete Files
- Java - File Properties
- Java - File Existence and Type
- Java - File Size
- Java - File Permissions
- Java - Directories
- Java - Listing Directories
- Java - Filtering Files/Directories
- Java - Deleting Directories
- Java - Renaming Files/Directories
Groovy Error & Exceptions
- Groovy - Exception Handling
- Groovy - try-catch Block
- Groovy - try-with-resources
- Groovy - Multi-catch Block
- Groovy - Nested try Block
- Groovy - Finally Block
- Groovy - throw Exception
- Groovy - Exception Propagation
- Groovy - Built-in Exceptions
- Groovy - Custom Exception
Groovy Multithreading
- groovy - Multithreading
- groovy - Thread Life Cycle
- groovy - Creating a Thread
- groovy - Starting a Thread
- groovy - Joining Threads
- groovy - Naming Thread
- groovy - Thread Scheduler
- groovy - Thread Pools
- groovy - Main Thread
- groovy - Thread Priority
- groovy - Daemon Threads
- groovy - Shutdown Hook
Groovy Synchronization
- groovy - Synchronization
- groovy - Block Synchronization
- groovy - Static Synchronization
- groovy - Inter-thread Communication
- groovy - Thread Deadlock
- groovy - Interrupting a Thread
- groovy - Thread Control
- groovy - Reentrant Monitor
- Groovy - Methods
- Groovy - Methods
- Groovy - Optional parenthesis
- Groovy - Named Arguments
- Groovy - Closures as Arguments
- Groovy - Method Overloading
- Groovy - Method Scope and Visibility
- Groovy - isCase Method
- Groovy - Implicit Return
- Groovy - Variable Arguments
- Groovy - Regular Expressions
- Groovy - Regular Expressions
- Groovy - Defining Regular Expressions
- Groovy - Matcher Object
- Groovy - Regex Tasks
- Groovy - XML
- Groovy - XML
- Groovy - Parsing XML
- Groovy - Creating XML
- Groovy - Modifying XML
- Groovy - Querying XML
- Groovy - Simplified Notation
- Groovy - Closure based Querying
- Groovy - Closure based Creation
- Groovy - JSON
- Groovy - JSON
- Groovy - Parsing JSON
- Groovy - Creating JSON using JsonOutput
- Groovy - Creating JSON using JsonBuilder
- Groovy - Modifying JSON
- Groovy - Error Handling
- Groovy - Handling JSON Arrays
- Groovy - JSON Array Operations
- Groovy - JSON Objects
- Groovy - JSON Object Operations
- Groovy - Generics
- Groovy - Generics
- Groovy - Declaring Generic Types
- Groovy - Bound Type Parameters
- Groovy - Wild Cards
- Groovy - Miscellaneous
- Groovy - Object Oriented
- Groovy - Closures
- Groovy - Annotations
- Groovy - JMX
- Groovy - DSLS
- Groovy - Database
- Groovy - Builders
- Groovy - Command Line
- Groovy - Unit Testing
- Groovy - Template Engines
- Groovy - Meta Object Programming
- Groovy Useful Resources
- Groovy - Quick Guide
- Groovy - Useful Resources
- Groovy - Discussion
Groovy - Bound Type Parameters
Bound Type Parameter provides a way to restrict the type of generics used in Groovy classes or methods. We can define either a lower bound using super or upper bound using extends for the type parameter. This kind of functionality is useful when we want to use only a set of types or types following a certain hiearchy.
Upper Bound Type Parameter
Upper Bound Type parameter restrict type parameter to be a subtype of particular class/interface.
Example.groovy
interface Drawable { void draw() } class Circle implements Drawable { void draw() { println "Circle Drawn" } } class Rectangle implements Drawable { void draw() { println "Rectangle drawn" } } class Canvas<T extends Drawable> { void drawShape(T shape) { // as shape is drawable, we can call draw() method shape.draw() } } // create a circle def circle = new Canvas<Circle>() // prints Circle Drawn circle.drawShape(new Circle()) def rectangle = new Canvas<Rectangle>() // prints Rectangle Drawn rectangle.drawShape(new Rectangle()) // String is not drawable def string = new Canvas<String>() // below line throws error as String is not drawable string.drawShape("Test")
Output
When we run the above program, we will get the following result.
Circle Drawn Rectangle drawn Caught: groovy.lang.MissingMethodException: No signature of method: Canvas.drawShape() is applicable for argument types: (java.lang.String) values: [Test] Possible solutions: drawShape(Drawable)
Lower Bound Type Parameter
Lower Bound Type parameter restrict type parameter to be a supertype of particular class. For example, we want to use only integers. In below example, we're restricting our method to accept either integers or its subtype as shown below −
Example.groovy
void process(List<? super Integer> list) { list.add(10) list.add(new Integer(20)) list.add("ABC") } def listOfIntegers = new ArrayList<Integer>() process(listOfIntegers) // prints [10, 20, ABC] println listOfIntegers def listOfNumbers = new ArrayList<Number>() process(listOfNumbers) // prints [10, 20, ABC] println listOfNumbers def listOfObjects = new ArrayList<Object>() process(listOfObjects) // prints [10, 20, ABC] println listOfObjects def listOfStrings = new ArrayList<String>() process(listOfStrings) // prints [10, 20, ABC] println listOfStrings
Output
When we run the above program, we will get the following result.
[10, 20, ABC] [10, 20, ABC] [10, 20, ABC] [10, 20, ABC]
But as evident from the output, due to type erasure, Groovy bypasses the type restriction. But using generic improves the code readablity and the code clarity.