{"id":37045,"date":"2024-11-01T09:54:21","date_gmt":"2024-11-01T09:54:21","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37045"},"modified":"2024-11-01T11:42:28","modified_gmt":"2024-11-01T11:42:28","slug":"course-on-kotlin-android-app-development-kotlin-inheritance-for-reusing-classes","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37045\/","title":{"rendered":"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes"},"content":{"rendered":"<p><body><\/p>\n<p>\n        Efficiency and code reusability are very important factors in Android app development. Kotlin is a modern programming language designed to maximize these aspects, particularly offering excellent features for code reuse through inheritance. In this article, we will take a deep dive into the concept of inheritance in Kotlin and explain how it can be utilized in actual Android app development with example code.\n    <\/p>\n<h2>Overview of Inheritance<\/h2>\n<p>\n        Inheritance is a feature in object-oriented programming that allows one class to inherit properties and methods from another class. This reduces code duplication and creates a more flexible structure. In Kotlin, when defining a class, it is by default declared as &#8216;final&#8217;, meaning it cannot be inherited. Therefore, if you want to allow inheritance, you need to use the keyword &#8216;open&#8217; in front of the class declaration.\n    <\/p>\n<h2>Basic Class Inheritance Example<\/h2>\n<pre><code class=\"language-kotlin\">\nopen class Animal(val name: String) {\n    fun sound() {\n        println(\"$name makes a sound.\")\n    }\n}\n\nclass Dog(name: String) : Animal(name) {\n    fun bark() {\n        println(\"$name barks.\")\n    }\n}\n    <\/code><\/pre>\n<p>\n        In the above example, we defined a base class called <code>Animal<\/code>. This class contains a property called <code>name<\/code> and a method called <code>sound<\/code>. Then, we defined a class called <code>Dog<\/code> that inherits from the <code>Animal<\/code> class and defines an additional method called <code>bark<\/code>. The subclass <code>Dog<\/code> can use the properties and methods of the superclass <code>Animal<\/code>.\n    <\/p>\n<h2>Utilizing Class Inheritance in Android Apps<\/h2>\n<p>\n        In Android app development, inheritance is often used for the reuse of screens (Activity) or Views. For instance, you can create a base class for Activities that have common UI behaviors or data processing, and multiple Activities can inherit from it.\n    <\/p>\n<h3>Creating a Base Activity Class<\/h3>\n<pre><code class=\"language-kotlin\">\nopen class BaseActivity : AppCompatActivity() {\n    fun showToast(message: String) {\n        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()\n    }\n}\n\nclass MainActivity : BaseActivity() {\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n        showToast(\"Welcome to MainActivity\")\n    }\n}\n\nclass SecondActivity : BaseActivity() {\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_second)\n        showToast(\"Welcome to SecondActivity\")\n    }\n}\n    <\/code><\/pre>\n<p>\n        Here, we created a base Activity class called <code>BaseActivity<\/code>. This class features a method called <code>showToast<\/code> that displays a Toast message. The <code>MainActivity<\/code> and <code>SecondActivity<\/code> classes inherit from <code>BaseActivity<\/code> and can easily utilize this functionality in their respective screens.\n    <\/p>\n<h2>Polymorphism and Inheritance<\/h2>\n<p>\n        The concept of polymorphism is also important in relation to inheritance. Polymorphism is the ability to process different objects using the same interface. In Kotlin, you can handle a subclass object using a superclass type. This increases the flexibility of the code.\n    <\/p>\n<h3>Polymorphism Example<\/h3>\n<pre><code class=\"language-kotlin\">\nfun makeSound(animal: Animal) {\n    animal.sound()\n}\n\nval dog = Dog(\"Buddy\")\nmakeSound(dog) \/\/ Outputs: Buddy makes a sound.\n    <\/code><\/pre>\n<p>\n        The <code>makeSound<\/code> function takes an object of type <code>Animal<\/code> as a parameter and calls the <code>sound<\/code> method of that object. When you pass a <code>Dog<\/code> object, it calls the <code>sound<\/code> method of <code>Animal<\/code>, but can also utilize the overridden method in the subclass, thereby leveraging polymorphism.\n    <\/p>\n<h2>Overriding in Inheritance<\/h2>\n<p>\n        During inheritance, a subclass can override (redefine) methods of the superclass. This allows the subclass to modify the default behavior of the superclass. Overriding is implemented using the <code>override<\/code> keyword.\n    <\/p>\n<h3>Overriding Example<\/h3>\n<pre><code class=\"language-kotlin\">\nopen class Vehicle {\n    open fun start() {\n        println(\"Vehicle is starting\")\n    }\n}\n\nclass Car : Vehicle() {\n    override fun start() {\n        println(\"Car is starting\")\n    }\n}\n    <\/code><\/pre>\n<p>\n        In the example above, we defined a method called <code>start<\/code> in a base class called <code>Vehicle<\/code>, and we overridden it in the <code>Car<\/code> class to match the behavior for cars.\n    <\/p>\n<h2>Inheritance and Interfaces<\/h2>\n<p>\n        In Kotlin, you can enhance code reusability through interfaces in conjunction with class inheritance. An interface provides a blueprint of methods that a class must implement and allows for multiple inheritance.\n    <\/p>\n<h3>Interface Usage Example<\/h3>\n<pre><code class=\"language-kotlin\">\ninterface Drivable {\n    fun drive()\n}\n\nclass Motorcycle : Drivable {\n    override fun drive() {\n        println(\"Motorcycle is driving\")\n    }\n}\n    <\/code><\/pre>\n<p>\n        We defined the <code>Drivable<\/code> interface, which is implemented by the <code>Motorcycle<\/code> class. By using interfaces, multiple classes can provide the same functionality while implementing it in different ways.\n    <\/p>\n<h2>Code Reusability and Maintenance<\/h2>\n<p>\n        Code generated through inheritance tends to be more reusable and easier to maintain. By reducing duplicate code, the likelihood of bugs decreases, and when changes occur, only the functionality of the superclass needs to be modified. However, indiscriminate use of inheritance can reduce code readability, so it&#8217;s important to use it appropriately.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        In this article, we explained the concept and application of inheritance for reusing classes in Android app development using Kotlin. Inheritance reduces code duplication and allows for flexible coding through polymorphism. Effectively utilizing Kotlin&#8217;s inheritance can help create more efficient and maintainable Android applications.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Efficiency and code reusability are very important factors in Android app development. Kotlin is a modern programming language designed to maximize these aspects, particularly offering excellent features for code reuse through inheritance. In this article, we will take a deep dive into the concept of inheritance in Kotlin and explain how it can be utilized &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37045\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[143],"tags":[],"class_list":["post-37045","post","type-post","status-publish","format-standard","hentry","category-kotlin-android-app-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/37045\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Efficiency and code reusability are very important factors in Android app development. Kotlin is a modern programming language designed to maximize these aspects, particularly offering excellent features for code reuse through inheritance. In this article, we will take a deep dive into the concept of inheritance in Kotlin and explain how it can be utilized &hellip; \ub354 \ubcf4\uae30 &quot;course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37045\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:54:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:42:28+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/37045\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37045\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes\",\"datePublished\":\"2024-11-01T09:54:21+00:00\",\"dateModified\":\"2024-11-01T11:42:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37045\/\"},\"wordCount\":596,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin Android app development\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37045\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37045\/\",\"name\":\"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:54:21+00:00\",\"dateModified\":\"2024-11-01T11:42:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37045\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37045\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37045\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/37045\/","og_locale":"ko_KR","og_type":"article","og_title":"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Efficiency and code reusability are very important factors in Android app development. Kotlin is a modern programming language designed to maximize these aspects, particularly offering excellent features for code reuse through inheritance. In this article, we will take a deep dive into the concept of inheritance in Kotlin and explain how it can be utilized &hellip; \ub354 \ubcf4\uae30 \"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes\"","og_url":"https:\/\/atmokpo.com\/w\/37045\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:54:21+00:00","article_modified_time":"2024-11-01T11:42:28+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/37045\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37045\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes","datePublished":"2024-11-01T09:54:21+00:00","dateModified":"2024-11-01T11:42:28+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37045\/"},"wordCount":596,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin Android app development"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37045\/","url":"https:\/\/atmokpo.com\/w\/37045\/","name":"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:54:21+00:00","dateModified":"2024-11-01T11:42:28+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37045\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37045\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37045\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"course on Kotlin Android App Development, Kotlin, Inheritance for Reusing Classes"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=37045"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37045\/revisions"}],"predecessor-version":[{"id":37046,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37045\/revisions\/37046"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}