{"id":6414,"date":"2025-07-09T10:09:01","date_gmt":"2025-07-09T05:09:01","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=6414"},"modified":"2025-07-09T10:09:03","modified_gmt":"2025-07-09T05:09:03","slug":"inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/","title":{"rendered":"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called the derived class or child class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What is the purpose of inheritance?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we are building a system for a school. We create a class called &#8220;Employee&#8221; that contains common information like name, ID, and salary. Now, we want to create a &#8220;Teacher&#8221; class. Instead of writing all the properties again, we let the &#8220;Teacher&#8221; class inherit from the &#8220;Employee&#8221; class. This way, Teacher automatically gets all the common features from Employee. We can then add teacher-specific features like subject or department. This is called code reusability. We do not repeat code unnecessarily, and we build a more organized structure where each class plays its specific role.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Basic syntax of inheritance<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Java, inheritance is declared using the keyword <code>extends<\/code>. Here is the general format:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Base {\n    \/\/ members of base class\n}\n\nclass Derived extends Base {\n    \/\/ members of derived class\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>extends<\/code> keyword means that the derived class will inherit all public and protected members from the base class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Simple example of inheritance<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee {\n    String name;\n    int id;\n\n    void displayInfo() {\n        System.out.println(\"Name: \" + name);\n        System.out.println(\"ID: \" + id);\n    }\n}\n\nclass Teacher extends Employee {\n    String subject;\n\n    void displayTeacher() {\n        displayInfo();\n        System.out.println(\"Subject: \" + subject);\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Teacher t = new Teacher();\n        t.name = \"Dr. Afzal\";\n        t.id = 101;\n        t.subject = \"Computer Science\";\n        t.displayTeacher();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, Teacher is inheriting from Employee. The Teacher class gets access to the name and id fields, and also to the displayInfo function. It also has its own field called subject and a function called displayTeacher.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>How constructors behave in inheritance<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Constructors are special methods that are automatically called when an object is created. In inheritance, when a derived class object is created, the base class constructor runs first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee {\n    Employee() {\n        System.out.println(\"Employee constructor called\");\n    }\n}\n\nclass Teacher extends Employee {\n    Teacher() {\n        System.out.println(\"Teacher constructor called\");\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Teacher t = new Teacher();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Employee constructor called\nTeacher constructor called\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Multiple Inheritance in Java<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Java does not support multiple inheritance with classes. However, it can be achieved using <strong>interfaces<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example using interfaces:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Person {\n    void setName(String n);\n}\n\ninterface Worker {\n    void setHours(int h);\n}\n\nclass Engineer implements Person, Worker {\n    String name;\n    int hoursWorked;\n\n    public void setName(String n) {\n        name = n;\n    }\n\n    public void setHours(int h) {\n        hoursWorked = h;\n    }\n\n    void display() {\n        System.out.println(\"Name: \" + name);\n        System.out.println(\"Hours Worked: \" + hoursWorked);\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Engineer e = new Engineer();\n        e.setName(\"Ali\");\n        e.setHours(40);\n        e.display();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this program, the class <code>Engineer<\/code> implements both <code>Person<\/code> and <code>Worker<\/code> interfaces. It gains the ability to store a name and number of hours worked, then display both using its own method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Access specifiers and inheritance<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Java has four main access specifiers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>public<\/code>: accessible everywhere<\/li>\n\n\n\n<li><code>protected<\/code>: accessible in the same package and subclasses<\/li>\n\n\n\n<li><code>private<\/code>: accessible only within the same class<\/li>\n\n\n\n<li>(default): accessible within the same package<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In public inheritance, public and protected members of the base class are accessible in the derived class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Real-life analogy to summarize<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think about vehicles. You have a base class called Vehicle. It has functions like start() and stop(). Now you create a derived class called Car. It automatically gets start() and stop(), but it may also have its own function openSunroof(). Similarly, a Truck class may have its own function loadCargo(). This is how we build specialized classes from a general one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Another example: Person and Student<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {\n    String name;\n    int age;\n\n    void showPerson() {\n        System.out.println(\"Name: \" + name + \", Age: \" + age);\n    }\n}\n\nclass Student extends Person {\n    int rollNumber;\n\n    void showStudent() {\n        showPerson();\n        System.out.println(\"Roll Number: \" + rollNumber);\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Student s = new Student();\n        s.name = \"Sara\";\n        s.age = 20;\n        s.rollNumber = 1234;\n        s.showStudent();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance allows you to create a new class that can reuse the features of an existing class. It helps in making your code more organized, more reusable, and easier to manage. You can use inheritance to model real-world relationships where one entity is a specialized version of another. A Teacher is an Employee. A Student is a Person. An Engineer is both a Person and a Worker. These examples help map real-world understanding into Java code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called the derived class or child class. What is the purpose of inheritance? Suppose we are building a system for a school. We create a class&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":6416,"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":false,"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":[],"class_list":["post-6414","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-object-oriented-programing-oop"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Afzal Badshah, PhD\"\/>\n\t<meta name=\"google-site-verification\" content=\"B8FNGv4w1toMpU3HUnD_SN9H9YMtp1ObsLuEDuirArI\" \/>\n\t<meta name=\"p:domain_verify\" content=\"87b5ca26c36438b20581a102dc290a47\" \/>\n\t<meta name=\"yandex-verification\" content=\"0eb3e2a9157fd34d\" \/>\n\t<meta name=\"keywords\" content=\"object oriented programing (oop)\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_GB\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Afzal Badshah, PhD - Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation - Afzal Badshah, PhD\" \/>\n\t\t<meta property=\"og:description\" content=\"Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/Copy-of-OOP.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/Copy-of-OOP.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2025-07-09T05:09:01+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2025-07-09T05:09:03+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/web.facebook.com\/abmanduri\/\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@DrAFZALBADSHAH\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation - Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@DrAFZALBADSHAH\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/Copy-of-OOP.jpg\" \/>\n\t\t<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t\t<meta name=\"twitter:data1\" content=\"Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#blogposting\",\"name\":\"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation - Afzal Badshah, PhD\",\"headline\":\"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation\",\"author\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/afzalbadshah.com\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/Copy-of-OOP.jpg?fit=1920%2C1080&ssl=1\",\"width\":1920,\"height\":1080},\"datePublished\":\"2025-07-09T10:09:01+05:00\",\"dateModified\":\"2025-07-09T10:09:03+05:00\",\"inLanguage\":\"en-GB\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#webpage\"},\"articleSection\":\"Object Oriented Programing (OOP)\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/afzalbadshah.com\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"name\":\"Courses\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"position\":2,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/object-oriented-programing-oop\\\/#listItem\",\"name\":\"Object Oriented Programing (OOP)\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/object-oriented-programing-oop\\\/#listItem\",\"position\":3,\"name\":\"Object Oriented Programing (OOP)\",\"item\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/object-oriented-programing-oop\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#listItem\",\"name\":\"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"name\":\"Courses\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#listItem\",\"position\":4,\"name\":\"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/object-oriented-programing-oop\\\/#listItem\",\"name\":\"Object Oriented Programing (OOP)\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\",\"name\":\"Afzal Badshah, PhD\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#personImage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/wp-content\\\/litespeed\\\/avatar\\\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1786545756\",\"width\":96,\"height\":96,\"caption\":\"Afzal Badshah, PhD\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/\",\"name\":\"Afzal Badshah, PhD\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#authorImage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/wp-content\\\/litespeed\\\/avatar\\\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1786545756\",\"width\":96,\"height\":96,\"caption\":\"Afzal Badshah, PhD\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#webpage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/\",\"name\":\"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation - Afzal Badshah, PhD\",\"description\":\"Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called\",\"inLanguage\":\"en-GB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/afzalbadshah.com\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/Copy-of-OOP.jpg?fit=1920%2C1080&ssl=1\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#mainImage\",\"width\":1920,\"height\":1080},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/07\\\/09\\\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\\\/#mainImage\"},\"datePublished\":\"2025-07-09T10:09:01+05:00\",\"dateModified\":\"2025-07-09T10:09:03+05:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/\",\"name\":\"Afzal Badshah, PhD\",\"alternateName\":\"Afzal Badshah\",\"description\":\"Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence\",\"inLanguage\":\"en-GB\",\"publisher\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation - Afzal Badshah, PhD","description":"Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called","canonical_url":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/","robots":"max-image-preview:large","keywords":"object oriented programing (oop)","webmasterTools":{"google-site-verification":"B8FNGv4w1toMpU3HUnD_SN9H9YMtp1ObsLuEDuirArI","p:domain_verify":"87b5ca26c36438b20581a102dc290a47","yandex-verification":"0eb3e2a9157fd34d","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#blogposting","name":"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation - Afzal Badshah, PhD","headline":"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation","author":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"publisher":{"@id":"https:\/\/afzalbadshah.com\/#person"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/Copy-of-OOP.jpg?fit=1920%2C1080&ssl=1","width":1920,"height":1080},"datePublished":"2025-07-09T10:09:01+05:00","dateModified":"2025-07-09T10:09:03+05:00","inLanguage":"en-GB","mainEntityOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#webpage"},"isPartOf":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#webpage"},"articleSection":"Object Oriented Programing (OOP)"},{"@type":"BreadcrumbList","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com#listItem","position":1,"name":"Home","item":"https:\/\/afzalbadshah.com","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","name":"Courses"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","position":2,"name":"Courses","item":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/object-oriented-programing-oop\/#listItem","name":"Object Oriented Programing (OOP)"},"previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/object-oriented-programing-oop\/#listItem","position":3,"name":"Object Oriented Programing (OOP)","item":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/object-oriented-programing-oop\/","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#listItem","name":"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation"},"previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","name":"Courses"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#listItem","position":4,"name":"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation","previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/object-oriented-programing-oop\/#listItem","name":"Object Oriented Programing (OOP)"}}]},{"@type":"Person","@id":"https:\/\/afzalbadshah.com\/#person","name":"Afzal Badshah, PhD","image":{"@type":"ImageObject","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#personImage","url":"https:\/\/afzalbadshah.com\/wp-content\/litespeed\/avatar\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1786545756","width":96,"height":96,"caption":"Afzal Badshah, PhD"}},{"@type":"Person","@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author","url":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/","name":"Afzal Badshah, PhD","image":{"@type":"ImageObject","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#authorImage","url":"https:\/\/afzalbadshah.com\/wp-content\/litespeed\/avatar\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1786545756","width":96,"height":96,"caption":"Afzal Badshah, PhD"}},{"@type":"WebPage","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#webpage","url":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/","name":"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation - Afzal Badshah, PhD","description":"Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called","inLanguage":"en-GB","isPartOf":{"@id":"https:\/\/afzalbadshah.com\/#website"},"breadcrumb":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#breadcrumblist"},"author":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"creator":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/Copy-of-OOP.jpg?fit=1920%2C1080&ssl=1","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#mainImage","width":1920,"height":1080},"primaryImageOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/#mainImage"},"datePublished":"2025-07-09T10:09:01+05:00","dateModified":"2025-07-09T10:09:03+05:00"},{"@type":"WebSite","@id":"https:\/\/afzalbadshah.com\/#website","url":"https:\/\/afzalbadshah.com\/","name":"Afzal Badshah, PhD","alternateName":"Afzal Badshah","description":"Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence","inLanguage":"en-GB","publisher":{"@id":"https:\/\/afzalbadshah.com\/#person"}}]},"og:locale":"en_GB","og:site_name":"Afzal Badshah, PhD - Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence","og:type":"article","og:title":"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation - Afzal Badshah, PhD","og:description":"Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called","og:url":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/","og:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/Copy-of-OOP.jpg","og:image:secure_url":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/Copy-of-OOP.jpg","og:image:width":1920,"og:image:height":1080,"article:published_time":"2025-07-09T05:09:01+00:00","article:modified_time":"2025-07-09T05:09:03+00:00","article:publisher":"https:\/\/web.facebook.com\/abmanduri\/","twitter:card":"summary_large_image","twitter:site":"@DrAFZALBADSHAH","twitter:title":"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation - Afzal Badshah, PhD","twitter:description":"Inheritance is one of the core ideas in object-oriented programming. It allows one class to use the properties and behavior of another class. In simple terms, inheritance helps us create a new class based on an existing class. The existing class is called the base class or parent class, and the new class is called","twitter:creator":"@DrAFZALBADSHAH","twitter:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/Copy-of-OOP.jpg","twitter:label1":"Written by","twitter:data1":"Afzal Badshah, PhD","twitter:label2":"Est. reading time","twitter:data2":"4 minutes"},"aioseo_meta_data":{"post_id":"6414","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":true,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":{"faqs":[],"keyPoints":[],"titles":[],"descriptions":[],"socialPosts":{"email":[],"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"created":"2025-07-09 04:39:51","updated":"2025-07-09 05:14:07","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/\" title=\"Courses\">Courses<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/object-oriented-programing-oop\/\" title=\"Object Oriented Programing (OOP)\">Object Oriented Programing (OOP)<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tInheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/afzalbadshah.com"},{"label":"Courses","link":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/"},{"label":"Object Oriented Programing (OOP)","link":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/object-oriented-programing-oop\/"},{"label":"Inheritance in Java for Beginners: Complete Guide with Examples and Real-Life Explanation","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/07\/09\/inheritance-in-java-for-beginners-complete-guide-with-examples-and-real-life-explanation\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/07\/Copy-of-OOP.jpg?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-1Fs","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/6414","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=6414"}],"version-history":[{"count":4,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/6414\/revisions"}],"predecessor-version":[{"id":6421,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/6414\/revisions\/6421"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/6416"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=6414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=6414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=6414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}