Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:49:12+00:00 2026-06-01T04:49:12+00:00

I have been looking at examples online, and tutorials, and I cannot find anything

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:49:13+00:00Added an answer on June 1, 2026 at 4:49 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking at EasyMock and tutorials/examples around using it for Unit Testing
I have been looking at samples of DataTemplateSelectors for sivleright and from the examples
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been looking to implement a custom class of : IList<ArraySegment<byte>> this will
I have been looking at LLVM lately, and I find it to be quite
I'm needing some help with mod rewriting. I have been looking online for the
I have been looking at several examples for configuring a datasource for MySQL in
I have been looking at the examples for updating models in the MVC architecture.
I have been looking online for days trying to solve my problems and I
i have been searching online for examples of how to create an HttpModule, since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.