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
Recommended Posts:
- Class and Object in Scala
- Scala | Case Class and Case Object
- Extending a Class in Scala
- Calling A Super Class Constructor in Scala
- Determine the class of a Scala object
- Call a method on a Super Class in Scala
- Scala Tutorial â Learn Scala with Step By Step Guide
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
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.

