Scala | Lower Bound

Type Bound basically is a kind of restriction on the parameter or the variable. By the usage of these type bounds we can set up limits for the variables. These bounds help to put up our code into real world examples. We need to impose certain limitations and boundaries to every factor in real life, that is what the Type bounds do in Scala.

There are mainly 2 types of Type bounds:

  • Upper type Bound
  • Lower type Bound
  • We will be more concerned about Lower Bounds in this section. Lower type bounds are used to declare a supertype of another type. A lower bound is denoted by >: . Any lower bound statement can be written as ‘[T>:S]’. Here T is a type parameter and S is a type. The above lower bound statement means that the type parameter T must be either same as S of a supertype of S.

    Lets understand about supertype, suppose we write S>:T then this lower bound statement always has T equal to S or less than S, meaning that T is bound in this case. So we can say T is a supertype of S.

    Syntax:

    [T >: Demo[T]]

    In the above case we have defined a Lower Bound from type parameter T to type Demo. And T must be either a Demo or a supertype of Demo type.

    Let’s take another Example
    If I declare all the classes in the certain format as shown below:

    trait Food
    class eatables extends Food
    class drinks extends Food
    class fruits extends eatables
    class vegetables extends eatables
    class cold_drinks extends drinks
    class hot_drinks extends drinks
    
    class position[A](val place:A) 
    

    Now all the classes as well as the sub-classes have been declared using the above statements. So if we consider the statement position[A], here position is generic and A is the abstract type. This can be further simplified, the position[A] indicates that the value required to pass to place must be of the type A. Which means that if I give a command position[fruits] then it will be created with an instance of Fruits. So to check this we will see one example:

    new position[fruits](new fruits)
    new position[fruits](new drinks)
    new position[eatables](new vegetables)

    In the above given example we will notice that the first statement will get compiled but the second statement will show errors because fruits and drinks are not of the same type. On the other hand the third statement will be compiled because vegetable is subtype of eatables. Now using some lower bound commands in the above given statements:

    class position[A >: fruits](val place=A)

    By this statement we can say that we can position any supertype of fruits which means eatables and Food. So, by this we can definitely see that the lower bounds of Scala can be very useful and adoptive if using some logic for any real world example. Thus we can add all the real life limitations and restrictions over our variables which we are using in the coding language.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala Program To Demonstrate Scala Lower Bound
    class GeeksforGeeks
    class Author extends GeeksforGeeks
    class Geeks extends GeeksforGeeks
      
    class ComputerSciencePortal
    {
        // Declaration of Lower bound
        def display [T >: Geeks](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)
        }
    }

    chevron_right

    
    

    Output:

    GeeksforGeeks@506e1b77
    Geeks@4fca772d
    Author@9807454
    

    Here, Lower bound is defined in class ComputerSciencePortal, and GeeksforGeeks is the Super class of Author. it is accepted in lower bound.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala Program To Demonstrate Scala Lower Bound
    class Principal
    class Teacher extends Principal
    class Student extends Teacher
      
    class School
    {
        // Declaration of lower 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)
    }
    }

    chevron_right

    
    

    Output:

    Principal@506e1b77
    Teacher@4fca772d
    Student@9807454
    

    Here, we have defined Lower bound in Class School i.e, [T >: Teacher] which implies that display method accepts Principal class object or subclass of it(i.e. Teacher) or subclass of Teacher class (i.e. Student) which means that super class of Teacher will be accepted.



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    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.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.