

{"id":7265,"date":"2018-02-05T10:18:17","date_gmt":"2018-02-05T10:18:17","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=7265"},"modified":"2026-05-18T16:13:31","modified_gmt":"2026-05-18T10:43:31","slug":"inheritance-in-java","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/","title":{"rendered":"Inheritance in Java with Examples"},"content":{"rendered":"<p>Before we begin to talk about inheritance in Java, let us understand a real-life situation here first. Imagine you have an older brother. He has to go off to college, and you get all his stuff when he is gone. This simple concept is called inheritance i.e, passing down of values.<\/p>\n<h3>Inheritance in Java<\/h3>\n<p>Inheritance in Java is one of the most important topics in Object-Oriented Programming. With the help of this, classes can reuse code from another class. Before we dive into the concepts of Inheritance, we must know a few concepts:<\/p>\n<p><strong>Java Subclass<\/strong><\/p>\n<p>A subclass in Java is a class that inherits methods and values from the superclass. It is the child class. It can have methods and values of its own aside from possessing all the variables.<\/p>\n<p><strong>Java Superclass<\/strong><\/p>\n<p>A superclass in Java is a class from which the child class inherits all the methods and values. This is the parent class.<\/p>\n<p><strong>Syntax for defining inheritance between two classes:<\/strong><\/p>\n<p><strong>&lt;child class&gt; extends &lt;parent-class&gt;<\/strong><\/p>\n<h3>Why do we need Inheritance in Java?<\/h3>\n<p>Inheritance allows programmers to define a class in terms of another class, which enhances code readability and interpretability. This helps in the maintenance of the application. We can also reuse the code from the parent class and override it according to the needs of the program.<\/p>\n<h3>extends Keyword in Java<\/h3>\n<p>The extends keyword extends the functionality of the child class by inheriting values from the parent class. It defines an \u201cis-a\u201d relationship. It comes after the class definition and carries the class name of the parent class. The syntax is as follows<\/p>\n<p><strong>&lt;child class&gt; extends &lt;parent-class&gt;<\/strong><\/p>\n<p>This means that the child class is a type of parent class.<\/p>\n<h3>Types of relationships in Java<\/h3>\n<p>There are two types of relationships in Java. They are as follows:<br \/>\n<strong>1.<\/strong> \u201cis-a\u201d relationship<br \/>\n<strong>2.<\/strong> \u201cHas-a\u201d relationship<\/p>\n<h4>Java \u201cis-a\u201d relationship<\/h4>\n<p>An \u201cis-a\u201d relationship gets implemented by inheritance. This means that a certain class is a part of the parent class. This is essential for code reusability. Inheritance is unidirectional, which means that the child class is a type of parent class, but the inverse is not true. For example, a brush is a tool, but not all tools are brushes. This relationship gets implemented with the help of extends and implements keywords.<\/p>\n<h4>Java \u201chas-a\u201d relationship<\/h4>\n<p>Composition is the other word of the \u201chas-a\u201d relationship. It means that a certain instance of this class has a reference to another class or is in the same class. This relationship also helps in code reusability.<\/p>\n<p>For example, A square has edges, A brush has bristles, and a cat has a tail. This implementation is possible if we use the new keyword.<\/p>\n<h4>Benefits of Inheritance in Java<\/h4>\n<p>Inheritance offers several advantages in object-oriented programming:<\/p>\n<p><strong>1. Code Reusability:<\/strong> By inheriting properties and behaviors from a parent class, you can avoid duplicating code. This promotes code maintainability and reduces the chances of errors.<\/p>\n<p><strong>2. Extensibility:<\/strong> Inheritance allows you to create specialized subclasses that extend the functionality of a parent class. This is particularly useful for building hierarchies of related classes.<\/p>\n<p><strong>3. Polymorphism:<\/strong> Inheritance enables polymorphic behavior, where subclasses can redefine methods inherited from the parent class. This allows you to write flexible code that can work with objects of different types.<\/p>\n<h3>Different Types of Inheritance in Java<\/h3>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84343\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1.jpg\" alt=\"Types of Inheritance in Java\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1-720x377.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1-520x272.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1-320x167.jpg 320w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p>Java supports the following kinds of interfaces:<br \/>\n<strong>a.<\/strong> Single Inheritance<br \/>\n<strong>b.<\/strong> Multilevel Inheritance<br \/>\n<strong>c.<\/strong> Hierarchical Inheritance<br \/>\n<strong>d.<\/strong> Multiple Inheritance(Only through Interfaces)<br \/>\n<strong>e.<\/strong> Hybrid Inheritance<\/p>\n<h4>Single Inheritance in Java<\/h4>\n<p>In single inheritance, a single child class inherits data and methods from its parent class. In this case, a child class can access all the methods and the variables of the parent class.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Single-Inheritance-in-java-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84344\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Single-Inheritance-in-java-1.jpg\" alt=\"Single Inheritance in java\" width=\"295\" height=\"214\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Single-Inheritance-in-java-1.jpg 295w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Single-Inheritance-in-java-1-150x109.jpg 150w\" sizes=\"auto, (max-width: 295px) 100vw, 295px\" \/><\/a><\/p>\n<p>This diagram illustrates a single inheritance where class B inherits from class A.<\/p>\n<p><strong>Java program to illustrate Single Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javainheritance;\r\nclass Father {\r\n  String familyName;\r\n  String houseaddress;\r\n  Father() {\r\n    familyName = \"Programmer\";\r\n    houseaddress = \"Delhi\";\r\n  }\r\n}\r\npublic class Son extends Father {\r\n  String name;\r\n  Son() {\r\n    name = \"Shraman\";\r\n  }\r\n  void printdetails() {\r\n    System.out.println(\"Hey my name is \" + this.name + \" \" + this.familyName + \" and I am from \" + this.houseaddress);\r\n  }\r\n  public static void main(String[] args) {\r\n    Son s1 = new Son();\r\n    s1.printdetails();\r\n\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Hey my name is Shraman Programmer and I am from Delhi<\/div>\n<h4>Multilevel Inheritance in Java<\/h4>\n<p>This represents a multi-tier inheritance wherein the child class inherits from a parent class, which in itself is a child class of another parent class.\u00a0It is created when a single level contains more levels of inheritance. In multilevel inheritance, the child class at the end will have all the properties of the first parent class. A real-life example would be a child inheriting from his father, who inherited from his grandfather<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multilevel-Inheritance-in-Java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84346\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multilevel-Inheritance-in-Java.jpg\" alt=\"Multilevel Inheritance in Java\" width=\"490\" height=\"167\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multilevel-Inheritance-in-Java.jpg 490w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multilevel-Inheritance-in-Java-300x102.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multilevel-Inheritance-in-Java-150x51.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multilevel-Inheritance-in-Java-320x109.jpg 320w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/a><\/p>\n<p>This is an example of a multilevel inheritance where C inherits from B and B inherits from A.<\/p>\n<p><strong>Java program to illustrate the use of Multilevel Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javainheritance;\r\nclass GrandFather {\r\n  GrandFather() {\r\n    System.out.println(\"I am the grandfather!\");\r\n  }\r\n}\r\n\r\nclass Father extends GrandFather {\r\n  String familyName;\r\n  String houseaddress;\r\n  Father() {\r\n    System.out.println(\"I am the father! I inherit from Grandfather\");\r\n  }\r\n}\r\npublic class Son extends Father {\r\n\r\n  Son() {\r\n    System.out.println(\"I am the son and I inherit from my father.\");\r\n  }\r\n\r\n  public static void main(String[] args) {\r\n    Son s1 = new Son();\r\n\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">I am the grandfather!<br \/>\nI am the father! I inherit from Grandfather<br \/>\nI am the son and I inherit from my father.<\/div>\n<p>As you can see, when the base class is declared and when the object gets created, it calls the constructor of the base class. However, its parent class\u2019s constructor gets executed first. This is an important concept to understand.<\/p>\n<h4>Hierarchical Inheritance in Java<\/h4>\n<p>In this type of inheritance, a single parent class passes its values and methods to multiple child classes. One class serves as the parent class, and the rest of the classes are the child classes.<\/p>\n<p>It has a tree-like structure in which a parent class can have more than one child class, and it can go further. Whereas the super class at the top represents the root, and other sub-classes are referred to as branches.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hierarchical-Inheritance.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84345\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hierarchical-Inheritance.jpg\" alt=\"Hierarchical Inheritance in java\" width=\"328\" height=\"364\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hierarchical-Inheritance.jpg 328w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hierarchical-Inheritance-270x300.jpg 270w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hierarchical-Inheritance-135x150.jpg 135w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hierarchical-Inheritance-320x355.jpg 320w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><\/a><\/p>\n<p>This is an example of hierarchical inheritance where classes B and C inherit from parent class A.<\/p>\n<p><strong>Java program to illustrate Hierarchical Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javainheritance;\r\nclass Father {\r\n  String familyName;\r\n  String houseaddress;\r\n  Father() {\r\n    familyName = \"Programmer\";\r\n    houseaddress = \"Delhi\";\r\n  }\r\n}\r\nclass Son extends Father {\r\n  Son() {\r\n    System.out.println(\"I am the Son\");\r\n    System.out.println(\"My family name is \" + this.familyName + \" and I am from \" + this.houseaddress);\r\n\r\n  }\r\n\r\n}\r\nclass Daughter extends Father {\r\n  Daughter() {\r\n    System.out.println(\"I am the Daughter\");\r\n    System.out.println(\"My family name is \" + this.familyName + \" and I am from \" + this.houseaddress);\r\n  }\r\n}\r\nclass Main {\r\n  public static void main(String[] args) {\r\n    Son s = new Son();\r\n    Daughter d = new Daughter();\r\n  }\r\n}<\/pre>\n<p>Output:<\/p>\n<div class=\"code-output\">I am the Son<br \/>\nMy family name is Programmer and I am from Delhi<br \/>\nI am the Daughter<br \/>\nMy family name is Programmer and I am from Delhi<\/div>\n<h4>Multiple Inheritance in Java<\/h4>\n<p>Multiple Inheritance, as the name suggests, means that multiple child classes can derive from one parent class. It is not allowed in Java. However, it can be implemented by using Interfaces.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multiple-Inheritance.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84347\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multiple-Inheritance.jpg\" alt=\"Multiple Inheritance in java\" width=\"286\" height=\"363\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multiple-Inheritance.jpg 286w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multiple-Inheritance-236x300.jpg 236w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Multiple-Inheritance-118x150.jpg 118w\" sizes=\"auto, (max-width: 286px) 100vw, 286px\" \/><\/a><\/p>\n<p>This is an example of multiple inheritance in which class C is inheriting from classes A and B<\/p>\n<p><strong>Java program to illustrate the use of Multiple Inheritance in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javainheritance;\r\ninterface first {\r\n  public void firstmethod();\r\n}\r\ninterface second {\r\n  public void secondmethod();\r\n}\r\ninterface third extends first,\r\nsecond {\r\n  public void mainmethod();\r\n}\r\n\r\npublic class MultInheritance implements third {\r\n  public void mainmethod() {\r\n    System.out.println(\"This is the main interface method\");\r\n  }\r\n  public void firstmethod() {\r\n    System.out.println(\"Hey I am the first method defined the first interface\");\r\n  }\r\n  public void secondmethod() {\r\n    System.out.println(\"Hey I am the second method defined it the second interface\");\r\n  }\r\n  public static void main(String[] args) {\r\n    MultInheritance ob = new MultInheritance();\r\n    ob.mainmethod();\r\n    ob.firstmethod();\r\n    ob.secondmethod();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is the main interface method<br \/>\nHey I am the first method defined the first interface<br \/>\nHey I am the second method defined it the second interface<\/div>\n<h4>Hybrid Inheritance in Java<\/h4>\n<p>It is a mix of two or more types of inheritance. However, to implement multiple inheritance inside a hybrid inheritance, you have to use interfaces, as Java does not support multiple inheritance.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hybrid-Inheritance.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84348\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hybrid-Inheritance.jpg\" alt=\"Hybrid Inheritance in java\" width=\"444\" height=\"381\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hybrid-Inheritance.jpg 444w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hybrid-Inheritance-300x257.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hybrid-Inheritance-150x129.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Hybrid-Inheritance-320x275.jpg 320w\" sizes=\"auto, (max-width: 444px) 100vw, 444px\" \/><\/a><\/p>\n<p>This is an example to show hybrid inheritance in Java, where there is a combination of two types of inheritance, i.e, Hierarchical and Multilevel.<\/p>\n<p><strong>Java program to illustrate the use of Hybrid Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javainheritance;\r\nclass A {\r\n  A() {\r\n    System.out.println(\"I am in class A! I have two children B and C\");\r\n  }\r\n}\r\nclass B extends A {\r\n  B() {\r\n    System.out.println(\"I am in class B! I have 1 child D\");\r\n  }\r\n\r\n}\r\nclass C extends A {\r\n  C() {\r\n    System.out.println(\"I am in class C. I am the child class of A\");\r\n  }\r\n}\r\nclass D extends B {\r\n  D() {\r\n    System.out.println(\"I am in D class which is the extension of B class\");\r\n  }\r\n}\r\nclass Main {\r\n  public static void main(String[] args) {\r\n    D d = new D(); \/\/This calls constructors of parent class A and B\r\n    B b = new B(); \/\/This calls constructors parent class A and B\r\n    C c = new C(); \/\/This calls the constructors class A and C\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">I am in class A! I have two children B and C<br \/>\nI am in class B! I have 1 child D<br \/>\nI am in the D class which is the extension of B class<br \/>\nI am in class A! I have two children B and C<br \/>\nI am in class B! I have 1 child D<br \/>\nI am in class A! I have two children B and C<br \/>\nI am in class C. I am the child class of A<\/div>\n<h3>Important Points Regarding Inheritance in Java<\/h3>\n<h4>1. Java Default Superclass<\/h4>\n<p>Every class in Java is a subclass of one and only one class in Java. There is one exception to this, and that is the Object class. The object class does not have any superclass.<\/p>\n<h4>2. No Multiple Inheritance in Java<\/h4>\n<p>In Java, as we already saw, a single class can inherit from one and only one class. Java does not support multiple inheritance. However, multiple inheritances are implemented by using interfaces, as we saw in the previous example. The reason for Java not supporting multiple inheritance is to avoid ambiguity. There is a popular confusion known as the Diamond problem.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/No-Multiple-Inheritance.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84349\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/No-Multiple-Inheritance.jpg\" alt=\"No Multiple Inheritance in java\" width=\"343\" height=\"382\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/No-Multiple-Inheritance.jpg 343w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/No-Multiple-Inheritance-269x300.jpg 269w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/No-Multiple-Inheritance-135x150.jpg 135w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/No-Multiple-Inheritance-320x356.jpg 320w\" sizes=\"auto, (max-width: 343px) 100vw, 343px\" \/><\/a><\/p>\n<p>Look at the figure above, where A is the parent class of B, C, and D. Both B and C derive from A, and D derives from B and C(Multiple inheritance).<\/p>\n<p>Suppose A has a method called speak(). Now, B and C both have these versions of speak, but they decide to overload the method speak() as per their requirements, Now if D wants to use the speak() method, which class\u2019s method should it use? B\u2019s or C\u2019s? There lies an ambiguity. That is the diamond problem; hence, Java does not allow multiple inheritance.<\/p>\n<h4>3. Constructor Inheritance in Java<\/h4>\n<p>Constructors are not inherited by the child classes. However, they are inherited with the help of the super() keyword.<\/p>\n<h4>4. Inheriting Private Members in Java<\/h4>\n<p>The subclass cannot access the private members of the parent class.<\/p>\n<p>Note that derived classes, or child classes, cannot access the private members of the parent class. However, if one of the methods of the parent class uses the private variables, then upon inheriting all the methods from the class, the child class can indirectly access the private variables of the parent class.<\/p>\n<h3>Real-life examples of Inheritance in Java<\/h3>\n<p>Suppose a Car has two functions: accelerate and brake. Now these functions have different values for different cars as every car has a different acceleration rate and braking mechanisms. So let us take the example of a Mercedes-Benz S-Class, which is a car. It inherits all the definitions of the class Car. However, its acceleration and braking systems are unique.<\/p>\n<p>Let us see a Java program that illustrates this concept.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javainheritance;\r\nclass Car {\r\n  public void Accelerate() {\r\n    System.out.println(\"All cars accelerate like this\");\r\n\r\n  }\r\n\r\n  public void Brake() {\r\n    System.out.println(\"All cars Brake like this\");\r\n\r\n  }\r\n}\r\nclass Mercedes extends Car {\r\n  public void Accelerate() {\r\n    System.out.println(\"The Mercedes Benz S-class accelerates like this!\");\r\n\r\n  }\r\n\r\n  public void Brake() {\r\n    System.out.println(\"The Mercedes Benz S-class brakes like this!\");\r\n\r\n  }\r\n  public static void main(String[] args) {\r\n    Mercedes c = new Mercedes();\r\n    c.Accelerate();\r\n    c.Brake();\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">The Mercedes Benz S-class accelerates like this!<br \/>\nThe Mercedes Benz S-class brakes like this!<\/div>\n<h3>Power of subclasses<\/h3>\n<p>There are many advantages of using subclasses for programming. Some of them are:<\/p>\n<ul>\n<li>The subclasses can have new fields that are not in the parent class.<\/li>\n<li>Subclasses can use the inherited fields directly.<\/li>\n<li>We can write instance methods, i.e, override the methods that have the same name as the superclass method. We can even add a different definition to the function.<\/li>\n<li>There is no need to declare separate objects for calling parent class methods.<\/li>\n<li>We can declare the method with the same signature as static, thus hiding the parent class method.<\/li>\n<li>We can define constructors that call the parent class constructor. If we want to do that explicitly, then we have to use the super keyword.<\/li>\n<li>The subclass can have methods that are not in the parent class, thus making it a better version of the parent class.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In this article, we learned about inheritance in Java and its use in programming. We also learned about the various types of inheritance and how to implement each one of them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before we begin to talk about inheritance in Java, let us understand a real-life situation here first. Imagine you have an older brother. He has to go off to college, and you get all&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":84343,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[4286,5633,5893,6422,7543,8930,8947,12905,15062],"class_list":["post-7265","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-example-of-inheritance-in-java","tag-hierarchical-inheritance-in-java","tag-how-inheritance-used-in-java","tag-hybrid-inheritance-in-java","tag-java-inheritance","tag-multilevel-inheritance-in-java","tag-multiple-inheritance-in-java","tag-single-inheritance-in-java","tag-types-of-inheritance-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Inheritance in Java with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"Inheritance in Java is an important topic in Object-Oriented Programming. Let&#039;s dive into the concept of inheritance with practical examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inheritance in Java with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Inheritance in Java is an important topic in Object-Oriented Programming. Let&#039;s dive into the concept of inheritance with practical examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-05T10:18:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-18T10:43:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inheritance in Java with Examples - DataFlair","description":"Inheritance in Java is an important topic in Object-Oriented Programming. Let's dive into the concept of inheritance with practical examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Inheritance in Java with Examples - DataFlair","og_description":"Inheritance in Java is an important topic in Object-Oriented Programming. Let's dive into the concept of inheritance with practical examples.","og_url":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-02-05T10:18:17+00:00","article_modified_time":"2026-05-18T10:43:31+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Inheritance in Java with Examples","datePublished":"2018-02-05T10:18:17+00:00","dateModified":"2026-05-18T10:43:31+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/"},"wordCount":1752,"commentCount":6,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1.jpg","keywords":["example of inheritance in java","Hierarchical Inheritance in Java","How inheritance used in Java","Hybrid Inheritance in Java","java inheritance","Multilevel Inheritance in Java","Multiple Inheritance in Java","Single Inheritance in Java","types of Inheritance in Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/inheritance-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/","url":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/","name":"Inheritance in Java with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1.jpg","datePublished":"2018-02-05T10:18:17+00:00","dateModified":"2026-05-18T10:43:31+00:00","description":"Inheritance in Java is an important topic in Object-Oriented Programming. Let's dive into the concept of inheritance with practical examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/inheritance-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Types-of-Inheritance-in-Java-1.jpg","width":1200,"height":628,"caption":"Types of Inheritance in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/inheritance-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Inheritance in Java with Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=7265"}],"version-history":[{"count":26,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7265\/revisions"}],"predecessor-version":[{"id":148348,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7265\/revisions\/148348"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/84343"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=7265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=7265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=7265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}