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.
[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.
Here, Upper bound is defined in class ComputerSciencePortal, and GeeksforGeeks is the Super class of Author so its not accepted.
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.
- Lower and Upper Bound Theory
- Scala | Lower Bound
- Set in Scala | Set-2
- Set in Scala | Set-1
- Scala | Either
- Scala Map
- Recursion in Scala
- Scala | map() method
- Scala sum Map values
- Scala Map min() method with example
- Scala Map take() method with example
- How to sort a Scala Map by value
- while and do while Loop in Scala
- ListSet in Scala
- Scala | Product3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.