{"id":5255,"date":"2021-03-23T16:06:20","date_gmt":"2021-03-23T16:06:20","guid":{"rendered":"https:\/\/ded9.com\/?p=5255"},"modified":"2026-02-09T08:29:56","modified_gmt":"2026-02-09T08:29:56","slug":"inheritance-training-in-java-in-quite-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/inheritance-training-in-java-in-quite-simple-language\/","title":{"rendered":"Java Inheritance Explained in Simple Terms"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Inheritance is one of the main features of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming\" target=\"_blank\" rel=\"noopener\">OOP<\/a> (object-oriented programming) that allows us to define a new class from an existing class.<\/span><\/p>\n<p>For example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Animal<\/li>\n<li dir=\"ltr\">{<\/li>\n<li dir=\"ltr\">\/\/ eat () method<\/li>\n<li dir=\"ltr\">\/\/ sleep () method<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Dog extends Animal<\/li>\n<li dir=\"ltr\">{<\/li>\n<li dir=\"ltr\">\/\/ bark () method<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>In Java, we use the extends keyword to inherit from a class. Here, the Dog class inherits from the Animal class.<\/p>\n<p>Animal is a superclass (parent or base class), and dog is a Dog subclass (child or derivative class). The subclass inherits features and methods from the superclass.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50152\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-java-inheritance-introduction-.png\" alt=\"In Java, we use the extends keyword to inherit a class.\u00a0Here, the Dog class inherits from the Animal class.\" width=\"573\" height=\"711\" data-ll-status=\"loaded\" \/><\/p>\n<p>It is important to note that the subclass inherited only visible (available) members.<\/p>\n<h2><span style=\"font-size: 18pt;\">Is-a relationship<\/span><\/h2>\n<p>Inheritance is an is-a relationship.\u00a0We use inheritance only if there is an is-a relationship between the two classes.<\/p>\n<p>Here are some examples:<\/p>\n<ul>\n<li>A car is a vehicle.<\/li>\n<li>An orange is a fruit.<\/li>\n<li>The surgeon is a doctor.<\/li>\n<li>The dog is a Dogl.<\/li>\n<\/ul>\n<p><strong>Example 1: Inheritance in Java<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Animal {<\/li>\n<li dir=\"ltr\">public void eat () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI can eat\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void sleep () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI can sleep\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Dog extends Animal {<\/li>\n<li dir=\"ltr\">public void bark () {<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;I can bark&#8221;);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Dog dog1 = new Dog ();<\/li>\n<li dir=\"ltr\">dog1.eat ();<\/li>\n<li dir=\"ltr\">dog1.sleep ();<\/li>\n<li dir=\"ltr\">dog1.bark ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">I can eat<\/p>\n<p dir=\"ltr\">I can sleep<\/p>\n<p dir=\"ltr\">I can bark<\/p>\n<\/blockquote>\n<p>Here, we inherited the Dog class from the Animal superclass.\u00a0The Dog class inherits the eat () and sleep methods from the Animal class.<\/p>\n<p>Hence, Dog class objects can invoke Dog class and Animal class methods.<\/p>\n<p><img decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50153\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-java-working-inheritance-png-1.png\" alt=\"Hence, Dog class objects can invoke Dog class and Animal class methods.\" width=\"607\" height=\"525\" data-ll-status=\"loaded\" \/><\/p>\n<h4><strong>Example 2: protected keyword<\/strong><\/h4>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Animal {<\/li>\n<li dir=\"ltr\">protected String type;<\/li>\n<li dir=\"ltr\">private String color;<\/li>\n<li dir=\"ltr\">public void eat () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI can eat\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void sleep () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI can sleep\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public String getColor () {<\/li>\n<li dir=\"ltr\">return color;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void setColor (String col) {<\/li>\n<li dir=\"ltr\">color = col;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Dog extends Animal {<\/li>\n<li dir=\"ltr\">public void displayInfo (String c) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI am a\u201d + type);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cMy color is\u201d + c);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void bark () {<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;I can bark&#8221;);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Dog dog1 = new Dog ();<\/li>\n<li dir=\"ltr\">dog1.eat ();<\/li>\n<li dir=\"ltr\">dog1.sleep ();<\/li>\n<li dir=\"ltr\">dog1.bark ();<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">dog1.type = \u201cmammal\u201d;<\/li>\n<li dir=\"ltr\">dog1.setColor (\u201cblack\u201d);<\/li>\n<li dir=\"ltr\">dog1.displayInfo (dog1.getColor ());<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">I can eat<\/p>\n<p dir=\"ltr\">I can sleep<\/p>\n<p dir=\"ltr\">I can bark<\/p>\n<p dir=\"ltr\">I am a mammal<\/p>\n<p dir=\"ltr\">My color is black<\/p>\n<\/blockquote>\n<p>In the example above, the Animal class has a private color property. Hence, it is only visible in animals. Therefore, the Dog subclass cannot inherit it.<\/p>\n<p>We used the general getColor () and setColor () methods inside the Animal superclass to access the color attribute.<\/p>\n<p>Also, pay attention to the protected keyword. Protected is an access-level regulator, just like public and private.<\/p>\n<p>Protected members are also visible in a package and subclass.\u00a0In the example above, the Animal superclass has a type attribute that is declared protected.\u00a0Hence, the Dog subclass can access it.<\/p>\n<h2><span style=\"font-size: 18pt;\">Overriding Method<\/span><\/h2>\n<p>The above examples show that objects in a subclass can access superclass methods and define their strategies.<\/p>\n<p>What happens if the same method is defined in the super and subclass?<\/p>\n<p>In this case, the methods in the subclass reject the methods in the superclass. For example,<\/p>\n<p><strong>Example 3: Example of the overriding method<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Animal {<\/li>\n<li dir=\"ltr\">protected String type = \u201canimal\u201d;<\/li>\n<li dir=\"ltr\">public void eat () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI can eat\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void sleep () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI can sleep\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Dog extends Animal {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\u00a0 @Override<\/li>\n<li dir=\"ltr\">public void eat () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI eat dog food\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void bark () {<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;I can bark&#8221;);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Dog dog1 = new Dog ();<\/li>\n<li dir=\"ltr\">dog1.eat ();<\/li>\n<li dir=\"ltr\">dog1.sleep ();<\/li>\n<li dir=\"ltr\">dog1.bark ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">I eat dog food<\/p>\n<p dir=\"ltr\">I can sleep<\/p>\n<p dir=\"ltr\">I can bark<\/p>\n<\/blockquote>\n<p>Here, the eat () method exists in the Animal and Dog subclasses. We created the dog1 object from the Dog subclass.<\/p>\n<p>When we call the eat () method using the dog1 object, the method inside the Dog class is called, and the superclass method is not. This is called the overriding method.<\/p>\n<p>In the above program, we used the overriding method to tell the compiler we were overriding the technique. However, it is not mandatory.<\/p>\n<p>We use the super keyword if we need to call the eat () method of Animal from the Dog subclass.<\/p>\n<p><strong>Example 4: super keyword<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Animal {<\/li>\n<li dir=\"ltr\">public Animal () {<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;I am an Animal&#8221;);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void eat () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI can eat\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Dog extends Animal {<\/li>\n<li dir=\"ltr\">public Dog () {<\/li>\n<li dir=\"ltr\">super ();<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;I am a dog&#8221;);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">@Override<\/li>\n<li dir=\"ltr\">public void eat () {<\/li>\n<li dir=\"ltr\">super.eat ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cI eat dog food\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void bark () {<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;I can bark&#8221;);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Dog dog1 = new Dog ();<\/li>\n<li dir=\"ltr\">dog1.eat ();<\/li>\n<li dir=\"ltr\">dog1.bark ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">I am an Animal<\/p>\n<p dir=\"ltr\">I am a dog<\/p>\n<p dir=\"ltr\">I can eat<\/p>\n<p dir=\"ltr\">I eat dog food<\/p>\n<p dir=\"ltr\">I can bark<\/p>\n<\/blockquote>\n<p>We have used the super keyword to call the constructor using super (). Also, using the super. In the Eat () method, we used the Animal superclass&#8217;s eat () method.<\/p>\n<p>When using the constructor and method, consider the difference between using super.<\/p>\n<h2><span style=\"font-size: 18pt;\">Inheritance types<\/span><\/h2>\n<p>There are five types of inheritance:<\/p>\n<ul>\n<li>Single heirs &#8211; Class B inherits only from Class A.<\/li>\n<li>Multilevel Inheritance &#8211; Class B inherits from Class A, and Class C inherits from Class B.<\/li>\n<li>Hierarchical Inheritance &#8211; Class A is a superclass for classes B, C, and D.<\/li>\n<li>Multiple Inheritance &#8211; Class C inherits from interfaces A and B.<\/li>\n<li>Combined Inheritance &#8211; A combination of two or more types of inheritance.<\/li>\n<\/ul>\n<p>Java does not support multiple and hybrid class inheritance. But it can be used with the help of interfaces.<\/p>\n<h2><span style=\"font-size: 18pt;\">Why do we use inheritance?<\/span><\/h2>\n<p>The most important application is the ability to reuse <a href=\"https:\/\/ded9.com\/how-to-code-with-python-and-benefit-from-it\/\">code<\/a>.\u00a0The code in the parent class does not need to be rewritten in the child class.<\/p>\n<p>Another application is to use the overriding method to achieve runtime polymorphism.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is inheritance in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Inheritance in Java is a mechanism where one class acquires the properties (fields) and behaviors (methods) of another class. It supports the concept of \"is-a\" relationships, allowing for hierarchical class structures.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What are the types of inheritance in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Java supports several types of inheritance: Single Inheritance: A class inherits from one superclass. Multilevel Inheritance: A class is derived from another class, which is also derived from another class. Hierarchical Inheritance: Multiple classes inherit from a single superclass. Multiple Inheritance (through interfaces): A class implements multiple interfaces. Java does not support multiple inheritance through classes to avoid ambiguity.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why is inheritance important in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Inheritance promotes code reusability, allowing developers to write and maintain less code. It also helps in creating a natural hierarchy between classes, making the code more organized and easier to understand.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance is one of the main features of OOP (object-oriented programming) that allows us to define a new class from an existing class. For example, class Animal { \/\/ eat () method \/\/ sleep () method } class Dog extends Animal { \/\/ bark () method } In Java, we use the extends keyword to [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5256,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[11411,4318],"class_list":["post-5255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-code","tag-oop"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=5255"}],"version-history":[{"count":7,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5255\/revisions"}],"predecessor-version":[{"id":267001,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5255\/revisions\/267001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/5256"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=5255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=5255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=5255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}