The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Calling super class constructor

4 replies on 1 page. Most recent reply: Dec 26, 2007 8:44 AM by Bill Venners

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Randolph Kahle

Posts: 6
Nickname: randykahle
Registered: Apr, 2006

Calling super class constructor Posted: Dec 24, 2007 8:17 AM
Reply to this message Reply
Advertisement
How do I call the parameterized superclass constructor of a Java superclass from a Scala subclass.

For example for the following Java class:

public class JavaClass
{
public JavaClass(int a, int b)
{
...
}
}


how do I write a Scala subclass that extends JavaClass and calls its parameterized constructor in the Scala class constructor?


Randolph Kahle

Posts: 6
Nickname: randykahle
Registered: Apr, 2006

Re: Calling super class constructor Posted: Dec 24, 2007 9:04 AM
Reply to this message Reply
I need to be more precise, I wish to define a Scala subclass with a no-argument constructor that extends a Java class with a parameterized constructor, so I need to call "'super" and set some parameter values.

Randy

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Calling super class constructor Posted: Dec 24, 2007 4:19 PM
Reply to this message Reply
> I need to be more precise, I wish to define a Scala
> subclass with a no-argument constructor that extends a
> Java class with a parameterized constructor, so I need to
> call "'super" and set some parameter values.
>
> Randy

It's done the same way if the superclass is Scala. Here's an example:


scala> class Fred(x: Int, y: Int) {}
defined class Fred

scala> class Bob extends Fred(1, 2) {}
defined class Bob


After extends you say the name of the superclass. Just put the parameters in parens after that name.

Justin Forder

Posts: 9
Nickname: jufo
Registered: May, 2007

Re: Calling super class constructor Posted: Dec 25, 2007 4:20 AM
Reply to this message Reply
Happy Christmas!

I don't think there's any coverage of superclass constructors in the book at the moment. I was looking for the answer to Randy's question, found it by thought and experiment, but then couldn't find confirmation in the book. I did eventually find a good example on page 65 of the Scala Language Specification.

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Calling super class constructor Posted: Dec 26, 2007 8:44 AM
Reply to this message Reply
> I don't think there's any coverage of superclass
> constructors in the book at the moment. I was looking for
> the answer to Randy's question, found it by thought and
> experiment, but then couldn't find confirmation in the
> book. I did eventually find a good example on page 65 of
> the Scala Language Specification.
>
I'll make sure we add it to the book.

Flat View: This topic has 4 replies on 1 page
Topic: book, reversing Previous Topic   Next Topic Topic: NetKernel and Scala

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use