{"id":325,"date":"2025-06-30T07:07:04","date_gmt":"2025-06-30T07:07:04","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=325"},"modified":"2025-07-16T10:12:35","modified_gmt":"2025-07-16T10:12:35","slug":"explain-about-inheritance","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/explain-about-inheritance\/","title":{"rendered":"Explain about Inheritance"},"content":{"rendered":"\n<p>In Java, <strong>inheritance<\/strong> allows a class (called the <em>subclass<\/em> or <em>child class<\/em>) to inherit fields and methods from another class (called the <em>superclass<\/em> or <em>parent class<\/em>). This promotes <strong>code reuse<\/strong> and establishes a natural hierarchy between classes.<\/p>\n\n\n\n<p>Java uses the keyword <code>extends<\/code> to achieve inheritance.<\/p>\n\n\n\n<p>\ud83e\udde9 <strong>Types of Inheritance in Java<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Single Inheritance<\/strong>\n<ul class=\"wp-block-list\">\n<li>A child class inherits from one parent class.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Multilevel Inheritance<\/strong>\n<ul class=\"wp-block-list\">\n<li>A class inherits from a child class, which in turn inherits from another class.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Hierarchical Inheritance<\/strong>\n<ul class=\"wp-block-list\">\n<li>Multiple classes inherit from the same parent class.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Hybrid Inheritance<\/strong> (Not supported directly in Java)\n<ul class=\"wp-block-list\">\n<li>Combination of two or more types of inheritance; Java avoids this using interfaces.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Multiple Inheritance<\/strong> (via Interfaces only)<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A class implements multiple interfaces to achieve multiple inheritance.<\/li>\n<\/ul>\n\n\n\n<p>\u2728 <strong>Examples<\/strong><\/p>\n\n\n\n<p>\u2705 1. Single Inheritance<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    void sound() {\n        System.out.println(\"Animal makes a sound\");\n    }\n}\n\nclass Dog extends Animal {\n    void bark() {\n        System.out.println(\"Dog barks\");\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Dog myDog = new Dog();\n        myDog.sound(); \/\/ Inherited method\n        myDog.bark();  \/\/ Child's own method\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u2705 2. <strong>Multilevel Inheritance<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    void sound() {\n        System.out.println(\"Animal sound\");\n    }\n}\n\nclass Dog extends Animal {\n    void bark() {\n        System.out.println(\"Dog barks\");\n    }\n}\n\nclass Puppy extends Dog {\n    void weep() {\n        System.out.println(\"Puppy weeps\");\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Puppy pup = new Puppy();\n        pup.sound();\n        pup.bark();\n        pup.weep();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>3. Hierarchical Inheritance<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    void eat() {\n        System.out.println(\"Animal eats\");\n    }\n}\n\nclass Dog extends Animal {\n    void bark() {\n        System.out.println(\"Dog barks\");\n    }\n}\n\nclass Cat extends Animal {\n    void meow() {\n        System.out.println(\"Cat meows\");\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Dog d = new Dog();\n        Cat c = new Cat();\n        d.eat();\n        d.bark();\n        c.eat();\n        c.meow();\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explore inheritance in Java and how it enables code reusability and hierarchy. Learn how subclasses extend functionality and promote object-oriented design.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-325","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=325"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/325\/revisions"}],"predecessor-version":[{"id":427,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/325\/revisions\/427"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}