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:
class Geek
{
class G 1
{
var a = 0
def method()
{
for (a < - 0 to 3 )
{
println( "Welcome to inner class: G1" );
}
}
}
}
object Main
{
def main(args : Array[String])
{
val obj = new Geek();
val o = new obj.G 1 ;
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 :
class outer _ class
{
object inner _ object
{
val q = 0 ;
def method()
{
for (q < - 0 to 2 )
{
println( "object inside a class example" )
}
println()
}
}
}
object outer _ object
{
class inner _ class
{
val s = 0 ;
def method()
{
for (s < - 0 to 2 )
{
println( "class inside an object example" )
}
}
}
}
object Main
{
def main(args : Array[String])
{
new outer _ class().inner _ object.method;
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Jan, 2019
Like Article
Save Article