0
abstract class Parent {
   def filter(p: Parent => Boolean): Parent = filterAcc(p, new Child)
}

class Child extends Parent {
  // ...
}

I am working on Scala tutorial and wondering how the following can be possible.

There are two classes Parent and Child. The Parent class creates an instance of child in the method filter.

How can a parent class refer to a child class which inherits the parent class?

1 Answer 1

1

That is no contradiction. If parent and child are defined within the same compilation unit, then the parent can refer to its sub-class, both symbols/types are known to each other.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.