1

I'm starting to learn Scala programming and OOP programming. I don't understand the concept of abstract class. I have read this example:

Consider the task of writing a class for sets of integer numbers with two operations,incl and contains. (s incl x) should return a new set which contains the element x together with all the elements of set s. (s contains x) should return true if the set s contains the element x, and should return false otherwise. The interface of such sets is given by:

abstract class IntSet {
  def incl(x: Int): IntSet
  def contains(x: Int): Boolean
}

Let’s say, we plan to implement sets as binary trees. There are two possible forms of trees. A tree for the empty set, and a tree consisting of an integer and two subtrees. Here are their implementations.

class EmptySet extends IntSet {
  def contains(x: Int): Boolean = false
  def incl(x: Int): IntSet = new NonEmptySet(x, new EmptySet, new EmptySet)
}



class NonEmptySet(elem: Int, left: IntSet, right: IntSet) extends IntSet { 
  def contains(x: Int): Boolean =
    if (x < elem) left contains x
    else if (x > elem) right contains x else true
  def incl(x: Int): IntSet =
    if (x < elem) new NonEmptySet(elem, left incl x, right)
    else if (x > elem) new NonEmptySet(elem, left, right incl x) 
    else this
}

Both EmptySet and NonEmptySet extend class IntSet. This implies that types EmptySet and NonEmptySet conform to type IntSet – a value of type EmptySet or NonEmptySet may be used wherever a value of type IntSet is required.

It is not clear to me the why is useful introducing the anonymous class since in the two class that extends the anonymous one there is again the definition of the incl and contains.

Thanks!

1
  • I am not able to understand how recursion works in incl method. Please help Apr 2, 2017 at 9:26

2 Answers 2

0

The main advantage of using InClass (which is an abstract class, not an anonymous class) is that it defines a type of element whose instances have always to implement incl and contains thus allowing you to abstract the implementation details when managing, in your case, set objects.

One example of how this is great is that you could have a list of sets Empty and NomEmpty together and use them indistinctly using the contract given by your abstract class:

val l = List[IntSet](new EmptySet, new NomEmptySet(1, new EmptySet, new EmptySet)
val setsContainingThree = l.filter(_.contains(3))

Your code makes use of that, note that I wrote new NomEmptySet(1, new EmptySet, new EmptySet) but it could also be:

new NomEmptySet(1, new EmptySet, new NomEmptySet(2, new EmptySet, new EmptySet))

Your NonEmptySet constructor expects InSet therefore your actual parameters can by of any type which is a sub type of InSet, that is: EmptySet or NonEmptySet.

This is not a Scala or Java feature but a basic principle of OOP.

0

The class is not anonymous, it is only abstract. Which -in simple words- means you cannot instantiate it. However, you could use it for example in a method signature... you will never know which implementation is passed to your method, but both offer the same interface to interact with them (because both are IntSets).

If it helps, you can think about an abstract class as an interface with method implementations, or as a simple form of a trait.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.