{"id":6204,"date":"2021-02-18T06:28:44","date_gmt":"2021-02-18T06:28:44","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=6204"},"modified":"2024-04-12T10:15:13","modified_gmt":"2024-04-12T10:15:13","slug":"generic-types-in-scala-generic-classes-tour-of-scala-prwatech","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/","title":{"rendered":"Scala &#8211; Generic Types"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Generic Classes | Tour of Scala&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">Generic Classes | Tour of Scala<\/span><\/h2>\r\n<div class=\"flex-1 overflow-hidden\">\r\n<div class=\"react-scroll-to-bottom--css-kloqy-79elbk h-full\">\r\n<div class=\"react-scroll-to-bottom--css-kloqy-1n7m0yu\">\r\n<div class=\"flex flex-col text-sm pb-9\">\r\n<div class=\"w-full text-token-text-primary\" dir=\"auto\" data-testid=\"conversation-turn-166\">\r\n<div class=\"px-4 py-2 justify-center text-base md:gap-6 m-auto\">\r\n<div class=\"flex flex-1 text-base mx-auto gap-3 juice:gap-4 juice:md:gap-6 md:px-5 lg:px-1 xl:px-5 md:max-w-3xl lg:max-w-[40rem] xl:max-w-[48rem] group final-completion\">\r\n<div class=\"relative flex w-full flex-col agent-turn\">\r\n<div class=\"flex-col gap-1 md:gap-3\">\r\n<div class=\"flex flex-grow flex-col max-w-full\">\r\n<div class=\"min-h-[20px] text-message flex flex-col items-start gap-3 whitespace-pre-wrap break-words [.text-message+&amp;]:mt-5 overflow-x-auto\" dir=\"auto\" data-message-author-role=\"assistant\" data-message-id=\"bd9b9ba3-9716-457e-8ab5-17bcd276c93c\">\r\n<div class=\"markdown prose w-full break-words dark:prose-invert light\">\r\n<p>Generic classes in Scala provide a powerful mechanism for writing reusable and type-safe code by allowing classes to work with different types without sacrificing type safety. A generic class is parameterize with one or more type parameters, which represent placeholder types that can be specified when creating instances of the class.<\/p>\r\n<p>By using generic classes, developers can write flexible and versatile code that can operate on a wide range of data types while enforcing type constraints at compile time.<\/p>\r\n<p>In Scala, generic classes provide a powerful mechanism for writing reusable and type-safe code by allowing classes to work with different types without sacrificing type safety.\u00a0<\/p>\r\n<p>For example, a generic class <code>Box[T]<\/code> can be define to hold values of any type <code>T<\/code>. The type parameter <code>T<\/code> can be specified when creating instances of <code>Box<\/code>, such as <code>Box[Int]<\/code> or <code>Box[String]<\/code>, allowing the same class implementation to work with different types of data.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>This enables the creation of container-like data structures (e.g., lists, stacks, queues) that are independent of the specific types of elements they hold.<\/p>\r\n<p>In Scala, generic classes are declared using square brackets (<code>[]<\/code>) to specify type parameters after the class name.<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<button class=\"cursor-pointer absolute z-10 rounded-full bg-clip-padding border text-token-text-secondary border-token-border-light right-1\/2 bg-token-main-surface-primary bottom-5\"><\/button><\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<h5><strong>For a Specific Type<\/strong><\/h5>\r\n<div class=\"w-full text-token-text-primary\" dir=\"auto\" data-testid=\"conversation-turn-166\">\r\n<div class=\"px-4 py-2 justify-center text-base md:gap-6 m-auto\">\r\n<div class=\"flex flex-1 text-base mx-auto gap-3 juice:gap-4 juice:md:gap-6 md:px-5 lg:px-1 xl:px-5 md:max-w-3xl lg:max-w-[40rem] xl:max-w-[48rem] group final-completion\">\r\n<div class=\"relative flex w-full flex-col agent-turn\">\r\n<div class=\"flex-col gap-1 md:gap-3\">\r\n<div class=\"flex flex-grow flex-col max-w-full\">\r\n<div class=\"min-h-[20px] text-message flex flex-col items-start gap-3 whitespace-pre-wrap break-words [.text-message+&amp;]:mt-5 overflow-x-auto\" dir=\"auto\" data-message-author-role=\"assistant\" data-message-id=\"bd9b9ba3-9716-457e-8ab5-17bcd276c93c\">\r\n<div class=\"markdown prose w-full break-words dark:prose-invert light\">\u00a0<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"598\" height=\"129\" class=\"wp-image-6205\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514.png 598w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514-300x65.png 300w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Now the compiler guarantees that the <a href=\"https:\/\/prwatech.in\/blog\/#\">course<\/a> item in <a href=\"https:\/\/www.scala-lang.org\/\">someOffer<\/a> can only be an\u00a0Prwatech\u00a0instance.<\/p>\r\n\r\n\r\n\r\n<p><strong>Without Type-Safety<\/strong><\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"598\" height=\"95\" class=\"wp-image-6206\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-517.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-517.png 598w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-517-300x48.png 300w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<p><strong>Type safety with Generic Classes<\/strong><\/p>\r\n\r\n\r\n\r\n<p>When declaring a class in Scala, we can specify type parameters. We use square brackets to surround these type parameters.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"118\" class=\"wp-image-6208\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-519.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-519.png 593w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-519-300x60.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Generic Classes | Tour of Scala<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Generic Classes | Tour of Scala Generic classes in Scala provide a powerful mechanism for writing reusable and type-safe code by allowing classes to work with different types without sacrificing type safety. A generic class is parameterize with one or more type parameters, which represent placeholder types that can be specified when creating instances of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[1999,2001,2000,1998],"class_list":["post-6204","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-basics-of-generics-in-scala","tag-generic-classes-tour-of-scala","tag-generic-classes-in-scala","tag-generics-scala-3-book"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Generic Classes | Tour of Scala - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Generic Classes | Tour of Scala - Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Generic Classes | Tour of Scala - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Generic Classes | Tour of Scala - Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-18T06:28:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T10:15:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514.png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/\",\"name\":\"Generic Classes | Tour of Scala - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514.png\",\"datePublished\":\"2021-02-18T06:28:44+00:00\",\"dateModified\":\"2024-04-12T10:15:13+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Generic Classes | Tour of Scala - Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514.png\",\"width\":598,\"height\":129},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Generic Types\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Generic Classes | Tour of Scala - Prwatech","description":"Master Generic Classes | Tour of Scala - Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Generic Classes | Tour of Scala - Prwatech","og_description":"Master Generic Classes | Tour of Scala - Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-02-18T06:28:44+00:00","article_modified_time":"2024-04-12T10:15:13+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514.png","type":"","width":"","height":""}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/","name":"Generic Classes | Tour of Scala - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514.png","datePublished":"2021-02-18T06:28:44+00:00","dateModified":"2024-04-12T10:15:13+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Generic Classes | Tour of Scala - Tour of Scala - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-514.png","width":598,"height":129},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/generic-types-in-scala-generic-classes-tour-of-scala-prwatech\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Generic Types"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/6204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=6204"}],"version-history":[{"count":8,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/6204\/revisions"}],"predecessor-version":[{"id":11378,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/6204\/revisions\/11378"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=6204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=6204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=6204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}