
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
Extend data class in Kotlin
Data class is a class that holds the data for an application. It is just like a POJO class that we use in Java in order to hold the data.
In Java, for data class, we need to create getter and setter methods in order to access the properties of that class. In Kotlin, when a class is declared as a data class, the compiler automatically creates some supporting methods required to access the member variable of the class. The compiler will create getters and setters for the constructor parameters, hashCode(), equals(), toString(), copy().
For a class to be considered as a data class in Kotlin, the following conditions are to be fulfilled −
The primary constructor needs to have at least one parameter.
All primary constructor parameters need to be marked as val or var.
Data classes cannot be abstract, open, sealed, or inner.
We cannot extend a data class but in order to implement the same feature, we can declare a super class and override the properties in a sub-class.
Example
In the following example, we will create two data classes "Student" and "Book". We will also create an abstract class "Resource". Inside "Book", we will override the properties of the "Resource" class.
data class Student(val name: String, val age: Int) fun main(args: Array) { val stu = Student("Student1", 29) val stu2 = Student("Student2", 30) println("Student1 Name is: ${stu.name}") println("Student1 Age is: ${stu.age}") println("Student2 Name is: ${stu2.name}") println("Student2 Age is: ${stu2.age}") val b=Book(1L,"India","123222") // implementing abstract class println(b.location) } // declaring super class abstract class Resource { abstract var id: Long abstract var location: String } // override the properties of the Resource class data class Book ( override var id: Long = 0, override var location: String = "", var isbn: String ) : Resource()
Output
It will generate the following output −
Student1 Name is: Student1 Student1 Age is: 29 Student2 Name is: Student2 Student2 Age is: 30 India
- Related Articles
- Override getter for Kotlin data class
- Can Enum extend any class in java?
- Can you extend a static inner class in Java?
- How to extend and implement at the same time in Kotlin?\n
- What happens if we try to extend a final class in java?
- Creating POJO Class for Kotlin
- How to check "instanceof" class in Kotlin?
- How to use the TextWatcher class in kotlin?
- Difference between a "class" and an "object" in Kotlin
- append() and extend() in Python
- append() and extend() in Python program
- How to extend Interfaces in Java
- Python – Extend consecutive tuples
- How to create an instance of an abstract class in Kotlin?
- Implement Runnable vs Extend Thread in Java
