0

I have been looking at examples online, and tutorials, and I cannot find anything that explains how this (inheritance) differs from java. Simple example:

class Shape {
String type;
Shape(String type) {
    this.type = type;
  }
...
}

class Square extends Shape {
Square(String name){ 
    Super(name);
  }
....
}

Whats confusing me is in the above example I need to call the super class in order to set the 'type' variable, as well as to access it to tell me the Box objects' type as well. In Scala, how can this be done? I know scala uses traits interfaces as well, but is the above example omitted completely from scala? Can anyone direct me to a good example or explain it. I really appreciate it.

2
  • Classes can be extended in scala as well class Square(val name: String) extends Shape(name). Is that your point of confusion? Commented Mar 30, 2012 at 2:11
  • Well my confusion lies in how its done. In java, when extending, you have to call the super class to set any variables you have as the property. But in scala, I don't see anything on how to extend from a parent class and put the right variables in the right place. Commented Mar 30, 2012 at 2:13

1 Answer 1

5

You can write almost exactly the same thing in Scala, much more concisely:

class Shape(var `type`: String)
class Square(name: String) extends Shape(name)

In the first line, the fact that type is preceded by var makes the compiler add getters and setters (from "5.3 Class Definitions" in the specification):

If a formal parameter declaration x : T is preceded by a val or var keyword, an accessor (getter) definition (§4.2) for this parameter is implicitly added to the class. The getter introduces a value member x of class c that is defined as an alias of the parameter. If the introducing keyword is var, a setter accessor x _= (§4.2) is also implicitly added to the class.

In the second line name is not preceded by val or var, and is therefore just a constructor parameter, which is this case we pass on to the superclass constructor in the extends clause. No getters or setters are added for name, so if we created an instance square of Square and called square.name, it wouldn't compile.

Note also that type is a keyword in Scala, so I've had to surround it by backticks in both the definition and the example above:

Example 1.1.2 Backquote-enclosed strings are a solution when one needs to access Java identifiers that are reserved words in Scala.

There are many, many resource that you can read for more information about inheritance in Scala. See for example Chapters 4 and 5 of Programming Scala.

Sign up to request clarification or add additional context in comments.

4 Comments

Oh, I see. So in Scala it does that, but then if Square has the variable too, what would the point be of setting any variables in Shape if the child class has it set as well?
How then do I access the parent classes variable from the child? Sorry for the questions. Just want to understand this.
I did, I cannot find anywhere how to access the Super's variables from the child, at least in terms of inheritance.
It didn't show it but I played with it a bit more and I found a way to do it. Not sure if its the right or best way, but it'll do. I really appreciate the hints!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.