- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Overriding inner class of object
Sun, 2010-01-17, 00:52
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?
Sun, 2010-01-17, 01:27
#2
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?
Sun, 2010-01-17, 05:50
#3
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?
>
Sun, 2010-01-17, 12:27
#4
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?
>










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