[scala] Inheritance with parameterized classes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[scala] Inheritance with parameterized classes

Емил Иванов / Emil Ivanov
I have no idea why this does not compile:
I expect Employee to "just get" the name and age fields!?

class Person(val name: String, val age: Int)
class Employee extends Person

object OO {
  def main(args: Array[String]) {
    var bob = new Employee("Bob", 45)
    println(bob.age)
  }
}
Reply | Threaded
Open this post in threaded view
|

Re: [scala] Inheritance with parameterized classes

Andrés Testi
class Employee(n: String, a: Int) extends Person(n, a)

- Andrés

2009/5/16 Emil Ivanov <[hidden email]>:

> I have no idea why this does not compile:
> I expect Employee to "just get" the name and age fields!?
>
> class Person(val name: String, val age: Int)
> class Employee extends Person
>
> object OO {
>  def main(args: Array[String]) {
>   var bob = new Employee("Bob", 45)
>   println(bob.age)
>  }
> }
>
Reply | Threaded
Open this post in threaded view
|

Re: [scala] Inheritance with parameterized classes

Jan Lohre
In reply to this post by Емил Иванов / Emil Ivanov
(val name : String, val age : Int) is the primary constructor of Person. Your Employee class has to call that in its own constructor. So you will have to do:

class Person(val name: String, val age: Int)
class Employee(name: String, age: Int) extends Person(name, age)

Note that you do not have to override the fields.

Kind regards,
Jan

2009/5/16 Emil Ivanov <[hidden email]>
I have no idea why this does not compile:
I expect Employee to "just get" the name and age fields!?

class Person(val name: String, val age: Int)
class Employee extends Person

object OO {
 def main(args: Array[String]) {
  var bob = new Employee("Bob", 45)
  println(bob.age)
 }
}

Reply | Threaded
Open this post in threaded view
|

Re: [scala] Inheritance with parameterized classes

Stepan Koltsov
In reply to this post by Емил Иванов / Emil Ivanov
Please, send minimal examples that fail as long as compiler output.

===
class Person(val name: String, val age: Int)
class Employee extends Person
===

You probably wanted to write:

===
class Person {
  var name: String
  var age: Int
}
class Employee extends Person
===


In case

===
class Person(val name: String, val age: Int)
===

Person gets constructor with two parameter. It is clearly mentioned in
the compiler output.

S.


On Sat, May 16, 2009 at 17:54, Emil Ivanov <[hidden email]> wrote:

> I have no idea why this does not compile:
> I expect Employee to "just get" the name and age fields!?
>
> class Person(val name: String, val age: Int)
> class Employee extends Person
>
> object OO {
>  def main(args: Array[String]) {
>   var bob = new Employee("Bob", 45)
>   println(bob.age)
>  }
> }
>
Reply | Threaded
Open this post in threaded view
|

Re: [scala] Inheritance with parameterized classes

Randall R Schulz-2
In reply to this post by Andrés Testi
On Saturday May 16 2009, Andrés Testi wrote:

> 2009/5/16 Emil Ivanov <[hidden email]>:
> > I have no idea why this does not compile:
> > I expect Employee to "just get" the name and age fields!?
> >
> > class Person(val name: String, val age: Int)
> > class Employee extends Person
> >
> > object OO {
> >  def main(args: Array[String]) {
> >   var bob = new Employee("Bob", 45)
> >   println(bob.age)
> >  }
> > }
>
> class Employee(n: String, a: Int) extends Person(n, a)
>
> - Andrés

To clarify Andrés' answer, constructors are not inherited (Java is the
same way), so you have to explicity declare them and, if / as
necessary, pass on subclass constructor arguments to the base class.


Randall Schulz
Reply | Threaded
Open this post in threaded view
|

Re: [scala] Inheritance with parameterized classes

Емил Иванов / Emil Ivanov
Thank you all for the reply.

Just one more question:

Why does this does not compile:

class Person(name: String, age: Int)
class Employee(name: String, age: Int) extends Person(name, age)

object OO {
  def main(args: Array[String]) {
    var bob = new Employee("Bob", 45)
    println(bob.age)
  }
}

The compiler complains:

Error:Error:line (7)error: value age is not a member of <empty>.this.Employee
println(bob.age)

If I put var/val in front of name and age in the Person class it compiles.

2009/5/16 Randall R Schulz <[hidden email]>
On Saturday May 16 2009, Andrés Testi wrote:
> 2009/5/16 Emil Ivanov <[hidden email]>:
> > I have no idea why this does not compile:
> > I expect Employee to "just get" the name and age fields!?
> >
> > class Person(val name: String, val age: Int)
> > class Employee extends Person
> >
> > object OO {
> >  def main(args: Array[String]) {
> >   var bob = new Employee("Bob", 45)
> >   println(bob.age)
> >  }
> > }
>
> class Employee(n: String, a: Int) extends Person(n, a)
>
> - Andrés

To clarify Andrés' answer, constructors are not inherited (Java is the
same way), so you have to explicity declare them and, if / as
necessary, pass on subclass constructor arguments to the base class.


Randall Schulz



--
My place to share my ideas:
http://bolddream.com (under contruction)
Reply | Threaded
Open this post in threaded view
|

Re: [scala] Inheritance with parameterized classes

