{"id":606,"date":"2011-10-25T10:46:00","date_gmt":"2011-10-25T10:46:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/scala-tutorial-objects-classes-inheritance-traits-lists-with-multiple-related-types-apply.html"},"modified":"2012-10-21T20:24:44","modified_gmt":"2012-10-21T20:24:44","slug":"scala-tutorial-objects-classes","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html","title":{"rendered":"Scala Tutorial &#8211; objects, classes, inheritance, traits, Lists with multiple related types, apply"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">\n<h2>   Preface<\/h2>\n<p>This is part 9 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other resources on <a href=\"http:\/\/icl-f11.utcompling.com\/links\">the links page of the Computational Linguistics course<\/a> I\u2019m creating these for.&nbsp;Additionally you can find this and other tutorial series on the JCG Java <a href=\"http:\/\/www.javacodegeeks.com\/p\/java-tutorials.html\">Tutorials page<\/a>.<\/p>\n<p>This tutorial is about object-oriented programming with Scala. Most of what we\u2019ve seen so far has been programming with functions and using basic types, like Int, Double, and String, and with predefined types like List and Map. As it turns out, these are all classes, or types of Scala data structures that allow one to create objects, or instances of the type. This tutorial will not give a broad introduction to object-oriented programming, but it will give some practical examples of classes and objects and how to use them. I apologize in advance for some sloppiness in the presentation of object-oriented concepts; the intent is to get across the ideas for beginners mainly through intuitive examples without being mired in lots of technical details. See <a href=\"http:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming\">the Wikipedia page on object-oriented programming<\/a> for more detail.<\/p>\n<p>Note that the definitions of objects and classes in this tutorial are most easily viewed as plain text, out of the REPL. So, I\u2019ll put a piece of code into the text, and you should add it to your own REPL (by simply cutting and pasting) in order to be able to follow along.<\/p>\n<h2>   Objects<\/h2>\n<p>At its core, an object can be thought of as a structure that encapsulates some data and functions. Let\u2019s start with an an example of an object representing a person and some of their possible attributes.<\/p>\n<pre class=\"brush: scala;\">object JohnSmith {\r\n  val firstName = \"John\"\r\n  val lastName = \"Smith\"\r\n  val age = 37\r\n  val occupation = \"linguist\"\r\n\r\n  def fullName: String = firstName + \" \" + lastName\r\n\r\n  def greet (formal: Boolean): String = {\r\n    if (formal)\r\n      \"Hello, my name is \" + fullName + \". I'm a \" + occupation + \".\"\r\n    else\r\n      \"Hi, I'm \" + firstName + \"!\"\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>If you put this into the Scala REPL, you\u2019ll be able to access the fields (<strong>firstName<\/strong>, <strong>lastName<\/strong>, <strong>age<\/strong>, and <strong>occupation<\/strong>) and the functions (<strong>fullName<\/strong> and <strong>greet<\/strong>).<\/p>\n<pre class=\"brush: scala;\">scala&gt; JohnSmith.firstName\r\nres0: java.lang.String = John\r\n\r\nscala&gt; JohnSmith.fullName\r\nres1: String = John Smith\r\n\r\nscala&gt; JohnSmith.greet(true)\r\nres2: String = Hello, my name is John Smith. I'm a linguist.\r\n\r\nscala&gt; JohnSmith.greet(false)\r\nres3: String = Hi, I'm John!\r\n<\/pre>\n<p>So, at its most basic level, an object is just that: a collection of values and functions (also often called methods). You can access any of those values or functions by giving the name of the object followed by a period followed by the value or function you want to use. This can be useful for organizing such collections, but it also leads to many more possibilities, as we\u2019ll see.<\/p>\n<p>We might of course be interested in having the information about another person encapsulated in this way. We could do this by mimicking the definition for John Smith.<\/p>\n<pre class=\"brush: scala;\">object JaneDoe {\r\n  val firstName = \"Jane\"\r\n  val lastName = \"Doe\"\r\n  val age = 34\r\n  val occupation = \"computer scientist\"\r\n\r\n  def fullName: String = firstName + \" \" + lastName\r\n\r\n  def greet (formal: Boolean): String = {\r\n    if (formal)\r\n      \"Hello, my name is \" + fullName + \". I'm a \" + occupation + \".\"\r\n    else\r\n      \"Hi, I'm \" + firstName + \"!\"\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>After adding the above code to the REPL, now Jane Doe can greet us.<\/p>\n<pre class=\"brush: scala;\">scala&gt; JaneDoe.greet(true)\r\nres4: String = Hello, my name is Jane Doe. I'm a computer scientist.\r\n\r\nscala&gt; JaneDoe.greet(false)\r\nres5: String = Hi, I'm Jane!\r\n<\/pre>\n<p>Of course, I created the <strong>JaneDoe<\/strong> object by doing a copy-and-paste and then replacing the fields with Jane Doe\u2019s information. This leads to a lot of wasted effort: the fields are the same, but the values are different, and the functions are completely identical. If you want to change something about the way greetings are made, you\u2019d have to update it across all of the objects.<\/p>\n<p>More importantly, these two objects are completely distinct from one another: one cannot put them in a list and map a function over that list. Consider the following failed attempt.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val people = List(JohnSmith, JaneDoe)\r\npeople: List[ScalaObject] = List(JohnSmith$@698fcb66, JaneDoe$@5f72cbae)\r\n\r\nscala&gt; people.map(person =&gt; person.firstName)\r\n&lt;console&gt;:11: error: value firstName is not a member of ScalaObject\r\npeople.map(person =&gt; person.firstName)\r\n                                          ^\r\n<\/pre>\n<p>The only thing that Scala knowns about <strong>JohnSmith<\/strong> and <strong>JaneDoe<\/strong> is that they are <strong>ScalaObjects<\/strong>. That means that a list of such objects can basically just contain them and allow you to move them around as a group. So, something more is needed to make these collections more useful and more general.<\/p>\n<h2>   Classes<\/h2>\n<p>With the list above, what we\u2019d like to have is a <strong>List[Person]<\/strong>, where <strong>Person<\/strong> is a type that has known fields and functions. We can accomplish this by defining a <strong>Person<\/strong> class and then defining John and Jane as members of that class. This also reduces the cut-and-paste duplication problem noted earlier. Here\u2019s what it looks like.<\/p>\n<pre class=\"brush: scala;\">class Person (\r\n  val firstName: String,\r\n  val lastName: String,\r\n  val age: Int,\r\n  val occupation: String\r\n) {\r\n\r\n  def fullName: String = firstName + \" \" + lastName\r\n\r\n  def greet (formal: Boolean): String = {\r\n    if (formal)\r\n      \"Hello, my name is \" + fullName + \". I'm a \" + occupation + \".\"\r\n    else\r\n      \"Hi, I'm \" + firstName + \"!\"\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>The <em>class<\/em> keyword indicates that this is a class definition and <strong>Person<\/strong> is the name of the class. The next part of the definition is a set of parameters to the class that allow us to construct objects that are instances of the class \u2014 in other words, they are placeholders that allow us to use the <strong>Person<\/strong> class as a factory for creating <strong>Person<\/strong> objects. We do this by using the <em>new<\/em> keyword, giving the name of the class and supplying the values for each of the parameters. For example, here\u2019s how we can create John Smith now.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val johnSmith = new Person(\"John\", \"Smith\", 37, \"linguist\")\r\njohnSmith: Person = Person@1979d4fb\r\n<\/pre>\n<p>Just as we could with the one-off standalone <strong>JohnSmith<\/strong> object previously, we can now access the fields and functions.<\/p>\n<pre class=\"brush: scala;\">scala&gt; johnSmith.age\r\nres8: Int = 37\r\n\r\nscala&gt; johnSmith.greet(true)\r\nres9: String = Hello, my name is John Smith. I'm a linguist.\r\n<\/pre>\n<p>Defining other people is now easy, and doesn\u2019t require any cutting-and-pasting.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val janeDoe = new Person(\"Jane\", \"Doe\", 34, \"computer scientist\")\r\njaneDoe: Person = Person@7ff5376c\r\n\r\nscala&gt; val johnDoe = new Person(\"John\", \"Doe\", 43, \"philosopher\")\r\njohnDoe: Person = Person@6544c984\r\n\r\nscala&gt; val johnBrown = new Person(\"John\", \"Brown\", 28, \"mathematician\")\r\njohnBrown: Person = Person@4076a247\r\n<\/pre>\n<p>These <strong>Person<\/strong> objects can now be put into a list together, giving us a <strong>List[Person]<\/strong> that allows mapping to retrieve specific values, like first names and ages, and performing computations like calculating the average age of the individuals in the list.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val people = List(johnSmith, janeDoe, johnDoe, johnBrown)\r\npeople: List[Person] = List(Person@1979d4fb, Person@7ff5376c, Person@6544c984, Person@4076a247)\r\n\r\nscala&gt; people.map(person =&gt; person.firstName)\r\nres10: List[String] = List(John, Jane, John, John)\r\n\r\nscala&gt; people.map(person =&gt; person.age)\r\nres11: List[Int] = List(37, 34, 43, 28)\r\n\r\nscala&gt; people.map(person =&gt; person.age).sum\/people.length.toDouble\r\nres12: Double = 35.5\r\n<\/pre>\n<p>We can sort them according to age.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val ageSortedPeople = people.sortBy(_.age)\r\nageSortedPeople: List[Person] = List(Person@4076a247, Person@7ff5376c, Person@1979d4fb, Person@6544c984)\r\n\r\nscala&gt; ageSortedPeople.map(person =&gt; person.fullName + \":\" + person.age)\r\nres13: List[java.lang.String] = List(John Brown:28, Jane Doe:34, John Smith:37, John Doe:43)\r\n<\/pre>\n<p>We can also group people by first name, last name, etc.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush: scala;\">scala&gt; people.groupBy(person =&gt; person.firstName)\r\nres14: scala.collection.immutable.Map[String,List[Person]] = Map(Jane -&gt; List(Person@7ff5376c), John -&gt; List(Person@1979d4fb, Person@6544c984, Person@4076a247))\r\n\r\nscala&gt; people.groupBy(person =&gt; person.lastName)\r\nres15: scala.collection.immutable.Map[String,List[Person]] = Map(Brown -&gt; List(Person@4076a247), Smith -&gt; List(Person@1979d4fb), Doe -&gt; List(Person@7ff5376c, Person@6544c984))\r\n<\/pre>\n<p>With this, we can have all the Johns greet us.<\/p>\n<pre class=\"brush: scala;\">scala&gt; people.groupBy(person =&gt; person.firstName)(\"John\").foreach(john =&gt; println(john.greet(true)))\r\nHello, my name is John Smith. I'm a linguist.\r\nHello, my name is John Doe. I'm a philosopher.\r\nHello, my name is John Brown. I'm a mathematician.\r\n<\/pre>\n<h2>   Standalone objects<\/h2>\n<p>Above, we saw how to create instances of the <strong>Person<\/strong> class by using the <em>new<\/em> keyword and assigning the resulting object to a variable. We can come back full circle to the first <strong>JohnSmith<\/strong> object we created, which was a standalone <strong>ScalaObject<\/strong>. We can instead create such a standalone object by <em>extending<\/em> the <strong>Person<\/strong> class.<\/p>\n<pre class=\"brush: scala;\">scala&gt; object ThomYorke extends Person(\"Thom\", \"Yorke\", 43, \"musician\")\r\ndefined module ThomYorke\r\n\r\nscala&gt; ThomYorke.greet(true)\r\nres25: String = Hello, my name is Thom Yorke. I'm a musician.\r\n<\/pre>\n<p>By extending the Person class to create the object, we are saying that the object is a kind of <strong>Person<\/strong> \u2014 see more on inheritance below. So, <strong>ThomYorke<\/strong> is a <strong>Person<\/strong> object, like the others we created, but it is for a different use case that we\u2019ll see more of in the next tutorial.&nbsp;For now, I\u2019ll summarize, very roughly, by saying that the <strong>ThomYorke<\/strong> object can be made more accessible by other code that might be using my code, while the <strong>johnSmith<\/strong> and <strong>janeDoe<\/strong> objects are going to be more locally contained.<\/p>\n<h2>   Inheritance<\/h2>\n<p>The standalone objects lead us naturally to the idea of inheritance. In many domains, there are natural hierachies of types, such that properties of a super type are inherited by its subtypes (e.g. fish have gills and swim, so salmon have gills and swim). For example, we could have a <strong>Linguist<\/strong> type that is a kind of <strong>Person<\/strong>, a <strong>ComputerScientist<\/strong> type that is a kind of <strong>Person<\/strong>, and so on. To model this, we create one class that extends another and possibly provides some additional parameters, such as the following definition of a <strong>Linguist<\/strong> sub-type of <strong>Person<\/strong>.<\/p>\n<pre class=\"brush: scala;\">class Linguist (\r\n  firstName: String,\r\n  lastName: String,\r\n  age: Int,\r\n  val speciality: String,\r\n  val favoriteLanguage: String\r\n) extends Person(firstName, lastName, age, \"linguist\") {\r\n\r\n  def workGreeting =\r\n    \"As a \" + occupation + \", I am a \" + speciality + \" who likes to study the language \" + favoriteLanguage + \".\"\r\n\r\n}\r\n<\/pre>\n<p>The <strong>Linguist<\/strong> class has its own parameter list: some of these, like <strong>firstName<\/strong>, <strong>lastName<\/strong>, and <strong>age<\/strong>, are passed on to <strong>Person<\/strong>, and there are new parameter fields <strong>speciality<\/strong> and <strong>favoriteLanguage<\/strong>. The <em>extends<\/em> portion of the definition passes on the relevant parameters needed to construct all the information to make a <strong>Person<\/strong>, and for a <strong>Linguist<\/strong>, it directly sets the occupation parameter to be \u201clinguist\u201d \u2014 thus, we don\u2019t need to provide that when we construct a <strong>Linguist<\/strong>, such as Noam Chomsky.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val noamChomsky = new Linguist(\"Noam\", \"Chomsky\", 83, \"syntactician\", \"English\")noamChomsky: Linguist = Linguist@54c0627f\r\n<\/pre>\n<p>Having defined a <strong>Linguist<\/strong> object in this way, we can ask it to give its work greeting.<\/p>\n<pre class=\"brush: scala;\">scala&gt; noamChomsky.workGreeting\r\nres26: java.lang.String = As a linguist, I am a syntactician who likes to study the language English.\r\n<\/pre>\n<p>We can also access fields and functions of <strong>Person<\/strong> objects, like <strong>age<\/strong> and <strong>greet<\/strong>.<\/p>\n<pre class=\"brush: scala;\">scala&gt; noamChomsky.age\r\nres27: Int = 83\r\n\r\nscala&gt; noamChomsky.greet(true)\r\nres28: String = Hello, my name is Noam Chomsky. I'm a linguist.\r\n<\/pre>\n<p>Of course, the Linguist-specific fields like <strong>favoriteLanguage<\/strong> are accessible too.<\/p>\n<pre class=\"brush: scala;\">scala&gt; noamChomsky.favoriteLanguage\r\nres29: String = English\r\n<\/pre>\n<p>The observant reader will have noticed that some of the parameters are prefaced with <em>val<\/em> and others are not. We\u2019ll get back to that distinction a bit later.<\/p>\n<h2>   Traits<\/h2>\n<p>We could of course now go on to define a <strong>ComputerScientist<\/strong> class that would also have  <strong>workGreeting<\/strong> function, but the <strong>Linguist.workGreeting<\/strong> and <strong>ComputerScientist.workGreeting<\/strong> would be entirely separate. To enable this, we can use traits, which are like classes, but which define an interface of functions and fields that classes can supply concrete values and implementations for.  (Note: traits can also define concrete fields and functions, so they aren\u2019t limited to placeholder functions as we show below.)<\/p>\n<p>As an example, here\u2019s a <strong>Worker<\/strong> trait, which simply defines a function <strong>workGreeting<\/strong> and declares that it must return a <strong>String<\/strong>.<\/p>\n<pre class=\"brush: scala;\">trait Worker {\r\n  def workGreeting: String\r\n}\r\n<\/pre>\n<p>The <strong>Linguist<\/strong> class defined earlier already provides an implementation of that function. To allow a <strong>Linguist<\/strong> to be considered as a type of <strong>Worker<\/strong>, we add <em>with Worker<\/em> after extending <strong>Person<\/strong>.<\/p>\n<pre class=\"brush: scala;\">class Linguist (\r\n  firstName: String,\r\n  lastName: String,\r\n  age: Int,\r\n  val speciality: String,\r\n  val favoriteLanguage: String\r\n) extends Person(firstName, lastName, age, \"linguist\") with Worker {\r\n\r\n  def workGreeting =\r\n    \"As a \" + occupation + \", I am a \" + speciality + \" who likes to study the language \" + favoriteLanguage + \".\"\r\n\r\n}\r\n<\/pre>\n<p>This is called \u201cmixing in\u201d the trait <strong>Worker<\/strong>, because the <strong>Linguist<\/strong> class mixes in the fields and functions of <strong>Worker<\/strong> with those of <strong>Person<\/strong>.<\/p>\n<p>Note that we can also create classes that simply extend a trait like <strong>Worker<\/strong>.<\/p>\n<pre class=\"brush: scala;\">class Student (school: String, subject: String) extends Worker {\r\n  def workGreeting = \"I'm studying \" + subject + \" at \" + school + \"!\"\r\n}\r\n<\/pre>\n<p>We can now create a <strong>Student<\/strong> object and request their greeting.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val anonymousStudent = new Student(\"The University of Texas at Austin\", \"history\")\r\nanonymousStudent: Student = Student@734445b5\r\n\r\nscala&gt; anonymousStudent.workGreeting\r\nres32: java.lang.String = I'm studying history at The University of Texas at Austin!\r\n<\/pre>\n<p>Notice that the parameters school and subject were not preceded by <em>val<\/em> in the definition of <strong>Student<\/strong>. That means that they are not member fields of the <strong>Student<\/strong> class, which means that they cannot be accessed externally. For example, attempting to access the value provided for <strong>school<\/strong> for <strong>anonymousStudent<\/strong> fails.<\/p>\n<pre class=\"brush: scala;\">scala&gt; anonymousStudent.school\r\n&lt;console&gt;:11: error: value school is not a member of Student\r\nanonymousStudent.school\r\n<\/pre>\n<p>Of course, internally, <strong>Student<\/strong> can use the values provided to such parameters, for example in defining the result of <strong>workGreeting<\/strong>. This sort of encapsulation hides properties of the objects of a class from code that is outside the class; this strategy can help reduce the degrees of freedom available to users of your code so that they only use what you want them to. In general, if others don\u2019t need to use it, you shouldn\u2019t make it available to them.<\/p>\n<p>Returning to classes that are both <strong>Persons<\/strong> and <strong>Workers<\/strong>, when we define a <strong>ComputerScientist<\/strong>, we do a similar <em>extends \u2026 with<\/em> declaration as we did for <strong>Linguist<\/strong>.<\/p>\n<pre class=\"brush: scala;\">class ComputerScientist (\r\n  firstName: String,\r\n  lastName: String,\r\n  age: Int,\r\n  val speciality: String,\r\n  favoriteProgrammingLanguage: String\r\n) extends Person(firstName, lastName, age, \"computer scientist\") with Worker {\r\n\r\n  def workGreeting =\r\n    \"As a \" + occupation + \", I work on \" + speciality + \". Much of my code is written in \" + favoriteProgrammingLanguage + \".\"\r\n\r\n}\r\n<\/pre>\n<p>Let\u2019s create <a href=\"http:\/\/www.cs.umass.edu\/~mccallum\/\">Andrew McCallum<\/a> as a <strong>ComputerScientist<\/strong> object.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val andrewMcCallum = new ComputerScientist(\"Andrew\", \"McCallum\", 44, \"machine learning\", \"Scala\")\r\nandrewMcCallum: ComputerScientist = ComputerScientist@493cd5ba\r\n\r\nscala&gt; andrewMcCallum.workGreeting\r\nres31: java.lang.String = As a computer scientist, I work on machine learning. Much of my code is written in Scala.\r\n<\/pre>\n<p>Because we redefined <strong>Linguist<\/strong> to be a <strong>Worker<\/strong>, we need to recreate Noam Chomsky using the new definition. (The creation looks the same as before, but it uses the new class definition that has been updated in the REPL.)<\/p>\n<pre class=\"brush: scala;\">scala&gt; val noamChomsky = new Linguist(\"Noam\", \"Chomsky\", 83, \"syntactician\", \"English\")\r\nnoamChomsky: Linguist = Linguist@6fccaf14\r\n<\/pre>\n<p>A minor thing to note: the <strong>speciality<\/strong> field of <strong>ComputerScientist<\/strong> is disconnected from that of <strong>Linguist<\/strong>, so there is no particular expectation of consistency of use across the two: for <strong>Linguist<\/strong> it is a description of a person working in a sub-area but for <strong>ComputerScientist<\/strong> is a description of a sub-area.<\/p>\n<p>So, what happens if we put noamChomsky and andrewMcCallum in a List together?<\/p>\n<pre class=\"brush: scala;\">scala&gt; val professors = List(noamChomsky, andrewMcCallum)\r\nprofessors: List[Person with Worker] = List(Linguist@6fccaf14, ComputerScientist@493cd5ba)\r\n<\/pre>\n<p>Scala has created a list with type <strong>List[Person with Worker]<\/strong>; this is the most specific type that is valid for all elements of the list. It means we can treat all of the elements as <strong>Persons<\/strong>, e.g. accessing their <strong>occupation<\/strong> (which is a member field of <strong>Person<\/strong>).<\/p>\n<pre class=\"brush: scala;\">scala&gt; professors.map(prof =&gt; prof.occupation)\r\nres34: List[String] = List(linguist, computer scientist)\r\n<\/pre>\n<p>And we can treat each element of the list as a <strong>Person<\/strong> and a <strong>Worker<\/strong>, e.g. printing out their <strong>fullName<\/strong> (from <strong>Person<\/strong>) and their <strong>workGreeting<\/strong> (from <strong>Worker<\/strong>).<\/p>\n<pre class=\"brush: scala;\">scala&gt; professors.foreach(prof =&gt; println(prof.fullName + \": \" + prof.workGreeting))\r\nNoam Chomsky: As a linguist, I am a syntactician who likes to study the language English.\r\nAndrew McCallum: As a computer scientist, I work on machine learning. Much of my code is written in Scala.\r\n<\/pre>\n<p>We cannot, however, access fields and functions that are specific to <strong>Linguists<\/strong> or <strong>ComputerScientists<\/strong>, such as <strong>favoriteLanguage<\/strong> from <strong>Linguist<\/strong>.<\/p>\n<pre class=\"brush: scala;\">scala&gt; professors.map(prof =&gt; prof.favoriteLanguage)\r\n&lt;console&gt;:15: error: value favoriteLanguage is not a member of Person with Worker\r\nprofessors.map(prof =&gt; prof.favoriteLanguage)\r\n<\/pre>\n<p>It is easy to see why Scala has this behavior: even though that would have been valid for <strong>noamChomsky<\/strong>, it would not be for <strong>andrewMcCallum<\/strong> (according to the way we defined <strong>Linguist<\/strong> and <strong>ComputerScientist<\/strong>).<\/p>\n<h2>   Matching on types in polymorphic Lists<\/h2>\n<p>Consider what happens when the <strong>anonymousStudent<\/strong> is in a list with the professors.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val workers = List(noamChomsky, andrewMcCallum, anonymousStudent)\r\nworkers: List[ScalaObject with Worker] = List(Linguist@6fccaf14, ComputerScientist@493cd5ba, Student@734445b5)\r\n<\/pre>\n<p>The <strong>Person<\/strong> type is gone, and we now have a list of a more general type <strong>ScalaObject with Worker<\/strong>. Now we can only use the <strong>workGreeting<\/strong> method from <strong>Worker<\/strong>.<\/p>\n<p>However, it is worth pointing out that <em>match<\/em> statements come in handy when you have collections of heterogenous objects. For example, put the following code into the REPL.<\/p>\n<pre class=\"brush: scala;\">val people = List(johnSmith, noamChomsky, andrewMcCallum, anonymousStudent)\r\n\r\npeople.foreach { person =&gt;\r\n  person match {\r\n    case x: Person with Worker =&gt; println(x.fullName + \": \" + x.workGreeting)\r\n    case x: Person =&gt; println(x.fullName + \": \" + x.greet(true))\r\n    case x: Worker =&gt; println(\"Anonymous:\" + x.workGreeting)\r\n  }\r\n}\r\n<\/pre>\n<p>The result is the following (remember that <strong>johnSmith<\/strong> was never defined as a <strong>Linguist<\/strong> \u2014 he was defined as a <strong>Person<\/strong> whose occupation is \u201clinguist\u201d).<\/p>\n<pre class=\"brush: scala;\">John Smith: Hello, my name is John Smith. I'm a linguist.\r\nNoam Chomsky: As a linguist, I am a syntactician who likes to study the language English.\r\nAndrew McCallum: As a computer scientist, I work on machine learning. Much of my code is written in Scala.\r\nAnonymous:I'm studying history at The University of Texas at Austin!\r\n<\/pre>\n<p>So, we can switch our behavior by matching to more specific types using Scala\u2019s pattern matching.<\/p>\n<h2>   The apply function<\/h2>\n<p>Scala provides a simple but incredibly nice feature: if you define an <strong>apply<\/strong> function in a class or object, you don\u2019t actually need to write \u201capply\u201d in order to use it. As an example, the following object adds one to an argument supplied to its <strong>apply<\/strong> method.<\/p>\n<pre class=\"brush: scala;\">object AddOne {\r\n  def apply (x: Int): Int = x+1\r\n}\r\n<\/pre>\n<p>So, we can use it just like you\u2019d normally expect.<\/p>\n<pre class=\"brush: scala;\">scala&gt; AddOne.apply(3)\r\nres41: Int = 4\r\n<\/pre>\n<p>But, we can also do without the \u201c.apply\u201d portion and get the same result.<\/p>\n<pre class=\"brush: scala;\">scala&gt; AddOne(3)\r\nres42: Int = 4\r\n<\/pre>\n<p>If a class has an <strong>apply<\/strong> method, then we can do the same trick with any object of that class.<\/p>\n<pre class=\"brush: scala;\">class AddN (amountToAdd: Int) {\r\n  def apply (x: Int): Int = x + amountToAdd\r\n}\r\n\r\nscala&gt; val add2 = new AddN(2)\r\nadd2: AddN = AddN@43ca04a1\r\n\r\nscala&gt; add2(5)\r\nres43: Int = 7\r\n\r\nscala&gt; val add42 = new AddN(42)\r\nadd42: AddN = AddN@83e591f\r\n\r\nscala&gt; add42(8)\r\nres44: Int = 50\r\n<\/pre>\n<p>As it turns out, you\u2019ve been using <strong>apply<\/strong> methods quite often, without knowing it! When you have a <strong>List<\/strong> and you access an element by index, you\u2019ve used the <strong>apply<\/strong> method of the <strong>List<\/strong> class.<\/p>\n<pre class=\"brush: scala;\">scala&gt; val numbers = 10 to 20 toList\r\nnumbers: List[Int] = List(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)\r\n\r\nscala&gt; numbers(3)\r\nres46: Int = 13\r\n\r\nscala&gt; numbers.apply(3)\r\nres47: Int = 13\r\n<\/pre>\n<p>Same thing for accessing values using keys in a <strong>Map<\/strong>, and similarly for many other of the classes you\u2019ve been using in Scala so far.<\/p>\n<h2>   Wrap-up<\/h2>\n<p>This tutorial has covered the basics of object-oriented programming in Scala. Hopefully, it is enough to give a decent sense of what objects and classes are and how you can do things with them. There is much much more to be learned about them, but this should be sufficient to get you started so that further study can be done meaningfully. It is important to understand these concepts since Scala is object-oriented from the ground up. In fact, in many of the previous tutorials, I\u2019ve at times gone through some extra hoops to try to describe what is going on without having to talk about object-orientation. But now you can see things like Int, Double, List, Map, and so on for what they are: classes that contain particular fields and functions that you can use to get things done. You can now start coding your own classes to enable your own custom behaviors in your applications.<\/p>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/bcomposes.wordpress.com\/2011\/10\/24\/first-steps-in-scala-for-beginning-programmers-part-9\/\">First steps in Scala for beginning programmers, Part 9<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Jason Baldridge at the <a href=\"http:\/\/bcomposes.wordpress.com\/\">Bcomposes<\/a> blog.<\/p>\n<p><strong><i>Related Articles :<\/i><\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-scala-repl-expressions.html\">Scala Tutorial &#8211; Scala REPL, expressions, variables, basic types, simple functions, saving and running programs, comments<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-tuples-lists-methods-on.html\">Scala Tutorial &#8211; Tuples, Lists, methods on Lists and Strings<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/scala-tutorial-conditional-execution.html\">Scala Tutorial &#8211; conditional execution with if-else blocks and matching<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-iteration-for.html\">Scala Tutorial &#8211; iteration, for expressions, yield, map, filter, count<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-regular-expressions.html\">Scala Tutorial &#8211; regular expressions, matching<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-regular-expressions_05.html\">Scala Tutorial &#8211; regular expressions, matching and substitutions with the scala.util.matching API<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-maps-sets-groupby.html\">Scala Tutorial &#8211; Maps, Sets, groupBy, Options, flatten, flatMap<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scalaiosource-accessing.html\">Scala Tutorial &#8211; scala.io.Source, accessing files, flatMap, mutable Maps<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-scripting-compiling-main.html\">Scala Tutorial &#8211; scripting, compiling, main methods, return values of functions<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-sbt-scalabha-packages.html\">Scala Tutorial &#8211; SBT, scalabha, packages, build systems<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/scala-tutorial-code-blocks-coding-style.html\">Scala Tutorial &#8211; code blocks, coding style, closures, scala documentation project<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/fun-with-function-composition-in-scala.html\">Fun with function composition in Scala<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/08\/how-scala-changed-way-i-think-about-my.html\">How Scala changed the way I think about my Java Code<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/testing-with-scala.html\">Testing with Scala<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/12\/things-every-programmer-should-know.html\">Things Every Programmer Should Know<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Preface This is part 9 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other resources on the links page of the Computational Linguistics course I\u2019m creating these for.&nbsp;Additionally you can find this and other tutorial series on the JCG Java Tutorials &hellip;<\/p>\n","protected":false},"author":67,"featured_media":227,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[235],"class_list":["post-606","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala","tag-scala-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Scala Tutorial - objects, classes, inheritance, traits, Lists with multiple related types, apply - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"PrefaceThis is part 9 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Tutorial - objects, classes, inheritance, traits, Lists with multiple related types, apply - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"PrefaceThis is part 9 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2011-10-25T10:46:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-21T20:24:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jason Baldridge\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@http:\/\/twitter.com\/jasonbaldridge\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jason Baldridge\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html\"},\"author\":{\"name\":\"Jason Baldridge\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/95ef2670a4040b0f7101c48dba2795c0\"},\"headline\":\"Scala Tutorial &#8211; objects, classes, inheritance, traits, Lists with multiple related types, apply\",\"datePublished\":\"2011-10-25T10:46:00+00:00\",\"dateModified\":\"2012-10-21T20:24:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html\"},\"wordCount\":2421,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"keywords\":[\"Scala Tutorial\"],\"articleSection\":[\"Scala\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html\",\"name\":\"Scala Tutorial - objects, classes, inheritance, traits, Lists with multiple related types, apply - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"datePublished\":\"2011-10-25T10:46:00+00:00\",\"dateModified\":\"2012-10-21T20:24:44+00:00\",\"description\":\"PrefaceThis is part 9 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/scala-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/scala-tutorial-objects-classes.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JVM Languages\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/jvm-languages\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Scala\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/jvm-languages\\\/scala\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Scala Tutorial &#8211; objects, classes, inheritance, traits, Lists with multiple related types, apply\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/95ef2670a4040b0f7101c48dba2795c0\",\"name\":\"Jason Baldridge\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g\",\"caption\":\"Jason Baldridge\"},\"sameAs\":[\"http:\\\/\\\/bcomposes.wordpress.com\\\/\",\"http:\\\/\\\/www.linkedin.com\\\/pub\\\/jason-baldridge\\\/5\\\/629\\\/9b2\",\"https:\\\/\\\/x.com\\\/http:\\\/\\\/twitter.com\\\/jasonbaldridge\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/jason-baldridge\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala Tutorial - objects, classes, inheritance, traits, Lists with multiple related types, apply - Java Code Geeks","description":"PrefaceThis is part 9 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html","og_locale":"en_US","og_type":"article","og_title":"Scala Tutorial - objects, classes, inheritance, traits, Lists with multiple related types, apply - Java Code Geeks","og_description":"PrefaceThis is part 9 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other","og_url":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2011-10-25T10:46:00+00:00","article_modified_time":"2012-10-21T20:24:44+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","type":"image\/jpeg"}],"author":"Jason Baldridge","twitter_card":"summary_large_image","twitter_creator":"@http:\/\/twitter.com\/jasonbaldridge","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Jason Baldridge","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html"},"author":{"name":"Jason Baldridge","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/95ef2670a4040b0f7101c48dba2795c0"},"headline":"Scala Tutorial &#8211; objects, classes, inheritance, traits, Lists with multiple related types, apply","datePublished":"2011-10-25T10:46:00+00:00","dateModified":"2012-10-21T20:24:44+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html"},"wordCount":2421,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","keywords":["Scala Tutorial"],"articleSection":["Scala"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html","url":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html","name":"Scala Tutorial - objects, classes, inheritance, traits, Lists with multiple related types, apply - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","datePublished":"2011-10-25T10:46:00+00:00","dateModified":"2012-10-21T20:24:44+00:00","description":"PrefaceThis is part 9 of tutorials for first-time programmers getting into Scala. Other posts are on this blog, and you can get links to those and other","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/scala-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/scala-tutorial-objects-classes.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JVM Languages","item":"https:\/\/www.javacodegeeks.com\/category\/jvm-languages"},{"@type":"ListItem","position":3,"name":"Scala","item":"https:\/\/www.javacodegeeks.com\/category\/jvm-languages\/scala"},{"@type":"ListItem","position":4,"name":"Scala Tutorial &#8211; objects, classes, inheritance, traits, Lists with multiple related types, apply"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/95ef2670a4040b0f7101c48dba2795c0","name":"Jason Baldridge","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b755d282f869512990e0ce9118c71ccd859fad42163f8e5d62d180ea42ea9720?s=96&d=mm&r=g","caption":"Jason Baldridge"},"sameAs":["http:\/\/bcomposes.wordpress.com\/","http:\/\/www.linkedin.com\/pub\/jason-baldridge\/5\/629\/9b2","https:\/\/x.com\/http:\/\/twitter.com\/jasonbaldridge"],"url":"https:\/\/www.javacodegeeks.com\/author\/jason-baldridge"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/606","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=606"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/606\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}