<p>In this article, you will be introduced with the basic OOPs concept i.e. <span style="color: #0000ff;"><strong>Classes and Objects</strong></span> and how you can create classes and objects in your Java program.</p>
<h3><span style="color: #000080;"><strong>Java Objects</strong></span></h3>
<p><span style="color: #008000;"><strong>Object</strong></span> refers to an <span style="display: inline !important; float: none; background-color: #ffffff; color: #2c3338; cursor: text; font-family: 'Noto Serif',Georgia,'Times New Roman',Times,serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: 1.2em; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;">entity that has state and behavior. </span></p>
<ul>
<li><span style="color: #0000ff;"><strong>State</strong></span>: represents the data (properties) of an object.</li>
<li><strong><span style="color: #0000ff;">Behavior</span></strong>: represents the behavior (functionality) of an object.</li>
</ul>
<p>Let&#8217;s take few examples:</p>
<p>1 . <span style="color: #008000;"><strong>car</strong></span> is an object</p>
<ul>
<li>It has model, color, weight, price as states.</li>
<li>speedup, changing gears, driving, etc. behavior.</li>
</ul>
<p>2 . <strong><span style="color: #008000;">account</span></strong> is an object</p>
<ul>
<li>It has account no, type, balance as states.</li>
<li>deposit, withdrawal, etc, are its behavior.</li>
</ul>
<h3></h3>
<h3><span style="color: #000080;"><strong>Java Class</strong></span></h3>
<p>A <strong><span style="color: #008000;">Class</span></strong> is a way to bind the data describing an object and its associated methods 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>
<h3></h3>
<h3><span style="color: #000080;"><strong>How to define a class in Java?</strong></span></h3>
<h4><span style="color: #0000ff;"><strong>Syntax</strong></span></h4>
<pre>class ClassName {
 <strong><span style="color: #008000;">//variables</span></strong>
<strong><span style="color: #008000;"> //methods</span></strong>
}</pre>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre> class Account {
 private String type;
 private int accountno;
 private float balance;

 public void deposit(float amount) {
 balance = balance + amount; 
 }

 public void withdraw(float amount) {
 balance = balance - amount;
 } 
}</pre>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Example explained</strong></span></h4>
<ul>
<li>The data describing an account (i.e., <span style="color: #008000;"><strong>account no, type and balance</strong></span>) and its associated operations (<strong><span style="color: #008000;">deposit and withdraw</span></strong>) are bound together under one name <span style="color: #0000ff;"><strong>Account </strong><span style="color: #000000;">(ClassName)</span></span>.</li>
<li>These variables and methods defined within a class are called <strong><span style="color: #008000;">members</span> </strong>of the class.</li>
</ul>
<p>Notice two keywords <span style="color: #008000;"><strong>private</strong></span> and <strong><span style="color: #008000;">public</span></strong> in above program. These are access modifiers which will be discussed in detail in later chapters. For now, just remember:</p>
<ul>
<li>The <span style="color: #0000ff;"><strong>private</strong></span> keyword makes instance variables and methods private which can only be accessed inside the same class.</li>
<li>The <strong><span style="color: #0000ff;">public</span></strong> keyword makes instance variables and methods public which can be accessed from outside the class.</li>
</ul>
<h3></h3>
<h3><span style="color: #000080;"><strong>Creating Objects</strong></span></h3>
<p>To access members defined within the class, you need to create objects.</p>
<ul>
<li>The objects of a class are created by using the <strong><span style="color: #008000;">&#8220;new&#8221; </span></strong><span style="color: #000000;">keyword</span>. <strong><span style="color: #008000;"> </span></strong></li>
<li><strong><span style="color: #008000;">new</span></strong> allocates memory for the object.</li>
<li>You can create multiple objects of the same class.</li>
</ul>
<h4></h4>
<h4><span style="color: #0000ff;"><strong>Syntax</strong></span></h4>
<p><img class=" wp-image-1764 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/04/obj_new_img-660436683-1585813944555.png" alt="obj_new_img" width="436" height="151" /></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>

<p> ;</p>
<p><span style="color: #008000;"><span style="color: #000000;">Here,</span><strong> Constructor</strong> </span>is a new term, we will discuss in detail in later chapters. For now, just remember:</p>
<ul>
<li>A Constructor is similar to a method(but not actually a method) with the same name as that of its class and it is used to initialize the objects with a legal initial value.</li>
</ul>
<h4><strong><span style="color: #0000ff;">Example</span></strong></h4>
<pre>class Account {

 private char type;
 private int accountno;
 private float balance;

 public void deposit(float amount) {
 balance = balance + amount;
 }

 public void withdraw(float amount) {
 balance = balance - amount;
 }
}

public class ClassMain {
 public static void main(String[] args) {

<strong><span style="color: #0000ff;"> //account1 and account2 are the objects of Account class</span></strong>
 <span style="color: #008000;"><strong>Account account1 = new Account();</strong></span>
<span style="color: #008000;"><strong> Account account2 = new Account();</strong></span>
 }
}</pre>
<h3></h3>
<h3></h3>
<h3><span style="color: #000080;"><strong>How to access members?</strong></span></h3>
<p>You can access members (call methods and access instance variables) by using the dot(<strong>.</strong>) operator.</p>
<p>For example, methods can be accessed as :</p>
<pre>account.deposit();</pre>
<p>This statement calls<span style="color: #008000;"><span style="color: #000000;"> the</span><strong> deposit()</strong></span> method inside the <strong><span style="color: #008000;">Account</span> </strong>class for an <strong><span style="color: #008000;">account</span> </strong>object.</p>
<p>When you call the method using the above statement, all statements within the body of <span style="color: #008000;"><strong>deposit()</strong></span> method 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 instance variable can be accessed as:</p>
<pre>account.balance;</pre>
<h3></h3>
<h3><span style="color: #000080;"><strong>Example: Java Class and Objects</strong></span></h3>
<pre>class Student {
<span style="color: #008000;"><strong> //instance variables</strong></span>
 int rollno;
 String name;
<strong><span style="color: #008000;"> //methods</span></strong>
 public void insertRecord(int roll_no, String student_name) {
 rollno = roll_no;
 name = student_name;
 }

 public void displayDetail() {
 System.out.println(rollno + " " + name);
 }
}

public class ClassMain {
 public static void main(String args[]) {

<span style="color: #008000;"><strong> //s1 and s2 are two objects of Student class</strong></span>
 <strong> Student s1 = new Student();
 Student s2 = new Student();</strong>
 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><strong><span style="color: #0000ff;">Example explained</span></strong></h4>
<p>In the above program,</p>
<ul>
<li><span style="color: #0000ff;"><strong>Student</strong></span> class consists of two instance variables <span style="color: #008000;"><strong>rollno, name</strong></span> and two methods <strong><span style="color: #008000;">insertRecord() </span></strong><span style="color: #008000;"><span style="color: #000000;">and</span></span><strong><span style="color: #008000;"> displayDetail()</span></strong>.</li>
<li>Inside main() method, <strong><span style="color: #0000ff;">s1</span></strong> and <strong><span style="color: #0000ff;">s2</span></strong> 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<span style="color: #008000;"><strong> insertRecord()</strong> </span>method.</li>
<li>And, we are displaying the state (data) of the objects by invoking the <strong><span style="color: #008000;">displayDetail()</span></strong> method.</li>
</ul>
<p>We have mentioned the term <span style="color: #008000;"><strong>method</strong></span> quite a few times. In the next chapter, you will learn about Java methods in detail.</p>
<p> ;</p>
<p> ;</p>
<p> ;</p>