Randall R Schulz-2
On Saturday May 16 2009, Емил Иванов / Emil Ivanov wrote:

> Thank you all for the reply.
>
> Just one more question:
>
> Why does this does not compile:
>
> class Person(name: String, age: Int)
> class Employee(name: String, age: Int) extends Person(name, age)
>
> object OO {
>   def main(args: Array[String]) {
>     var bob = new Employee("Bob", 45)
>     println(bob.age)
>   }
> }
>
> The compiler complains:
>
> Error:Error:line (7)error: value age is not a member of
> <empty>.this.Employee
> println(bob.age)
>
> If I put var/val in front of name and age in the Person class it
> compiles.

That's the answer to your question. With out those keywords, the
constructor parameters are just that: parameters to the constructor. If
you add var or val, then you combine constructor parameters with val or
var field declarations all in one go.

These questions belong on scala-user. And they suggest that you should
be using some of the abundant tutorial materials to get your toes wet
with Scala.


Randall Schulz
Reply | Threaded
Open this post in threaded view
|

Re: [scala] Inheritance with parameterized classes

Jan Lohre
In reply to this post by Емил Иванов / Emil Ivanov
Without val they are just constructor parameters. With val (or var) its still a constructor parameter, but the compiler will add corresponding fields that are initialized to the constructor parameters value.

Kind regards,
Jan

2009/5/16 Емил Иванов / Emil Ivanov <[hidden email]>
Thank you all for the reply.

Just one more question:

Why does this does not compile:

class Person(name: String, age: Int)

class Employee(name: String, age: Int) extends Person(name, age)

object OO {
  def main(args: Array[String]) {
    var bob = new Employee("Bob", 45)
    println(bob.age)
  }
}

The compiler complains:

Error:Error:line (7)error: value age is not a member of <empty>.this.Employee
println(bob.age)

If I put var/val in front of name and age in the Person class it compiles.

2009/5/16 Randall R Schulz <[hidden email]>

On Saturday May 16 2009, Andrés Testi wrote:
> 2009/5/16 Emil Ivanov <[hidden email]>:
> > I have no idea why this does not compile:
> > I expect Employee to "just get" the name and age fields!?
> >
> > class Person(val name: String, val age: Int)
> > class Employee extends Person
> >
> > object OO {
> >  def main(args: Array[String]) {
> >   var bob = new Employee("Bob", 45)
> >   println(bob.age)
> >  }
> > }
>
> class Employee(n: String, a: Int) extends Person(n, a)
>
> - Andrés

To clarify Andrés' answer, constructors are not inherited (Java is the
same way), so you have to explicity declare them and, if / as
necessary, pass on subclass constructor arguments to the base class.


Randall Schulz



--
My place to share my ideas:
http://bolddream.com (under contruction)

Reply | Threaded
Open this post in threaded view
|

Re: [scala] Inheritance with parameterized classes

Емил Иванов / Emil Ivanov
Wow. Big wow.
Not to the parameters - but to you.
The scala community must be the greatest one I've ever encountered.

Thank you very much.

Have a very good day!

Regards

2009/5/16 Jan Lohre <[hidden email]>
Without val they are just constructor parameters. With val (or var) its still a constructor parameter, but the compiler will add corresponding fields that are initialized to the constructor parameters value.

Kind regards,
Jan

2009/5/16 Емил Иванов / Emil Ivanov <[hidden email]>

Thank you all for the reply.

Just one more question:

Why does this does not compile:

class Person(name: String, age: Int)

class Employee(name: String, age: Int) extends Person(name, age)

object OO {
  def main(args: Array[String]) {
    var bob = new Employee("Bob", 45)
    println(bob.age)
  }
}

The compiler complains:

Error:Error:line (7)error: value age is not a member of <empty>.this.Employee
println(bob.age)

If I put var/val in front of name and age in the Person class it compiles.

2009/5/16 Randall R Schulz <[hidden email]>

On Saturday May 16 2009, Andrés Testi wrote:
> 2009/5/16 Emil Ivanov <[hidden email]>:
> > I have no idea why this does not compile:
> > I expect Employee to "just get" the name and age fields!?
> >
> > class Person(val name: String, val age: Int)
> > class Employee extends Person
> >
> > object OO {
> >  def main(args: Array[String]) {
> >   var bob = new Employee("Bob", 45)
> >   println(bob.age)
> >  }
> > }
>
> class Employee(n: String, a: Int) extends Person(n, a)
>
> - Andrés

To clarify Andrés' answer, constructors are not inherited (Java is the
same way), so you have to explicity declare them and, if / as
necessary, pass on subclass constructor arguments to the base class.


Randall Schulz



--
My place to share my ideas:
http://bolddream.com (under contruction)




--
My place to share my ideas:
http://bolddream.com (under contruction)
Reply | Threaded
Open this post in threaded view
|

Re: [scala] Inheritance with parameterized classes

Randall R Schulz-2
On Saturday May 16 2009, Емил Иванов / Emil Ivanov wrote:
> Wow. Big wow.
> Not to the parameters - but to you.
> The scala community must be the greatest one I've ever encountered.

Hey! We can be plenty grumpy when the mood strikes us!


> Thank you very much.
>
> Have a very good day!
>
> Regards


RRS