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

  • SEARCH
  • Home
  • 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 8422457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:27:06+00:00 2026-06-10T03:27:06+00:00

I am learning Scala from Martin Odersky’s book Programming in Scala. In his Packages

  • 0

I am learning Scala from Martin Odersky’s book “Programming in Scala”. In his “Packages and Imports” Chapter there is an example that Martin has in which he is demonstrating how import clauses work. To test my understanding of import clauses in Scala, I created a source file called Vegetable.scala in Eclipse under the Scala perspective.This code is identical to Martin’s code, except that I changed the name of the trait from Fruit to Vegetable and the name of the object from Fruits to Vegetables).Shown below is the code for Vegetable.scala

package com.att.scala
import java.awt.Color

trait Vegetable {
  val name: String
  val color: Color
}

object Vegetables {
   object Asparagus extends Vegetable { } 
   object Shallot extends Vegetable { }
   object Cauliflower extends Vegetable { }
   object Spinach extends Vegetable { }
   val veggiePlatter = List(Asparagus, Shallot)
}

These are my findings:
Eclipse does not want to compile this code. It says: “object creation impossible, since: value color in trait Vegetable of type java.awt.Color is not defined value name in trait Vegetable of type String is not defined”

What I hope to accomplish from this code is to understand why this error occurs. How can I make Vegetable available to object Asparagus, Shallot and Cauliflower? My rather (naive) understanding at this point is that since Vegetable and Vegetables occur in the same package and in the same source file, trait Vegetable should be visible to object Asparagus (which extends Vegetable) for example.

Is there something specific about a trait that I need to know before I even use a trait in this manner?

  • 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-10T03:27:07+00:00Added an answer on June 10, 2026 at 3:27 am

    You are missing the implementation of your objects. This should work:

    package com.att.scala
    import java.awt.Color
    
    trait Vegetable {
      val name: String
      val color: Color
    }
    
    object Vegetables {
       object Asparagus extends Vegetable { 
           name = "Asparagus"
           color = new Color(255,255,255)
       } 
       //...
    }
    

    val declarations without a value are similar to abstract methods in Java in that they need an ‘implementation’ i.e. a value.

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

Sidebar

Related Questions

I'm learning Scala and there is a thing that I can't find out about
I‘m learning Scala by the book Programming in Scala. When I try to run
I'm currently learning Scala by working through the Programming in Scala book. So far,
I am working through the book Learning OpenCV from the O'Reilly series and am
Chapter 2 of the new Manning book, Scala in Depth by Josh Sueresh is
I'm learning more about Scala, and I'm having a little trouble understanding the example
I just started learning scala. Is there any way to find CPU time and
I am learning Scala (coming from a background of mostly Java). I am trying
I am learning Play! 2 for Scala, coming from a Django background. In Django
Coming from Java, I am learning Scala. I am interested in game and virtual

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.