This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Overriding inner class of object

4 replies
tommycli
Joined: 2010-01-17,
User offline. Last seen 42 years 45 weeks ago.

Hi, is it possible to override the inner object of a class?

Example:

object Clock {
class Hand(arg: String) {
...
}
val hour = new Hand("second")
val minute = new Hand("minute")
}

object UberClock extends Clock {
override class Hand(arg: String) extends Clock.Hand(arg) {
...
}
}

This is pretty much trying to extend a class that wasn't designed for
extensibility - but is it possible?

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: Overriding inner class of object

On Saturday January 16 2010, tommycli wrote:
> Hi, is it possible to override the inner object of a class?

It's not even possible to extend an object...

This works (by which I mean it compiles). Perhaps it corresponds
to what you want to do or something like what you want to do:

class Clock {
class Hand(arg: String) { }
val hour = new Hand("second")
val minute = new Hand("minute")
}

class SubClock extends Clock {
class Hand(arg: String) extends super.Hand(arg) { }
}

Randall Schulz

Michal Politowski 2
Joined: 2009-09-15,
User offline. Last seen 42 years 45 weeks ago.
Re: Overriding inner class of object

On Sat, 16 Jan 2010 15:51:29 -0800, tommycli wrote:
>
> Hi, is it possible to override the inner object of a class?

You mean a member class of an object?

> Example:
>
> object Clock {
> class Hand(arg: String) {
> ...
> }
> val hour = new Hand("second")
> val minute = new Hand("minute")
> }
>
> object UberClock extends Clock {

This you cannot do since Clock is an object not a class,
you extend (class-)types, not their instances.
So, if you want to inherit the hour and minute vals,
this is the impossible part.

> override class Hand(arg: String) extends Clock.Hand(arg) {

But you can of course extend Clock.Hand, including in some object
definition, it just won't be any override.

> ...
> }
> }

So,

object UberClock {
class Hand(arg: String) extends Clock.Hand(arg) {
...
}
val hour = new Hand("second")
val minute = new Hand("minute")
}

> This is pretty much trying to extend a class that wasn't designed for
> extensibility - but is it possible?

tommycli
Joined: 2010-01-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Overriding inner class of object

Is there a way to get and extend an object's class? I tried something like
this, but it didn't work:

object UberClock extends classOf[Clock] {
...
}

Michal Politowski wrote:
>
> On Sat, 16 Jan 2010 15:51:29 -0800, tommycli wrote:
>>
>> Hi, is it possible to override the inner object of a class?
>
> You mean a member class of an object?
>
>> Example:
>>
>> object Clock {
>> class Hand(arg: String) {
>> ...
>> }
>> val hour = new Hand("second")
>> val minute = new Hand("minute")
>> }
>>
>> object UberClock extends Clock {
>
> This you cannot do since Clock is an object not a class,
> you extend (class-)types, not their instances.
> So, if you want to inherit the hour and minute vals,
> this is the impossible part.
>
>> override class Hand(arg: String) extends Clock.Hand(arg) {
>
> But you can of course extend Clock.Hand, including in some object
> definition, it just won't be any override.
>
>> ...
>> }
>> }
>
> So,
>
> object UberClock {
> class Hand(arg: String) extends Clock.Hand(arg) {
> ...
> }
> val hour = new Hand("second")
> val minute = new Hand("minute")
> }
>
>> This is pretty much trying to extend a class that wasn't designed for
>> extensibility - but is it possible?
>

Andreas Hofstadler
Joined: 2009-02-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Overriding inner class of object

Hi,

I would do it that way:

abstract class ClockImpl {
class Hand(arg: String) {
...
}
}

object Clock extends ClockImpl {
val hour = new Hand("hour")
val minute = new Hand("minute")
}

object UberClock extends ClockImpl {
class Hand(arg: String) extends super.Hand(arg) {
...
}
val hour = new Hand("hour")
...
}

tommycli wrote:
> Hi, is it possible to override the inner object of a class?
>
> Example:
>
> object Clock {
> class Hand(arg: String) {
> ...
> }
> val hour = new Hand("second")
> val minute = new Hand("minute")
> }
>
> object UberClock extends Clock {
> override class Hand(arg: String) extends Clock.Hand(arg) {
> ...
> }
> }
>
> This is pretty much trying to extend a class that wasn't designed for
> extensibility - but is it possible?
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland