<p>In this article, you will be introduced with the basic OOPs concept i.e. Classes and Objects and how you can create classes and objects in your Kotlin program.</p>
<p>Kotlin supports both functional and object-oriented programming.</p>
<p>Kotlin supports features such as higher-order functions, function types, and lambdas which represent Kotlin as a functional language. We will learn about these concepts in later chapters.</p>
<p>In this article, we will focus on the basic object-oriented programming concept (OOPs) ie. <span style="color: #008000;"><strong>Classes and Objects</strong></span>.</p>
<h3><span style="color: #000080;"><strong>Kotlin Objects</strong></span></h3>
<p><strong><span style="color: #008000;">Object</span></strong> refers to an entity that has state and behavior.</p>
<p><strong><span style="color: #0000ff;">State</span></strong>: represents the data (properties) of an object.<br />
<strong><span style="color: #0000ff;">Behavior</span></strong>: represents the behavior (functionality) of an object.</p>
<p>Let’s take few examples:</p>
<p>1 . car is an object</p>
<p>It has model, color, weight, price as states.<br />
speedup, changing gears, driving, etc. behavior.</p>
<p>2 . account is an object</p>
<p>It has account no, type, balance as states.<br />
deposit, withdrawal, etc, are its behavior.</p>
<h3><span style="color: #000080;"><strong>Kotlin Class</strong></span></h3>
<p>A <span style="color: #008000;"><strong>Class</strong></span> is a way to bind the data (properties) describing an object and its associated functions together.</p>
<p>Classes are needed to represent real-world entities that not only have properties (their characteristics) but also have associated operations (their behavior).</p>
<h4><strong><span style="color: #0000ff;">How to define a class in Kotlin?</span></strong></h4>
<p>To define a class in Kotlin, <strong><span style="color: #008000;">class </span></strong><span style="color: #008000;"><span style="color: #000000;">keyword is used.</span></span></p>
<h4><strong><span style="color: #0000ff;">Syntax:</span></strong></h4>
<p>The syntax of defining a class in Kotlin:</p>
<pre><span style="color: #0000ff;"><strong>class</strong></span> ClassName {
 <span style="color: #008000;"><strong>//property
 //member function</strong></span>
}</pre>
<p>In Kotlin, either the property must be <strong><span style="color: #008000;">initialized</span></strong> or must be declared <strong><span style="color: #008000;">abstract</span></strong>.</p>
<p><span style="color: #008000;"><strong>ClassName</strong></span> should use UpperCamelCase. For example, ClassAndObjectsDemo.</p>
<h4><strong><span style="color: #0000ff;">Example: Kotlin class</span></strong></h4>
<pre>class Account {
 <span style="color: #008000;"><strong>//properties</strong></span>
 private var type: String = ""
 private var accountNo: Int = 0
 private var balance: Float = 0f
 <strong><span style="color: #008000;"> //member functions</span></strong>
 fun deposit(amount: Float) {
 balance = balance + amount
 }

 fun withdraw(amount: Float) {
 balance = balance - amount
 }
}</pre>
<h4><strong><span style="color: #0000ff;">Example explained</span></strong></h4>
<ul>
<li>The data describing an account (i.e., account no, type and balance) and its associated operations (deposit and withdraw) are bound together under one name <span style="color: #0000ff;"><strong>Account</strong></span> (ClassName).</li>
</ul>
<p>The keyword <span style="color: #008000;"><strong>private</strong></span> in the above example, is an access modifier which we will discuss in detail in later chapters. For now, just remember:</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<ul>
<li>The <span style="color: #008000;"><strong>private</strong></span> keyword makes properties and member functions private which can only be accessed inside the same class.</li>
<li>In Kotlin, by default properties and member functions are <strong><span style="color: #008000;">public </span></strong><span style="color: #000000;">(no need to write public keyword)</span> which can be accessed from outside the class.</li>
</ul>
<h3></h3>
<h3><strong><span style="color: #000080;">Creating Objects</span></strong></h3>
<ul>
<li>To access members defined within the class, you need to create objects.</li>
<li>You can create multiple objects of the same class.</li>
</ul>
<h4><strong><span style="color: #0000ff;">Syntax:</span></strong></h4>
<p>The syntax of creating an object of a class:</p>
<pre>var obj = className()</pre>
<h4><strong><span style="color: #0000ff;">Example: Creating objects of a class</span></strong></h4>
<pre>class Account {

 var type: String = ""
 var accountNo: Int = 0
 var balance: Float = 0f

 fun deposit(amount: Float) {
 balance = balance + amount
 }

 fun withdraw(amount: Float) {
 balance = balance - amount
 }
}
<strong><span style="color: #008000;">//account1 and account2 are two objects of class Account</span></strong>
fun main(args: Array<;String>;) {
 <span style="color: #0000ff;"><strong> var account1 = Account()
 var account2 = Account()</strong></span>
}</pre>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>How to access class members?</strong></span></h4>
<p>You can access class members (call functions and access properties) by using the dot(.) operator.</p>
<p>For example, member functions of a class can be accessed as :</p>
<pre>account.deposit()</pre>
<p>This statement calls the <strong><span style="color: #008000;">deposit()</span></strong> method inside the <strong><span style="color: #0000ff;">Account</span></strong> class for an <strong><span style="color: #008000;">account</span></strong> object.</p>
<p>When you call the function using the above statement, all statements within the body of <span style="color: #008000;"><strong>deposit()</strong></span> function are executed. Then, the control of the program jumps back to the statement following <strong><span style="color: #008000;">account.deposit()</span></strong>.</p>
<p>Similarly, the property of a class can be accessed as:</p>
<pre>account.balance</pre>
<p> ;</p>
<h3><span style="color: #000080;"><strong>Example: Kotlin Class and Objects</strong></span></h3>
<pre>class Student {

 <strong><span style="color: #008000;">//properties</span></strong>
 var rollno: Int = 0
 var name: String = ""

 <strong><span style="color: #008000;"> //member functions</span></strong>
 fun insertRecord(roll_no: Int, student_name: String) {
 rollno = roll_no
 name = student_name
 }

 fun displayDetail() {
 println("$rollno $name")
 }
}

 fun main(args: Array<;String>;) {
 <strong><span style="color: #008000;">//s1 and s2 are two objects of Student class</span></strong>
 var s1 = Student()
 var s2 = Student()
 s1.insertRecord(101, "Manish")
 s2.insertRecord(202, "Arun")
 s1.displayDetail()
 s2.displayDetail()
 }</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>101 Manish
202 Arun</pre>
<h4><span style="color: #0000ff;"><strong>Example explained</strong></span></h4>
<p>In the above example,</p>
<ul>
<li><strong><span style="color: #0000ff;">Student</span></strong> class consists of two properties rollno, name and two member functions insertRecord() and displayDetail().</li>
<li>Inside <span style="color: #0000ff;"><strong>main</strong><strong>()</strong></span> function, s1 and s2 are two objects of <span style="color: #008000;"><strong>Student</strong></span> class.</li>
<li>Here, we are initializing the value to these objects by invoking the <strong><span style="color: #008000;">insertRecord()</span></strong> method.</li>
<li>And, we are displaying the state (property) of the objects by invoking the <strong><span style="color: #008000;">displayDetail()</span></strong> method.</li>
</ul>


