Scala - Comments



This chapter takes you through the concept of extending a class in Scala programming. A new class (subclass) to inherit properties and methods from an existing class (superclass). So, there is code reuse and the creation of hierarchical class structures.

Extending a Class

You can extend a base Scala class and you can design an inherited class in the same way you do it in Java (use extends key word), but there are two restrictions: method overriding requires the override keyword, and only the primary constructor can pass parameters to the base constructor. Let us extend our above class and add one more class method.

Syntax

The syntax for extending a class is -

class Superclass {
   // Superclass methods and fields
}

class Subclass extends Superclass {
   // Subclass methods and fields
}

Example of Extending a Class

Let us take an example of two classes Point class (as same example as above) and Location class is inherited class using extends keyword. Such an ‘extends’ clause has two effects: it makes Location class inherit all non-private members from Point class, and it makes the type Location a subtype of the type Point class. So here the Point class is called superclass and the class Location is called subclass. Extending a class and inheriting all the features of a parent class is called inheritance but Scala allows the inheritance from just one class only.

Note − Methods move() method in Point class and move() method in Location class do not override the corresponding definitions of move since they are different definitions (for example, the former take two arguments while the latter take three arguments).

Try the following example program to implement inheritance.

import java.io._

class Point(val xc: Int, val yc: Int) {
   var x: Int = xc
   var y: Int = yc
   
   def move(dx: Int, dy: Int) = {
      x = x + dx
      y = y + dy
      println ("Point x location : " + x);
      println ("Point y location : " + y);
   }
}

class Location(override val xc: Int, override val yc: Int,
   val zc :Int) extends Point(xc, yc){
   var z: Int = zc

   def move(dx: Int, dy: Int, dz: Int) = {
      x = x + dx
      y = y + dy
      z = z + dz
      println ("Point x location : " + x);
      println ("Point y location : " + y);
      println ("Point z location : " + z);
   }
}

object Demo {
   def main(args: Array[String]) = {
      val loc = new Location(10, 20, 15);

      // Move to a new location
      loc.move(10, 10, 5);
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

Point x location : 20
Point y location : 30
Point z location : 20

Another Example

Let us take an example of two classes Animal class and Dog class, where the Dog class inherits from the Animal class using the extends keyword. Such an ‘extends’ clause has two effects: it makes Dog class inherit all non-private members from Animal class, and it makes the type Dog a subtype of the type Animal class. So here the Animal class is called superclass and the class Dog is called subclass. Extending a class and inheriting all the features of a parent class is called inheritance but Scala allows the inheritance from just one class only.

Note − Methods sound() method in Animal class and bark() method in Dog class do not override the corresponding definitions of sound since they are different definitions (for example, the former has no parameters while the latter has one parameter).

Try the following example program to implement inheritance.

import java.io._

class Animal(val name: String) {
   def sound() = {
      println(s"$name makes a sound")
   }
}

class Dog(override val name: String) extends Animal(name) {
   def bark(times: Int) = {
      for (_ <- 1 to times) {
         println(s"$name barks")
      }
   }
}

object Demo {
   def main(args: Array[String]) = {
      val dog = new Dog("Buddy")

      // Call methods
      dog.sound()
      dog.bark(3)
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

Buddy makes a sound
Buddy barks
Buddy barks
Buddy barks
Advertisements