{"id":3814,"date":"2025-01-01T09:00:00","date_gmt":"2025-01-01T04:00:00","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=3814"},"modified":"2025-09-15T09:10:18","modified_gmt":"2025-09-15T04:10:18","slug":"introduction-to-object-oriented-programming-oop","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/01\/introduction-to-object-oriented-programming-oop\/","title":{"rendered":"Introduction to Object-Oriented Programming (OOP)"},"content":{"rendered":"\n<p>Object-Oriented Programming, or OOP, is a way of writing programs by focusing on real-life objects. In the real world, everything we deal with is an object, such as a car, a book, or a student. Each of these objects has certain features and can perform specific actions. In OOP, we try to represent these features as <strong>attributes<\/strong> and the actions as <strong>functions<\/strong>. <a href=\"https:\/\/afzalbadshah.com\/index.php\/comprehensive-guide-to-object-oriented-programming-oop-in-java\/\" title=\"\">The detailed tutorial can be visited here. <\/a><\/p>\n\n\n\n<p>This programming style helps us organize our code in a way that is closer to how we understand things in real life. It makes large programs easier to design, manage, and extend<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Aspect<\/strong><\/th><th><strong>Procedural Programming<\/strong><\/th><th><strong>Object-Oriented Programming (OOP)<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Approach<\/strong><\/td><td>Top-down<\/td><td>Bottom-up<\/td><\/tr><tr><td><strong>Structure<\/strong><\/td><td>Program is divided into functions<\/td><td>Program is divided into objects and classes<\/td><\/tr><tr><td><strong>Data &amp; Functions<\/strong><\/td><td>Data and functions are separate<\/td><td>Data and functions are bundled together in objects<\/td><\/tr><tr><td><strong>Reusability<\/strong><\/td><td>Limited<\/td><td>High (through inheritance and modularity)<\/td><\/tr><tr><td><strong>Security<\/strong><\/td><td>Less secure (data is exposed)<\/td><td>More secure (data hidden using encapsulation)<\/td><\/tr><tr><td><strong>Real-world Modeling<\/strong><\/td><td>Difficult to model real-world scenarios<\/td><td>Easier to model real-world entities and interactions<\/td><\/tr><tr><td><strong>Examples<\/strong><\/td><td>C, Pascal<\/td><td>Java, C++, Python (with OOP), C#<\/td><\/tr><tr><td><strong>Function Reuse<\/strong><\/td><td>Uses functions repeatedly<\/td><td>Uses objects and methods repeatedly<\/td><\/tr><tr><td><strong>Code Maintenance<\/strong><\/td><td>Harder to manage and scale in large programs<\/td><td>Easier to manage due to modular structure<\/td><\/tr><tr><td><strong>Main Focus<\/strong><\/td><td>Focuses on functions (actions)<\/td><td>Focuses on objects (entities)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What is OOP?<\/h2>\n\n\n\n<p>Object-oriented programming is a programming model that allows developers to organize and build programs around real-world concepts. In this model, we define objects that contain both data (attributes) and actions (functions or methods).<\/p>\n\n\n\n<p>The main concepts of OOP are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Classes\n<ul class=\"wp-block-list\">\n<li>Objects<\/li>\n\n\n\n<li>Attributes<\/li>\n\n\n\n<li>Functions<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Encapsulation<\/li>\n\n\n\n<li>Inheritance<\/li>\n\n\n\n<li>Polymorphism<\/li>\n<\/ul>\n\n\n\n<p>Each of these will be explained step-by-step below.<\/p>\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\/2-1024x576.webp?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-3821\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/2-jpg.webp?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/2-jpg.webp?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/2-jpg.webp?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/2-jpg.webp?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/2-jpg.webp?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/2-jpg.webp?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/2-jpg.webp?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">What is OOP?<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use Object-Oriented Programming (OOP)?<\/h3>\n\n\n\n<p><strong>OOP<\/strong> offers several significant advantages that make it a popular programming paradigm:<\/p>\n\n\n\n<p><strong>Reusability:<\/strong> One of the core benefits of OOP is <strong>code reusability<\/strong>. Through <strong>inheritance<\/strong>, new classes can be created based on existing ones, inheriting their properties and methods. This means that you can reuse code from previously developed classes, saving time and effort.<\/p>\n\n\n\n<p><strong>Modularity:<\/strong> OOP promotes <strong>modularity<\/strong> by breaking down complex problems into smaller, more manageable objects. Each object encapsulates its data and behavior, making it easier to understand, modify, and test independently. This modular approach improves code organization and maintainability.<\/p>\n\n\n\n<p><strong>Maintainability:<\/strong> OOP code is generally <strong>easier to maintain<\/strong> than procedural code. The encapsulation of data and behavior within objects helps to isolate changes, making it less likely that modifications in one part of the code will have unintended consequences in another. This reduces the risk of introducing bugs and makes it easier to update and extend the software over time.<\/p>\n\n\n\n<p><strong>Flexibility:<\/strong> <strong>Polymorphism<\/strong> is a fundamental concept in OOP that allows objects of different classes to be treated as if they were of the same type. This provides <strong>flexibility<\/strong> and <strong>adaptability<\/strong> in your code. For example, you can create a function that takes a generic object as an argument, and it can work with objects of different classes as long as they have the necessary methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Concepts in Object-Oriented Programming (OOP)<\/h2>\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\/4-1.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-6305\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/4-1.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/4-1.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/4-1.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/4-1.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/4-1.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/4-1.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/4-1.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Key concepts of OOP<\/figcaption><\/figure>\n\n\n\n<p><strong>Class <\/strong>is the template, design or blueprint for creating real-world objects. It defines what attributes and functions an object will have, but it doesn\u2019t store real data itself.<\/p>\n\n\n\n<p><strong>Objects<\/strong> are instances of a class. They possess specific <strong>attributes<\/strong> (properties) and <strong>behaviors<\/strong> (methods). Each object has a unique identity, allowing it to be distinguished from others. For example my car is an object of the class car. <\/p>\n\n\n\n<p><strong>Encapsulation<\/strong> is the practice of <strong>hiding the internal implementation details of an object<\/strong>. This is achieved through <strong>access control<\/strong> mechanisms that determine how other parts of the program can interact with an object&#8217;s properties and methods. Encapsulation promotes <strong>modularity<\/strong> and <strong>maintainability<\/strong> by making objects more self-contained and easier to understand and modify. In example fo the car, the controls hides the inner functinalities of the car.<\/p>\n\n\n\n<p><strong>Inheritance<\/strong> is a mechanism that allows you to <strong>create new classes based on existing ones<\/strong>. The new class has an <strong>&#8220;is-a&#8221; relationship<\/strong> with the parent class, meaning it is a specialized version of the parent class. Inheritance promotes <strong>code reuse<\/strong> by allowing you to avoid duplicating code. For examole, animal is a base class and we create its subclass &#8216;cow&#8217;, which will inherit the functionalities of inheritance. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Class<\/h2>\n\n\n\n<p>A class is like a <strong>blueprint<\/strong> or <strong>design<\/strong> for creating objects. It defines what attributes and functions an object will have, but it doesn\u2019t store real data itself.<\/p>\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\/5-1.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-6306\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/5-1.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/5-1.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/5-1.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/5-1.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/5-1.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/5-1.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/5-1.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Representation of class in OOP<\/figcaption><\/figure>\n\n\n\n<p>For example, a <code>Student<\/code> class can define:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Attributes like <code>name<\/code>, <code>rollNo<\/code>, and <code>department<\/code><\/li>\n\n\n\n<li>Functions like <code>giveExam()<\/code> and <code>submitAssignment()<\/code><\/li>\n<\/ul>\n\n\n\n<p>You can then create many student objects from this one class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Object<\/h2>\n\n\n\n<p>An object is a <strong>real instance of a class<\/strong>. It is created using the blueprint defined by the class and stores actual data.<\/p>\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\/8-2.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-6307\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/8-2.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/8-2.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/8-2.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/8-2.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/8-2.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/8-2.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/8-2.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Class and its objects in OOP<\/figcaption><\/figure>\n\n\n\n<p>For example, once you define a <code>Student<\/code> class, you can create:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student s1 = new Student();\ns1.name = \"Ali\";\ns1.giveExam();\n<\/code><\/pre>\n\n\n\n<p>Here, <code>s1<\/code> is an object of class <code>Student<\/code>. Each object can have different values for the same attributes, like different names or roll numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Attributes<\/h2>\n\n\n\n<p>Attributes are the <strong>properties or characteristics<\/strong> of an object. These are also called <strong>data members<\/strong>. They describe what an object has.<\/p>\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\/11.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-6308\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/11.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/11.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/11.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/11.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/11.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/11.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/11.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Object attributes in OOP<\/figcaption><\/figure>\n\n\n\n<p>Attributes are written inside a class and accessed through objects.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Book {\n    String title;\n    String author;\n    int pages;\n}\n<\/code><\/pre>\n\n\n\n<p>Here, <code>title<\/code>, <code>author<\/code>, and <code>pages<\/code> are attributes. Each book object will have its own values for these.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Functions<\/h2>\n\n\n\n<p>Functions, also called <strong>methods<\/strong>, define the <strong>actions<\/strong> an object can perform. These are written inside the class and can be called using the object.<\/p>\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\/13.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-6309\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/13.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/13.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/13.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/13.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/13.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/13.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/13.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Function of the object in OOP<\/figcaption><\/figure>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Book {\n    void read() {\n        System.out.println(\"Reading the book...\");\n    }\n\n    void bookmarkPage() {\n        System.out.println(\"Page bookmarked.\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>When we create a <code>Book<\/code> object, it can call these functions to perform tasks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Encapsulation<\/h2>\n\n\n\n<p>Encapsulation means <strong>protecting the internal data<\/strong> of an object by keeping attributes private and accessing them only through special functions (getters and setters).<\/p>\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\/15.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-6310\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/15.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/15.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/15.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/15.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/15.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/15.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/15.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Encapsulation in OOP<\/figcaption><\/figure>\n\n\n\n<p>This improves security and keeps the data safe from direct changes.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student {\n    private int marks;\n\n    public int getMarks() {\n        return marks;\n    }\n\n    public void setMarks(int m) {\n        marks = m;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>Here, <code>marks<\/code> is private, so it cannot be accessed directly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Inheritance<\/h2>\n\n\n\n<p>Inheritance means creating a <strong>new class based on an existing class<\/strong>. The new class (called child) can use everything from the old class (called parent) and also add its own features.<\/p>\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\/17.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-6311\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/17.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/17.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/17.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/17.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/17.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/17.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/17.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Inheritance in OOP<\/figcaption><\/figure>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Vehicle {\n    void start() {\n        System.out.println(\"Starting...\");\n    }\n}\n\nclass Car extends Vehicle {\n    void playMusic() {\n        System.out.println(\"Playing music...\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><code>Car<\/code> is using <code>start()<\/code> from <code>Vehicle<\/code> and also has its own function <code>playMusic()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Polymorphism<\/h2>\n\n\n\n<p>Polymorphism means <strong>one function behaves differently for different classes<\/strong>. It allows the same function name to do different jobs based on the object.<\/p>\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\/19.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-6312\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/19.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/19.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/19.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/19.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/19.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/19.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/19.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Polymorphism in OOP<\/figcaption><\/figure>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    void speak() {\n        System.out.println(\"Animal speaks\");\n    }\n}\n\nclass Dog extends Animal {\n    void speak() {\n        System.out.println(\"Dog barks\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>When we call <code>speak()<\/code>, the result depends on whether the object is an Animal or Dog.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Task<\/h2>\n\n\n\n<p>Take a <strong>Book<\/strong> as an example. Now apply OOP concepts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Object<\/strong>: A specific book like <code>book1<\/code><\/li>\n\n\n\n<li><strong>Attributes<\/strong>: title, author, price<\/li>\n\n\n\n<li><strong>Functions<\/strong>: read(), bookmarkPage()<\/li>\n\n\n\n<li><strong>Encapsulation<\/strong>: Keep <code>price<\/code> private, use methods to get\/set it<\/li>\n\n\n\n<li><strong>Inheritance<\/strong>: Make <code>EBook<\/code> a child class of <code>Book<\/code><\/li>\n\n\n\n<li><strong>Polymorphism<\/strong>: Override <code>read()<\/code> in <code>EBook<\/code> and <code>PrintedBook<\/code><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-canva wp-block-embed-canva\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"OOP\" src=\"https:\/\/www.canva.com\/design\/DAGPn6yCRc4\/2WBEAOb9sNj5hYcyJH-t6w\/view?embed&amp;meta\" height=\"360\" width=\"640\" style=\"border: none; border-radius: 8px; width: 640px; height: 360px;\" allowfullscreen=\"allowfullscreen\" allow=\"fullscreen\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object-Oriented Programming, or OOP, is a way of writing programs by focusing on real-life objects. In the real world, everything we deal with is an object, such as a car, a book, or a student. Each of these objects has certain features and can perform specific actions. In OOP, we try to represent these features as attributes and the actions as functions. The detailed tutorial can be visited here. This programming style helps us organize our code in a way&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/01\/introduction-to-object-oriented-programming-oop\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":6316,"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":[605,415,607,606,604,603,608],"class_list":["post-3814","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-object-oriented-programing-oop","tag-class","tag-encapsulation","tag-inheritance","tag-object","tag-object-oriented-programing","tag-oop","tag-polymorphism"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/OOP.jpg?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-Zw","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3814","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=3814"}],"version-history":[{"count":6,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3814\/revisions"}],"predecessor-version":[{"id":6315,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3814\/revisions\/6315"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/6316"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=3814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=3814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=3814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}