Site icon C1CTech

Java Class and Objects

<p>In this article&comma; you will be introduced with the basic OOPs concept i&period;e&period; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Classes and Objects<&sol;strong><&sol;span> and how you can create classes and objects in your Java program&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java Objects<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Object<&sol;strong><&sol;span> refers to an <span style&equals;"display&colon; inline &excl;important&semi; float&colon; none&semi; background-color&colon; &num;ffffff&semi; color&colon; &num;2c3338&semi; cursor&colon; text&semi; font-family&colon; 'Noto Serif'&comma;Georgia&comma;'Times New Roman'&comma;Times&comma;serif&semi; font-size&colon; 16px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; 400&semi; letter-spacing&colon; normal&semi; line-height&colon; 1&period;2em&semi; orphans&colon; 2&semi; text-align&colon; left&semi; text-decoration&colon; none&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; -webkit-text-stroke-width&colon; 0px&semi; white-space&colon; normal&semi; word-spacing&colon; 0px&semi;">entity that has state and behavior&period; <&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>State<&sol;strong><&sol;span>&colon; represents the data &lpar;properties&rpar; of an object&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Behavior<&sol;span><&sol;strong>&colon; represents the behavior &lpar;functionality&rpar; of an object&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Let&&num;8217&semi;s take few examples&colon;<&sol;p>&NewLine;<p>1 &period; <span style&equals;"color&colon; &num;008000&semi;"><strong>car<&sol;strong><&sol;span> is an object<&sol;p>&NewLine;<ul>&NewLine;<li>It has model&comma; color&comma; weight&comma; price as states&period;<&sol;li>&NewLine;<li>speedup&comma; changing gears&comma; driving&comma; etc&period; behavior&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>2 &period; <strong><span style&equals;"color&colon; &num;008000&semi;">account<&sol;span><&sol;strong> is an object<&sol;p>&NewLine;<ul>&NewLine;<li>It has account no&comma; type&comma; balance as states&period;<&sol;li>&NewLine;<li>deposit&comma; withdrawal&comma; etc&comma; are its behavior&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java Class<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>A <strong><span style&equals;"color&colon; &num;008000&semi;">Class<&sol;span><&sol;strong> is a way to bind the data describing an object and its associated methods together&period;<&sol;p>&NewLine;<p>Classes are needed to represent real-world entities that not only have properties &lpar;their characteristics&rpar; but also have associated operations &lpar;their behavior&rpar;&period;<&sol;p>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>How to define a class in Java&quest;<&sol;strong><&sol;span><&sol;h3>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>class ClassName &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;variables<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;methods<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre> class Account &lbrace;&NewLine; private String type&semi;&NewLine; private int accountno&semi;&NewLine; private float balance&semi;&NewLine;&NewLine; public void deposit&lpar;float amount&rpar; &lbrace;&NewLine; balance &equals; balance &plus; amount&semi; &NewLine; &rcub;&NewLine;&NewLine; public void withdraw&lpar;float amount&rpar; &lbrace;&NewLine; balance &equals; balance - amount&semi;&NewLine; &rcub; &NewLine;&rcub;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example explained<&sol;strong><&sol;span><&sol;h4>&NewLine;<ul>&NewLine;<li>The data describing an account &lpar;i&period;e&period;&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>account no&comma; type and balance<&sol;strong><&sol;span>&rpar; and its associated operations &lpar;<strong><span style&equals;"color&colon; &num;008000&semi;">deposit and withdraw<&sol;span><&sol;strong>&rpar; are bound together under one name <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Account <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">&lpar;ClassName&rpar;<&sol;span><&sol;span>&period;<&sol;li>&NewLine;<li>These variables and methods defined within a class are called <strong><span style&equals;"color&colon; &num;008000&semi;">members<&sol;span> <&sol;strong>of the class&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Notice two keywords <span style&equals;"color&colon; &num;008000&semi;"><strong>private<&sol;strong><&sol;span> and <strong><span style&equals;"color&colon; &num;008000&semi;">public<&sol;span><&sol;strong> in above program&period; These are access modifiers which will be discussed in detail in later chapters&period; For now&comma; just remember&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>private<&sol;strong><&sol;span> keyword makes instance variables and methods private which can only be accessed inside the same class&period;<&sol;li>&NewLine;<li>The <strong><span style&equals;"color&colon; &num;0000ff&semi;">public<&sol;span><&sol;strong> keyword makes instance variables and methods public which can be accessed from outside the class&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Objects<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>To access members defined within the class&comma; you need to create objects&period;<&sol;p>&NewLine;<ul>&NewLine;<li>The objects of a class are created by using the <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;new&&num;8221&semi; <&sol;span><&sol;strong><span style&equals;"color&colon; &num;000000&semi;">keyword<&sol;span>&period; <strong><span style&equals;"color&colon; &num;008000&semi;"> <&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">new<&sol;span><&sol;strong> allocates memory for the object&period;<&sol;li>&NewLine;<li>You can create multiple objects of the same class&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Syntax<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><img class&equals;" wp-image-1764 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;04&sol;obj&lowbar;new&lowbar;img-660436683-1585813944555&period;png" alt&equals;"obj&lowbar;new&lowbar;img" width&equals;"436" height&equals;"151" &sol;><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;">Here&comma;<&sol;span><strong> Constructor<&sol;strong> <&sol;span>is a new term&comma; we will discuss in detail in later chapters&period; For now&comma; just remember&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>A Constructor is similar to a method&lpar;but not actually a method&rpar; with the same name as that of its class and it is used to initialize the objects with a legal initial value&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>class Account &lbrace;&NewLine;&NewLine; private char type&semi;&NewLine; private int accountno&semi;&NewLine; private float balance&semi;&NewLine;&NewLine; public void deposit&lpar;float amount&rpar; &lbrace;&NewLine; balance &equals; balance &plus; amount&semi;&NewLine; &rcub;&NewLine;&NewLine; public void withdraw&lpar;float amount&rpar; &lbrace;&NewLine; balance &equals; balance - amount&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;public class ClassMain &lbrace;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol;account1 and account2 are the objects of Account class<&sol;span><&sol;strong>&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>Account account1 &equals; new Account&lpar;&rpar;&semi;<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> Account account2 &equals; new Account&lpar;&rpar;&semi;<&sol;strong><&sol;span>&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>How to access members&quest;<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>You can access members &lpar;call methods and access instance variables&rpar; by using the dot&lpar;<strong>&period;<&sol;strong>&rpar; operator&period;<&sol;p>&NewLine;<p>For example&comma; methods can be accessed as &colon;<&sol;p>&NewLine;<pre>account&period;deposit&lpar;&rpar;&semi;<&sol;pre>&NewLine;<p>This statement calls<span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;"> the<&sol;span><strong> deposit&lpar;&rpar;<&sol;strong><&sol;span> method inside the <strong><span style&equals;"color&colon; &num;008000&semi;">Account<&sol;span> <&sol;strong>class for an <strong><span style&equals;"color&colon; &num;008000&semi;">account<&sol;span> <&sol;strong>object&period;<&sol;p>&NewLine;<p>When you call the method using the above statement&comma; all statements within the body of <span style&equals;"color&colon; &num;008000&semi;"><strong>deposit&lpar;&rpar;<&sol;strong><&sol;span> method are executed&period; Then&comma; the control of the program jumps back to the statement following <strong><span style&equals;"color&colon; &num;008000&semi;">account&period;deposit&lpar;&rpar;<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>Similarly&comma; the instance variable can be accessed as&colon;<&sol;p>&NewLine;<pre>account&period;balance&semi;<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Example&colon; Java Class and Objects<&sol;strong><&sol;span><&sol;h3>&NewLine;<pre>class Student &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;instance variables<&sol;strong><&sol;span>&NewLine; int rollno&semi;&NewLine; String name&semi;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;methods<&sol;span><&sol;strong>&NewLine; public void insertRecord&lpar;int roll&lowbar;no&comma; String student&lowbar;name&rpar; &lbrace;&NewLine; rollno &equals; roll&lowbar;no&semi;&NewLine; name &equals; student&lowbar;name&semi;&NewLine; &rcub;&NewLine;&NewLine; public void displayDetail&lpar;&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;rollno &plus; " " &plus; name&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;public class ClassMain &lbrace;&NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar; &lbrace;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;s1 and s2 are two objects of Student class<&sol;strong><&sol;span>&NewLine; <strong> Student s1 &equals; new Student&lpar;&rpar;&semi;&NewLine; Student s2 &equals; new Student&lpar;&rpar;&semi;<&sol;strong>&NewLine; s1&period;insertRecord&lpar;101&comma; "Manish"&rpar;&semi;&NewLine; s2&period;insertRecord&lpar;202&comma; "Arun"&rpar;&semi;&NewLine; s1&period;displayDetail&lpar;&rpar;&semi;&NewLine; s2&period;displayDetail&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>101 Manish&NewLine;202 Arun<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>In the above program&comma;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Student<&sol;strong><&sol;span> class consists of two instance variables <span style&equals;"color&colon; &num;008000&semi;"><strong>rollno&comma; name<&sol;strong><&sol;span> and two methods <strong><span style&equals;"color&colon; &num;008000&semi;">insertRecord&lpar;&rpar; <&sol;span><&sol;strong><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;">and<&sol;span><&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;"> displayDetail&lpar;&rpar;<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li>Inside main&lpar;&rpar; method&comma; <strong><span style&equals;"color&colon; &num;0000ff&semi;">s1<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;0000ff&semi;">s2<&sol;span><&sol;strong> are two objects of <span style&equals;"color&colon; &num;008000&semi;"><strong>Student<&sol;strong><&sol;span> class&period;<&sol;li>&NewLine;<li>Here&comma; we are initializing the value to these objects by invoking the<span style&equals;"color&colon; &num;008000&semi;"><strong> insertRecord&lpar;&rpar;<&sol;strong> <&sol;span>method&period;<&sol;li>&NewLine;<li>And&comma; we are displaying the state &lpar;data&rpar; of the objects by invoking the <strong><span style&equals;"color&colon; &num;008000&semi;">displayDetail&lpar;&rpar;<&sol;span><&sol;strong> method&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>We have mentioned the term <span style&equals;"color&colon; &num;008000&semi;"><strong>method<&sol;strong><&sol;span> quite a few times&period; In the next chapter&comma; you will learn about Java methods in detail&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version