Scala has some restrictions on the Type Parameters or Type Variable i.e, (type bound) and Upper bound is one of them. By implementing Type Bounds, We can explicate the restraints of a Type Variable, these type bound restricts the definite values of the type variables and discloses more details about the member of these types. Upper bound is defined on type parameters.
Syntax
[T <: S]
Where, ‘T’ is a type parameter and ‘S’ is a type“[T <: S]” means this Type Parameter T must be either same as S or Sub-Type of S.
Let’s understand upper bound with example.
Example
// Scala Program To Demonstrate Scala Upper Bound class GeeksforGeeks class Author extends GeeksforGeeks class Geeks extends Author class ComputerSciencePortal { // Declaration of upper bound def display [T <: Author](d : T) { println(d) } } // Object created object ScalaUpperBounds { // Driver code def main(args: Array[String]) { val geeksforgeeks = new GeeksforGeeks val author = new Author val geeks = new Geeks val computerscienceportal = new ComputerSciencePortal // computerscienceportal.display(geeksforgeeks) computerscienceportal.display(geeks) computerscienceportal.display(author) } } |
Geeks@506e1b77 Author@4fca772d
Here, Upper bound is defined in class ComputerSciencePortal, and GeeksforGeeks is the Super class of Author so its not accepted.
Example
// Scala Program To Demonstrate Scala Upper Bound class Principal class Teacher extends Principal class Student extends Teacher class School { // Declaration of upper bound def display [T <: Teacher](t: T) { println(t) } } // Object created object ScalaUpperBounds { // Driver code def main(args: Array[String]) { // Defined new variable val principal = new Principal val teacher = new Teacher val student = new Student val school = new School // school.display(principal) school.display(teacher) school.display(student) } } |
Teacher@506e1b77 Student@4fca772d
Here, we have defined Upper bound in Class School i.e, [T <: Teacher] which implies that display method accepts Teacher class object or subclass of it (i.e. Student) which means that super class of Teacher will not be accepted. So, if Super class of Teacher i.e, Principal is not commented then it will show type mismatch error.
Recommended Posts:
- Lower and Upper Bound Theory
- Scala | Lower Bound
- Scala - View Bound
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala | Functions Call-by-Name
- Program to convert Java list to an iterator in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

