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

  • Home
  • SEARCH
  • 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 8078557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:49:54+00:00 2026-06-05T15:49:54+00:00

I can extend an inner class/trait inside the outer class or inside a class

  • 0

I can extend an inner class/trait inside the outer class or inside a class derived from the outer class. I can extend an inner class of a specific instance of an outer class as in:

class Outer
{
  class Inner{}
}

class OtherCl(val outer1: Outer)
{
  class InnA extends outer1.Inner{}
}

Note: even this seems to compile fine producing very interesting possibilities:

trait OuterA
{ trait InnerA } 

trait OuterB
{ trait InnerB }

class class2(val outerA1: OuterA, val outerB1: OuterB)
{ class Inner2 extends outerA1.InnerA with outerB1.InnerB }

But this won’t compile:

class OtherCl extends Outer#Inner

As far as I can see I’m trying to extend a parametrised class where the type parameter is an instance of the outer class so something to the effect of

class OtherCl[T where T is instance of Outer] extends T.Inner

So is the anyway to extend an inner class/ trait that’s inside an outer trait/class without reference to the outer trait/class?

I am not looking to instantiate the derived inner class without an instance of the outer class only declare its type.

  • 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-05T15:49:55+00:00Added an answer on June 5, 2026 at 3:49 pm

    You can use a trait with a self-type to do something similar. Suppose for example that we have the following:

    class Outer(val x: Int) {
      class Inner {
        def y = x
      }
    }
    

    And we want to add some functionality to Inner without having an Outer around:

    trait MyInner { this: Outer#Inner =>
      def myDoubledY = this.y * 2
    }
    

    Now when we instantiate an Inner we can mix in MyInner:

    scala> val o = new Outer(21)
    o: Outer = Outer@72ee303f
    
    scala> val i = new o.Inner with MyInner
    i: o.Inner with MyInner = $anon$1@2c7e9758
    
    scala> i.myDoubledY
    res0: Int = 42
    

    It’s not exactly what you want, but close.

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

Sidebar

Related Questions

A class can be a subclass of itself if its inner class extend the
In Ruby, there's Modules and you can extend a class by mixing-in the module.
I have a problem making an inner class that extends from JPanel to draw
How can I access a private field of an inner class in another package
Java Q: I can't access a public variable in my parent's class' inner class
Suppose I have the following code: trait Trait1 { trait Inner { val name
I was wondering whether or not I can extend the Enum type in C#
I've a web page, where user can extend the session using AJAX call to
I want button in vertically, for that i can extend the height and shrink
Can I extend WPF commands routing in a way so it would first check

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.