Shelly Verma Shelly Verma - 1 year ago 56
Scala Question

Scala inheritance: why do I have to define my classes to be abstract?

class inheritance
{
def a()
println("Version 1")
}

class inheritance1
{
def b()
println("version 2")
}

class inheritance2 extends inheritance
{
def c()
println("version 3")
}

class inheritance3 extends inheritance2
{
def d()
println("version 4")
}

object inherited
{
def main(args: Array[String])
{
var obj = new inheritance3
obj.d
obj.c
}
}


It throws a few errors:

class inheritance needs to be abstract since method a is not defined

class inheritance1 needs to be abstract since method b is not defined

class inheritance2 needs to be abstract since it has two unimplemented members

class inheritance3 needs to be abstract since it has two unimplemented members

Answer Source

Something like

def a()
println("foo")

Declares an undefined method a that takes no parameters and returns Nothing and then prints out a string.

def a() = println("foo")

or

def a() {
  println("foo")
}

or

def a() = 
  println("foo")

Define a method a, that prints out a string and returns a Unit.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download