{"id":369,"date":"2016-01-29T08:02:13","date_gmt":"2016-01-29T08:02:13","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=369"},"modified":"2016-02-25T06:22:26","modified_gmt":"2016-02-25T06:22:26","slug":"class-objects","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/class-objects","title":{"rendered":"Class And Object In JAVA With Examples &#8211; Tutorial"},"content":{"rendered":"<p>You might have already heard many times JAVA is an Object Oriented Programming which simply means coding in JAVA constantly involve classes and objects. In other words coding in JAVA is not possible without object and class. Even the smallest Hello world program requires declaration of class and method work on object. So let\u2019s understand these two concepts which are really very important in JAVA.<\/p>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Definition of Class and Object:<\/strong><\/span><\/h4>\n<p><span style=\"text-decoration: underline; color: #008000;\"><strong>Class:<\/strong><\/span> The concept of class comes into role when we see certain type of objects or things around us and the common idea or a blueprint behind this type of objects is called Class.\u00a0In other words class is a properties behind each of the objects or things possess.<\/p>\n<p><strong>For example: <\/strong>Consider you have iPhone, Samsung and Sony devices and you want to represent them in JAVA. To do that you first need to find out what can be the blueprint behind these devices. And here blueprint can be a Mobile because they all are a type of Mobile. So Mobile is a class which can represent iPhone, Samsung and Sony devices here.<\/p>\n<p><span style=\"color: #008000;\"><strong>How To Declare Class in JAVA:<\/strong><\/span><\/p>\n<pre>class Mobile{\r\n\/**ToDo Code Here*\/\r\n}<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Class is a representation of similar types of objects or it is a implementation of encapsulation.<\/p>\n<ul>\n<li>In terms of java, a class is type declaration means when you want to define a specific type of data for special use then it can be easily obtained with the help of class.<\/li>\n<li>Basically, class is blueprint for specific type. In the human language across the world, if you count, there are lots of classes e.g. car, bank, bird, student, employee etc. are very simple example to understand.<\/li>\n<li>Class is created with the word <code>class <\/code>when you want to define it.<br \/>\nEx: class Mobile{}<\/li>\n<li>Class itself consists of various methods and variable.<\/li>\n<li>To call upon class objects of other classes there must be main method with static keyword. Because with the static word method will be called even if you do not create its objects.<\/li>\n<li>Classes are provided with special access modifiers that are <strong>default, public, private<\/strong> and <strong>protected<\/strong>.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline; color: #008000;\"><strong>Object:<\/strong><\/span> Object is an instance of class. Understanding the concept of object is lot easier when considering real life examples around us because the concept is actually based on real life objects. So just look around yourself and you will find yourself surrounded with lots of objects which has a certain characteristics and behaviors.<\/p>\n<p><strong> For example:<\/strong>\u00a0Your mobile, it\u2019s an example of object which has a lots of characteristics like color, RAM, camera etc and behaviors like calling, messaging etc.<\/p>\n<p><span style=\"color: #008000;\"><strong>How To Create Object:<\/strong><\/span><\/p>\n<p>Suppose Mobile is the class for which we want to create object name abhi. Below is the code:<\/p>\n<pre>\/\/Object abhi is declared\r\nMobile abhi;\r\n\/\/Object is created using new keyword\r\nabhi = new Mobile();<\/pre>\n<p>Alternatively you can create object in one line:<\/p>\n<pre>Mobile abhi = new Mobile();<\/pre>\n<ul>\n<li>Object can be defined as state, behavior of class.<\/li>\n<li>Objects are the instance variable of class.<\/li>\n<li>Objects are stored through references.<\/li>\n<li>Objects are created with <code>new <\/code>keyword in java. Ex: Mobile abhi = new Mobile();<\/li>\n<li>\u00a0Every object has its own memory.<\/li>\n<li>Objects of class of same type can interact with each other means you can pass your message from one object to other.<\/li>\n<\/ul>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Objects are run time entity. They are always getting a memory at run time.<\/p>\n<p><strong><span style=\"color: #ff0000;\">Important Note:<\/span><\/strong> Object are created from a class and methods or actions are performed on object.<\/p>\n<hr \/>\n<h4><strong><span style=\"color: #008000;\">Real Life Example Of Class And Objects:<\/span><\/strong><\/h4>\n<p>Here in this example we will display the details of Mobile which three person Abhishek, Rahul and Ravi own. To do that in JAVA first we will create a class Mobile, declare fields for mobile specs (i.e brand, color, camera) and initialized constructor.<\/p>\n<p>Then in main method we will three object for Mobile class. Each object will have the specification details of Mobile which he owns. And finally using System.out.println() method we display the details:<\/p>\n<pre>\/\/Class Mobile\r\nclass Mobile{\r\n\u00a0\u00a0 \u00a0String brand, color;\r\n\u00a0\u00a0 \u00a0int camera;\r\n\r\n\/\/Constructor initialized\r\n\u00a0\u00a0 \u00a0public Mobile(String brand, String color, int camera){\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.brand = brand;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.color = color;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.camera = camera;\r\n\u00a0\u00a0 \u00a0} \r\n\u00a0\u00a0 \u00a0public static void main(String args[]){ \r\n\r\n\/\/Object created\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Mobile abhishek = new Mobile(\"iPhone\",\"Gold\",8);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Mobile rahul = new Mobile(\"Samsung\",\"White\",13);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Mobile ravi = new Mobile(\"Nexus\",\"Black\",8);\r\n\r\n\/\/Smartphone details displayed for each user\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Abhishek own \" + abhishek.brand +\" \"+ abhishek.color + \" color smartphone having \"+ abhishek.camera+ \"MP\"+ \" camera\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Rahul own \" + rahul.brand +\" \"+ rahul.color + \" color smartphone having \"+ rahul.camera+ \"MP\"+ \" camera\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Ravi own \" + ravi.brand +\" \"+ ravi.color + \" color smartphone having \"+ ravi.camera+ \"MP\"+ \" camera\");\r\n\r\n\u00a0\u00a0 \u00a0} \r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Abhishek own iPhone Gold color smartphone having 8MP camera\r\nRahul own Samsung White color smartphone having 13MP camera\r\nRavi own Nexus Black color smartphone having 8MP camera<\/pre>\n<h4><span style=\"color: #008000;\"><strong>Another Program With Explanation To illustrate The Concept Of Class And Object:<\/strong><\/span><\/h4>\n<p>Here in this program class <strong>ClassInJava<\/strong> is taken to create data type of student who is having Name, Roll number, Fathers\u2019 Name, Contact Number, Address as fields. As from the name it is clear that these are of different primitive data type and all these are taken together in a class.<\/p>\n<p>Here declaration of\u00a0<strong>constructor <\/strong>of the class is used which initialize the object with predefined values which are passed at the creation of object.<\/p>\n<pre>public class ClassInJava {\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0String name;\r\n\u00a0\u00a0 \u00a0int rollno;\r\n\u00a0\u00a0 \u00a0String fathername;\r\n\u00a0\u00a0 \u00a0String contactno;\r\n\u00a0\u00a0 \u00a0String address;\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\/\/Constructor\r\n\r\n\u00a0\u00a0 \u00a0ClassInJava(String name,int rollno,String fathername,String contactno,String address){\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.name=name;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.rollno=rollno;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.fathername=fathername;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.contactno=contactno;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.address=address;\r\n\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\n<\/pre>\n<p>And next class is <strong>ObjectOfClass <\/strong> which is used to create the object of first class. So the main method with the <strong>static<\/strong> written in ObjectOfClass. And this is also good prevention to declare first letter of each word of class name as capital.<\/p>\n<pre>public class ObjectOfClass {\r\n\r\n\u00a0\u00a0 \u00a0\/**Simple illustration of how to create an object of given class and how it works\r\n\u00a0\u00a0 \u00a0 *\/\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String[] args) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/Object of class ClassInJava\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ClassInJava object=new ClassInJava(\"Mr. Abhishek\",123,\"Mr. Sulekh\", \"+1-8745733445\",\"#321, South Street, No-3, Ontario\");\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Student Name is: \" + object.name);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Roll Number is: \" + object.rollno);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Fathers' Name is: \"+ object.fathername);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Contact Number is: \"+ object.contactno);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.print(\"Student Address is: \"+ object.address);\r\n\r\n\u00a0\u00a0 \u00a0}\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Student Name is: Mr. Abhishek\r\nRoll Number is: 123\r\nFathers' Name is: Mr. Sulekh\r\nContact Number is: +1-8745733445\r\nStudent Address is: #321, South Street, No-3, Ontario<\/pre>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Important Points about class and object:<\/strong><\/span><\/h4>\n<ul>\n<li>The word &#8220;Class&#8221; came from simula language.<\/li>\n<li>Class is blueprint for an entity.<\/li>\n<li>In class there are variables and methods.<\/li>\n<li>The access modifiers like public, private and protected used in different situation.<\/li>\n<li>Objects represent the state and behavior of class.<\/li>\n<li>Object is just a memory area or a buffer in heap area in which all the instance data members are getting a memory.<\/li>\n<li>In Java &#8220;new&#8221; operator is only used for allocating a memory for an object only.<\/li>\n<li>Memory associated with object is automatically collected by garbage collector.<\/li>\n<li>Class with main method having static keyword is mandatory to call upon the object of other classes.<\/li>\n<li>All the objects are sharing a same memory location for each static data members.<\/li>\n<\/ul>\n<hr \/>\n<p><span style=\"color: #008000;\"><strong>History about word &#8220;Class&#8221;: <\/strong><\/span>Aristotle was initially consider deeply the concept of type. <strong>Simula<\/strong> is language used for simulation of \u201cbank teller problem\u201d. There were lots of object like customer account, bank detail, employee detail and many more. So all these objects were put together in \u201cclasses of objects\u201d. And hence the word class was invented for programming languages<strong>. <\/strong>That was main origin of word class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might have already heard many times JAVA is an Object Oriented Programming which simply means coding in JAVA constantly involve classes and objects. In other words coding in JAVA is not possible without object and class. Even the smallest Hello world program requires declaration of class and method work on object. So let\u2019s understand &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/class-objects\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Class And Object In JAVA With Examples &#8211; Tutorial<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-369","page","type-page","status-publish","hentry"],"psp_head":"<title>Class And Object In JAVA With Examples - Tutorial \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"JAVA is an Object Oriented Programming which means coding in JAVA constantly involve classes and objects. Learn the concept with examples, code and program.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/class-objects\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=369"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/369\/revisions"}],"predecessor-version":[{"id":1502,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/369\/revisions\/1502"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}