{"id":3852,"date":"2024-09-05T08:24:06","date_gmt":"2024-09-05T03:24:06","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=3852"},"modified":"2024-10-30T09:56:22","modified_gmt":"2024-10-30T04:56:22","slug":"understanding-class-and-its-components-in-java","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/09\/05\/understanding-class-and-its-components-in-java\/","title":{"rendered":"Understanding Class and Its Components in Java"},"content":{"rendered":"\n<p>A class is a blueprint or a template for creating objects. It defines the properties (attributes) and behaviors (methods) that an object of that class will have. Think of a blueprint for a house. The blueprint defines the layout, number of rooms, and overall structure of the house. When you build houses based on this blueprint, each house will have the same layout and structure. In programming, a class is like a blueprint for objects. It defines the properties and behaviours that objects of that class will have.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: The <code>Car<\/code> Class<\/strong><\/h3>\n\n\n\n<p>Let&#8217;s create a class called <code>Car<\/code> to represent a car. A car has properties like its color, make, and model, and behaviors like starting, stopping, and accelerating.<\/p>\n\n\n\n<p><strong>Class Car<\/strong><\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Car {\n    private String color;\n    private String make;\n    private String model;\n\n    public Car(String color, String make, String model) {\n        this.color = color;\n        this.make = make;\n        this.model = model;\n    }\n\n    public void start() {\n        System.out.println(\"Car started.\");\n    }\n\n    public void stop() {\n        System.out.println(\"Car stopped.\");\n    }\n\n    public void accelerate() {\n        System.out.println(\"Car accelerating.\");\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Main Class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public class Main {\npublic static void main(String&#91;] args) {\n\/\/ Create a car object\nCar myCar = new Car(\"red\", \"Toyota\", \"Corolla\");   \/\/ Call methods on the car object\n    myCar.start();\n    myCar.accelerate();\n    myCar.stop();\n}}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Class Variables<\/strong><\/h3>\n\n\n\n<p>Class variables, also known as static variables in Java, are attributes that belong to the class itself, rather than to individual objects of that class. They are shared by all instances of the class.<\/p>\n\n\n\n<p>In our <code>Car<\/code> class, we can define a class variable to represent the default number of wheels a car has:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private String color;\n    private String make;\n    private String model;<\/code><\/pre>\n\n\n\n<p>Now, every <code>Car<\/code> object will have the above-mentioned attributes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Class Functions (Methods)<\/strong><\/h3>\n\n\n\n<p>Class functions, also known as methods in Java, are the behaviors or actions that an object of the class can perform. They define what the object can do.<\/p>\n\n\n\n<p>In our <code>Car<\/code> class, we have defined three methods: <code>start<\/code>, <code>stop<\/code>, and <code>accelerate<\/code>. These methods represent actions that a car can perform.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Objects (Instances) of a Class<\/strong><\/h3>\n\n\n\n<p>To use a class, we create objects, also known as instances, of that class. Each object is a unique instance of the class and has its own set of values for the class&#8217;s attributes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Car myCar = new Car(\"red\", \"Toyota\", \"Corolla\");\nCar yourCar = new Car(\"blue\", \"Honda\", \"Civic\");\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Introduction to Object Oriented Programing (OOP)\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/Oki2VvIiTVw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing Class Variables and Methods<\/strong><\/h3>\n\n\n\n<p>Once we have created objects, we can access their attributes and call their methods:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.println(myCar.getColor());  \/\/ Output: red\nmyCar.start();  \/\/ Output: Car started.\nSystem.out.println(Car.defaultWheels);  \/\/ Output: 4<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Points to Remember<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A class is a blueprint for creating objects.<\/li>\n\n\n\n<li>Class variables (static variables in Java) are shared by all instances of a class.<\/li>\n\n\n\n<li>Class functions (methods) define the behaviours of objects.<\/li>\n\n\n\n<li>Objects are instances of a class.<\/li>\n\n\n\n<li>You can access class variables and methods using dot notation.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/Object-Oriented-Programing-OOP-1-1024x576.webp?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-3865\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/Object-Oriented-Programing-OOP-1-jpg.webp?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/Object-Oriented-Programing-OOP-1-jpg.webp?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/Object-Oriented-Programing-OOP-1-jpg.webp?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/Object-Oriented-Programing-OOP-1-jpg.webp?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/Object-Oriented-Programing-OOP-1-jpg.webp?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/Object-Oriented-Programing-OOP-1-jpg.webp?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/Object-Oriented-Programing-OOP-1-jpg.webp?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Class (OOP) assignment<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><a href=\"https:\/\/www.canva.com\/design\/DAGPzwfGlwI\/ez9TBBHcANSgfy-kqJtWPw\/view?utm_content=DAGPzwfGlwI&amp;utm_campaign=designshare&amp;utm_medium=link&amp;utm_source=editor\" target=\"_blank\" rel=\"noopener\" title=\"\">Download the presentation here. <\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>A class is a blueprint or a template for creating objects. It defines the properties (attributes) and behaviors (methods) that an object of that class will have. Think of a blueprint for a house. The blueprint defines the layout, number of rooms, and overall structure of the house. When you build houses based on this blueprint, each house will have the same layout and structure. In programming, a class is like a blueprint for objects. It defines the properties and&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/09\/05\/understanding-class-and-its-components-in-java\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3858,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[602],"tags":[613,603],"class_list":["post-3852","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-object-oriented-programing-oop","tag-classs","tag-oop"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/Object-Oriented-Programing-OOP-jpg.webp?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-108","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=3852"}],"version-history":[{"count":9,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3852\/revisions"}],"predecessor-version":[{"id":4628,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3852\/revisions\/4628"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/3858"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=3852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=3852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=3852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}