Reputation: 135
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 !
Upvotes: 5
Views: 4673
Reputation: 53819
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
}
Upvotes: 9
Reputation: 422
Regarding this question:
More over is the below statement valid ???
var ucv:UnitConversions = new Conversions println(ucv.milesToKilometers(3))
This doesn't compile. object
basically means singleton and can be used for "static" methods. It doesn't make sense to create more than one instance of a singleton. Take a look at this question: Difference between object and class in scala
Upvotes: 0