user3619698 user3619698 - 1 year ago 102
Scala Question

Can an object extend abstract class in scala?

I am newbie to scala . I am trying to create an Object that extends abstract class like show below

object Conversions extends UnitConversions
override def inchesToCentimeters(inches:Int) = inches * 2.5
override def gallonsToLiters(gallons:Int) = gallons * 3.78
override def milesToKilometers(miles:Int) = miles * 1.6

abstract class UnitConversions
def inchesToCentimeters(inches:Int)
def gallonsToLiters(gallons:Int)
def milesToKilometers(miles:Int)

While i try to access the object's member functions i get () this expression as output .

Conversions.milesToKilometers(20) //I get output ()

More over is the below statement valid ???

var ucv:UnitConversions = new Conversions
println(ucv.milesToKilometers(3)) // I get output () here as well

Thanks in Advance !

Answer Source

You need to provide a return type for the functions, otherwise they return Unit:

abstract class UnitConversions {
  def inchesToCentimeters(inches:Int): Double
  def gallonsToLiters(gallons:Int): Double
  def milesToKilometers(miles:Int): Double
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download