Open In App

Inner class in Scala

Last Updated : 27 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Inner class means defining a class into another. This feature enables the user to logically group classes that are only used in one place, thus this increases the use of encapsulation, and create more readable and maintainable code. In Scala, the concept of inner classes is different from Java. Like in Java, the inner class is the member of the outer class, but in Scala, the inner class is bound to the outer object.
Syntax:

class Outer_class{
class Inner_class{
// Code..
}
}

Example:




// Scala program to illustrate how to 
// create inner class
  
// Outer class
class Geek
{
      
    // Inner class
    class G1
    {
        var a = 0
        def method()
        {
            for(a<-0 to 3)
            {
                println("Welcome to inner class: G1");
            }
        }
    }
}
object Main 
{
    def main(args: Array[String]) 
    {
          
        // Creating object of the outer and
        // inner class Here, G1 class is 
        // bounded with the object of Geek class
        val obj = new Geek();
        val o = new obj.G1;
        o.method();
    }
}


Output:

Welcome to inner class: G1
Welcome to inner class: G1
Welcome to inner class: G1
Welcome to inner class: G1

Explanation: In the above example, Geek is outer class and G1 is the inner class. Now, to create the object of the inner class, first of all, you need to create the object of the outer class and the object of the outer class is obj. Now, obj is prefixed with G1 class and create the object o of G1 class because the inner class is bound to the object of the outer class.

How to create class inside object and object inside class

In Scala, we can also include a class inside an object or an object inside a class. Let’s discuss with an example. In the below example, first, we create an object inside a class and access the method of the object with the help of new keyword followed by the class name, object name and method name like as shown in the following statement:

new outer_class().inner_object.method;

Now, second, we create a class inside an object and access the methods present in the class accessed with the help of new keyword followed by the object name, class name and method name, as shown in the following statement:

new outer_object.inner_class().method; 

Example :




// Scala program to illustrate how to 
// create an object inside a class, Or
// a class inside an object
  
// Class inside Object
class outer_class
{
    object inner_object
    {
        val q = 0;
        def method()
        {
            for(q <- 0 to 2)
            {
                println("object inside a class example")
            }
            println()
        }
    }
}
  
// Object inside Class
object outer_object
{
    class inner_class
    {
        val s = 0;
        def method()
        {
            for(s <- 0 to 2)
            {
                println("class inside an object example")
            }
        }
    }
}
  
object Main
{
      
    // Main method
    def main(args: Array[String])
    {
          
        // Object inside a class
        new outer_class().inner_object.method;
          
        // Class inside an object
        new outer_object.inner_class().method;
    }
}


Output:

object inside a class example
object inside a class example
object inside a class example

class inside an object example
class inside an object example
class inside an object example


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads