Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9226823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:55:20+00:00 2026-06-18T04:55:20+00:00

In Scala, suppose classes A and B that has a common method, but not

  • 0

In Scala, suppose classes A and B that has a common method, but not inherited any common interface:

class A {
  def foo() {
    println("A.foo")
  }
}

class B {
  def foo() {
    println("B.foo")
  }
}

What I want to do is to define a trait that extends any classes that has foo() method. I tried this:

type Fooable = {
  def foo()
}

trait Q extends Fooable {
  override def foo() {
    println("before super.foo")
    super.foo()
    println("after super.foo")
  }
}

(new A with Q).foo()
(new B with Q).foo()    

However, Scala compiler rejects this code with an error:

error: class type required but AnyRef{def foo(): Unit} found trait Q extends Fooable {

The best solution is to make an interface that contains common methods and modify classes A and B to inherit the interface. Unfortunately, we cannot modify these classes. This is common in Java libraries such as:

  • java.sql.Connection.close() and java.io.Closeable.close()
  • android.app.Activity.onDestroy() and android.app.Service.onDestroy()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:55:21+00:00Added an answer on June 18, 2026 at 4:55 am

    You can’t do it. Here’s the fundamental problem with it, quoting you:

    define a trait that extends any classes

    Scala demands that the class hierarchy to be a tree, which means any element in it will have one, and just one, parent class. It can have many parent traits, and many ancestor classes, but what you ask for is strictly against the hierarchy demanded by Scala.

    Now, you are mixing two concepts here: nominal typing, where the type of something is defined by its class or trait, and structural typing, where the type of something is defined by what methods it implements.

    Nominal typing and structural typing are, however, rather incompatible. In fact, before Scala it was generally agreed on that the two could not cooexist and, in fact, Scala has a very limited version of structural typing. You could do something like this:

    scala> def f(m: { def foo(): Unit }) = {
         |     println("before foo")
         |     m.foo()
         |     println("after foo")
         | }
    f: (m: AnyRef{def foo(): Unit})Unit
    
    scala> f(new A)
    before foo
    A.foo
    after foo
    

    Or you could combine structural types with self types, like this:

    scala> trait Q { self: { def foo(): Unit } =>
         |   def bar() {
         |     println("before foo")
         |     foo()
         |     println("after foo")
         |   }
         | }
    defined trait Q
    
    scala> (new A with Q).bar()
    before foo
    A.foo
    after foo
    
    scala> (new B with Q).bar()
    before foo
    B.foo
    after foo
    

    But you cannot inherit from a structural type, as inheritance is a characteristic exclusive of nominal types. And, because you cannot inherit a structural type, you cannot override it, or call it on an ancestor, which means trait Q cannot declare an override on foo().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a simple class in Scala: class Simple { def doit(a: String):
Suppose I have the following scala classes ( that needs to improve to address
Suppose I have the following code: case class Foo(x: SortedSet[String]) { def bar: Set[String]
Let me explain ;-) both classes below are in package com.company.foo RoleGroup.scala abstract class
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
Scala programmer should have known that this sort of writing : class Person{ var
Suppose a class defines an implicit function that converts an integer to a String:
Suppose I have following code def foo(x:Int):Unit = { if (x == 1) println
Suppose that I have a string in scala and I want to try to
Suppose I have type alias defined in scala as object Foo { type Bar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.