{"id":5280,"date":"2021-03-23T16:48:52","date_gmt":"2021-03-23T16:48:52","guid":{"rendered":"https:\/\/ded9.com\/?p=5280"},"modified":"2025-11-05T10:34:32","modified_gmt":"2025-11-05T10:34:32","slug":"teaching-classes-and-objects-in-java-in-very-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/teaching-classes-and-objects-in-java-in-very-simple-language\/","title":{"rendered":"Teaching Classes &#038; Objects in Java \u2014 A Super Simple Guide"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Java is an object-oriented programming language that allows you to split complex problems into smaller sets by creating objects.\u00a0These objects have two properties:<\/span><\/p>\n<p>Java is an object-oriented programming language (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming\" target=\"_blank\" rel=\"noopener\">OOP<\/a>). In this tutorial, you will get acquainted with object orientation and how to create custom classes and objects in the program.<span id=\"more-50117\"><\/span><\/p>\n<ul>\n<li>state of<\/li>\n<li>Behavior<\/li>\n<\/ul>\n<p>Let&#8217;s give some examples:<\/p>\n<p>1- A Lamp is an object<\/p>\n<ul>\n<li>It can be on or off.<\/li>\n<li>You can turn the Lamp on and off. (Behavior)<\/li>\n<\/ul>\n<p>2- A bicycle is an object<\/p>\n<ul>\n<li>It has gears, two wheels, several gears, etc. (mode)<\/li>\n<li>It can be braked, accelerated, gears changed, and so on. (Behavior)<\/li>\n<\/ul>\n<h2><span style=\"font-size: 18pt;\">Java class<\/span><\/h2>\n<h2><span style=\"font-size: 18pt;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-257955 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/types-of-classes-in-java.png\" alt=\"Java class\" width=\"700\" height=\"315\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/types-of-classes-in-java.png 700w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/types-of-classes-in-java-300x135.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/span><\/h2>\n<p>Before you can create objects in <a href=\"https:\/\/ded9.com\/teaching-classes-and-objects-in-java-in-very-simple-language\/\">Java<\/a>, you must define a class.<\/p>\n<p>A class is a design for an object. We can imagine the classroom as a design (prototype) of a house. It contains all the details about floors, doors, windows, etc. Based on these descriptions, we built the house. The house is an object.<\/p>\n<p>Since many houses are built with the same description, we can create many objects from one class.<\/p>\n<h2><span style=\"font-size: 18pt;\">How to define a class in Java?<\/span><\/h2>\n<p>Here is how to define a class in Java:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class ClassName {<\/li>\n<li dir=\"ltr\">\/\/ variables<\/li>\n<li dir=\"ltr\">\u00a0 \/\/ methods<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Another example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Lamp {<\/li>\n<li dir=\"ltr\">\u00a0 \/\/ variable instance<\/li>\n<li dir=\"ltr\">private boolean isOn;<\/li>\n<li dir=\"ltr\">\u00a0 \/\/ method<\/li>\n<li dir=\"ltr\">public void turnOn () {<\/li>\n<li dir=\"ltr\">isOn = true;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">\/\/ method<\/li>\n<li dir=\"ltr\">public void turnOff () {<\/li>\n<li dir=\"ltr\">isOn = false;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Here, we defined a class called Lamp.<\/p>\n<p>The class has a sample variable (variable defined within the class), which is On, and two methods (turnOn and turnOff). These variables and methods defined in a class are called class members.<\/p>\n<p>In the above program, focus on two keywords: private and public. These terms define the level of access, which we will explain in later tutorials. For now, remember:<\/p>\n<ul>\n<li>The private keyword privatizes variables and methods that are only accessible within the same class.<\/li>\n<li>The public keyword generalizes variables and methods that can be accessed outside the classroom.<\/li>\n<\/ul>\n<p>In the above program, the variable isOn is private, while the methods turnOn () and turnOff () are public.<\/p>\n<p>If you try to access private members from outside the classroom, the compiler will show an error.<\/p>\n<h2><span style=\"font-size: 18pt;\">Java objects<\/span><\/h2>\n<h1><span style=\"font-size: 18pt;\"><img decoding=\"async\" class=\"aligncenter wp-image-257952 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/what-is-object-class-in-java.jpg\" alt=\"Java objects\" width=\"1701\" height=\"1529\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/what-is-object-class-in-java.jpg 1701w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/what-is-object-class-in-java-300x270.jpg 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/what-is-object-class-in-java-1024x920.jpg 1024w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/what-is-object-class-in-java-768x690.jpg 768w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/what-is-object-class-in-java-1536x1381.jpg 1536w\" sizes=\"(max-width: 1701px) 100vw, 1701px\" \/><\/span><\/h1>\n<p>When the class is defined, only the object properties are defined.\u00a0No memory or storage space is allocated.<\/p>\n<p>You must create an object to access the class&#8217;s defined members. In the following code snippet, we created an object from the Lamp class.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Lamp {<\/li>\n<li dir=\"ltr\">boolean isOn;<\/li>\n<li dir=\"ltr\">void turnOn () {<\/li>\n<li dir=\"ltr\">isOn = true;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">void turnOff () {<\/li>\n<li dir=\"ltr\">isOn = false;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class ClassObjectsExample {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Lamp l1 = new Lamp ();\u00a0\/\/ create l1 object of Lamp class<\/li>\n<li dir=\"ltr\">Lamp l2 = new Lamp ();\u00a0\/\/ create l2 object of Lamp class<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>This program creates two objects, l1 and l2, from the Lamp class.<\/p>\n<h2><span style=\"font-size: 18pt;\">How do you access members?<\/span><\/h2>\n<p>You can access members (functions and variables) using (.).\u00a0For example,<\/p>\n<blockquote>\n<p dir=\"ltr\">l1.turnOn ();<\/p>\n<\/blockquote>\n<p>This statement invokes the turnOn () method in the Lamp class for object l1.<\/p>\n<p>When you call a method using the above expression, all expressions in the body of the method turnN () are executed.\u00a0Then, control the program under the phrase<\/p>\n<p dir=\"ltr\">l1.turnOn ()<\/p>\n<p>Jumps.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-252427 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/c-users-mr-desktop-java-method-working-jpg.webp\" alt=\"How do you access members?\" width=\"450\" height=\"369\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/c-users-mr-desktop-java-method-working-jpg.webp 450w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/c-users-mr-desktop-java-method-working-jpg-300x246.webp 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>Similarly, the sample variable can be accessed:<\/p>\n<blockquote>\n<p dir=\"ltr\">l2.isOn = false;<\/p>\n<\/blockquote>\n<p>It is important to note that private members are only accessible within the classroom.\u00a0If the code<\/p>\n<p dir=\"ltr\">l2.isOn = false;<\/p>\n<p>The compiler shows an error in the primary () function (outside the Lamp class).<\/p>\n<p><strong>Example: Classes and Objects in Java<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Lamp {<\/li>\n<li dir=\"ltr\">boolean isOn;<\/li>\n<li dir=\"ltr\">void turnOn () {<\/li>\n<li dir=\"ltr\">isOn = true;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">void turnOff () {<\/li>\n<li dir=\"ltr\">isOn = false;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">void displayLightStatus () {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">System.out.println (\u201cLight on?\u201d + IsOn);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class ClassObjectsExample {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">Lamp l1 = new Lamp (), l2 = new Lamp ();<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">l1.turnOn ();<\/li>\n<li dir=\"ltr\">l2.turnOff ();<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">l1.displayLightStatus ();<\/li>\n<li dir=\"ltr\">l2.displayLightStatus ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Light on?\u00a0true<\/p>\n<p dir=\"ltr\">Light on?\u00a0false<\/p>\n<\/blockquote>\n<p><strong>In the above program,<\/strong><\/p>\n<ul>\n<li>The Lamp class is created.<\/li>\n<li>The class has one instance variable, isOn, and three methods (turnOn (), turnOff, and displayLightStatus ().<\/li>\n<li>The primary function creates two objects, ts l1 and l2, of the Lamp class.<\/li>\n<li>Here, the turnOn () method is called using the object l1:; () l1.turnOn<\/li>\n<li>This method sets the isOn variable of object l1 to true.<\/li>\n<li>And the turnoff () method is called using the object l2:; () l2.turnOff<\/li>\n<li>This method sets the isOff variable of object l2 to false.<\/li>\n<li>Finally, () l1.displayLightStatus phrase Light on? Prints true. Because the isOn variable is set to true for object l1.<\/li>\n<li>And l2.displayLightStatus; The phrase Light on? Prints False. The reason is that the isOn variable for object l2 is set to false.<\/li>\n<\/ul>\n<p>When objects are initialized, each contains its own version of variables and methods. For example, the value of the variable is different for objects l1 and l2.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is a class in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A class is a blueprint that defines the properties (variables) and behaviors (methods) that its objects will have.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you create an object from a class?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>You use the new keyword, e.g. Lamp l1 = new Lamp(); to make an instance of the Lamp class.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why do we use private and public keywords?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>private restricts access so that only the class itself can use that field or method, while public allows access from other classes.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Java is an object-oriented programming language that allows you to split complex problems into smaller sets by creating objects.\u00a0These objects have two properties: Java is an object-oriented programming language (OOP). In this tutorial, you will get acquainted with object orientation and how to create custom classes and objects in the program. state of Behavior Let&#8217;s [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5282,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[4318],"class_list":["post-5280","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-oop"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=5280"}],"version-history":[{"count":8,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5280\/revisions"}],"predecessor-version":[{"id":265021,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5280\/revisions\/265021"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/5282"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=5280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=5280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=5280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}