Related Articles
Scala | Upper bound
• Last Updated : 14 Mar, 2019

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)``    ``}``}`
Output:
```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)``  ``}``}`
Output:
```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.

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.5501307,"math_prob":0.50977904,"size":2224,"snap":"2021-21-2021-25","text_gpt3_token_len":590,"char_repetition_ratio":0.12792793,"word_repetition_ratio":0.14243324,"special_character_ratio":0.22616906,"punctuation_ratio":0.10382514,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98027444,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-05-15T21:04:23Z\",\"WARC-Record-ID\":\"<urn:uuid:64ec5dbb-6c83-4324-bc50-256a4762548b>\",\"Content-Length\":\"96458\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bbea4b12-7068-4d5d-897f-24208f836dc1>\",\"WARC-Concurrent-To\":\"<urn:uuid:0b306dfc-211a-4b99-99c0-62a2b6a00d98>\",\"WARC-IP-Address\":\"23.62.230.9\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/scala-upper-bound/\",\"WARC-Payload-Digest\":\"sha1:PJJEGYZCY3X5HCORBSOQNBCOV3G5EYAJ\",\"WARC-Block-Digest\":\"sha1:5RZVMIOIS62IXJQQVXJUQSEORRCD75DU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-21/CC-MAIN-2021-21_segments_1620243991378.52_warc_CC-MAIN-20210515192444-20210515222444-00590.warc.gz\"}"}