Skip to content
Related Articles

Related Articles

Improve Article

Scala | Lower Bound

  • Last Updated : 14 Mar, 2019

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.


    [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.


    // 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)
    // 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

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

    // 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)
    // 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

    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
  • Recommended Articles
    Page :