{"id":110639,"date":"2026-05-13T16:28:34","date_gmt":"2026-05-13T10:58:34","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=110639"},"modified":"2026-05-13T16:28:35","modified_gmt":"2026-05-13T10:58:35","slug":"overriding-in-java","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/overriding-in-java\/","title":{"rendered":"Overriding in Java : Simple Beginner&#8217;s Guide with Examples"},"content":{"rendered":"\n<p>When you learn Java, one of the first things you come across is inheritance. A child class can inherit methods from a parent class. But what if the child class wants to do something differently using the same method? That is exactly where overriding in Java comes in. It is a simple concept that makes your code flexible, clean, and powerful.<\/p>\n\n\n\n<p>This guide explains method overriding in Java from scratch with real examples, rules, and tips that every beginner needs to know before writing object-oriented Java code.<\/p>\n\n\n\n<p><strong>Quick Answer<\/strong><\/p>\n\n\n\n<p>Overriding in Java means a child class redefines a method that already exists in its parent class, using the same name, same parameters, and same return type. When you call that method on a child class object, the child&#8217;s version runs instead of the parent&#8217;s.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is a Parent Class and Child Class<\/strong><\/h2>\n\n\n\n<p>Before understanding overriding, you need to know these two terms.<\/p>\n\n\n\n<p><strong>Parent Class (Superclass):<\/strong><\/p>\n\n\n\n<ul>\n<li>The original class that has the method<\/li>\n\n\n\n<li>Also called superclass or base class<\/li>\n\n\n\n<li>Example: Animal is a parent class with a method sound()<\/li>\n<\/ul>\n\n\n\n<p><strong>Child Class (Subclass):<\/strong><\/p>\n\n\n\n<ul>\n<li>A class that inherits from the parent class using the extends keyword<\/li>\n\n\n\n<li>Also called subclass or derived class<\/li>\n\n\n\n<li>Example: Dog extends Animal and wants its own version of sound()<\/li>\n<\/ul>\n\n\n\n<p>Inheritance means the child class gets all the public and protected methods of the parent class automatically. Overriding means the child class replaces one of those inherited methods with its own version.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is Overriding in Java<\/strong><\/h2>\n\n\n\n<p>Method overriding in <a href=\"https:\/\/www.guvi.in\/blog\/introduction-to-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a> means the child class writes its own version of a method that was already defined in the parent class. Both versions have the same name, same parameters, and same return type. But they do different things.<\/p>\n\n\n\n<p>When you create an object of the child class and call that method, Java runs the child&#8217;s version, not the parent&#8217;s. This is called runtime <a href=\"https:\/\/www.guvi.in\/blog\/what-is-polymorphism-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">polymorphism<\/a> because Java decides which version to run at the time the program is running.<\/p>\n\n\n\n<p><strong>Simple real-life example:<\/strong><\/p>\n\n\n\n<p>Think of it like a recipe passed down from parent to child. The parent has a recipe for making tea. The child inherits it but makes their own version with ginger and lemon. The dish is still called &#8220;making tea&#8221; but the steps are different.<\/p>\n\n\n\n<p>Do check out HCL GUVI\u2019s <a href=\"https:\/\/www.guvi.in\/courses\/programming\/java-beginners\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=overriding-in-java-simple-beginners-guide-with-examples\" target=\"_blank\" rel=\"noreferrer noopener\">Java Course for Beginners<\/a> if you want to learn core Java concepts like method overriding, inheritance, polymorphism, OOP principles, and real-world Java programming from scratch. This beginner-friendly course includes hands-on coding exercises and practical examples to help you build a strong foundation in Java development.\u00a0<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Overriding in Java: Basic Example<\/strong><\/h3>\n\n\n\n<p><strong>Parent class:<\/strong><\/p>\n\n\n\n<p>Java code<\/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<\/code><\/pre>\n\n\n\n<p><strong>Child class overriding the method:<\/strong><\/p>\n\n\n\n<p>Java code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog extends Animal {\n    @Override\n    void sound() {\n        System.out.println(\"Dog barks\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Main class:<\/strong><\/p>\n\n\n\n<p>Java code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n    public static void main(String&#91;] args) {\n        Dog d = new Dog();\n        d.sound();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong> Dog barks<\/p>\n\n\n\n<p>Even though Dog inherits sound() from Animal, its own version runs because it overrides it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is @Override Annotation<\/strong><\/h2>\n\n\n\n<p>You may have noticed @Override written above the method in the child class. This is called an annotation. It is not compulsory but it is highly recommended.<\/p>\n\n\n\n<p><strong>What @Override does:<\/strong><\/p>\n\n\n\n<ul>\n<li>Tells the Java compiler that you intend to override a method<\/li>\n\n\n\n<li>If you accidentally change the method name or parameters, the compiler throws an error and stops you<\/li>\n\n\n\n<li>Makes your code easier to read and understand for others<\/li>\n<\/ul>\n\n\n\n<p>Always use @Override when overriding a method. It is a best practice in Java development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Rules of Overriding in Java<\/strong><\/h2>\n\n\n\n<p>These rules must be followed. Breaking any of them will cause an error or the method will not override correctly.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Rule<\/strong><\/td><td><strong>Details<\/strong><\/td><\/tr><tr><td>Same method name<\/td><td>The child method must have the exact same name as the parent method<\/td><\/tr><tr><td>Same parameters<\/td><td>The number, type, and order of parameters must be identical<\/td><\/tr><tr><td>Same or compatible return type<\/td><td>Return type must match or be a subtype of the parent&#8217;s return type<\/td><\/tr><tr><td>Access level cannot be reduced<\/td><td>If parent method is public, child cannot make it protected or private<\/td><\/tr><tr><td>Cannot override static methods<\/td><td>Static methods belong to the class, not the object<\/td><\/tr><tr><td>Cannot override private methods<\/td><td>Private methods are not inherited, so they cannot be overridden<\/td><\/tr><tr><td>Cannot override final methods<\/td><td>A method marked final in the parent cannot be changed in the child<\/td><\/tr><tr><td>Must have IS-A relationship<\/td><td>Overriding only works when the child class extends the parent class<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Access Modifier Rules in Overriding<\/strong><\/h2>\n\n\n\n<p>The child class can keep the same access level or make it more open. It cannot make it more restrictive.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Parent Method Access<\/strong><\/td><td><strong>Allowed in Child<\/strong><\/td><\/tr><tr><td>public<\/td><td>public only<\/td><\/tr><tr><td>protected<\/td><td>protected or public<\/td><\/tr><tr><td>default (no modifier)<\/td><td>default, protected, or public<\/td><\/tr><tr><td>private<\/td><td>Cannot be overridden<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Example of a mistake beginners make:<\/strong><\/p>\n\n\n\n<p>Java code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    public void sound() {\n        System.out.println(\"Animal makes a sound\");\n    }\n}\n\nclass Dog extends Animal {\n    @Override\n    protected void sound() { \/\/ ERROR: reducing access from public to protected\n        System.out.println(\"Dog barks\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>This will cause a compile-time error. Never reduce access level in an overriding method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using super to Call the Parent Method<\/strong><\/h2>\n\n\n\n<p>Sometimes you want to run the parent&#8217;s version of the method along with the child&#8217;s version. You can do that using the super keyword.<\/p>\n\n\n\n<p>Java code<\/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    @Override\n    void sound() {\n        super.sound(); \/\/ calls parent method first\n        System.out.println(\"Dog barks\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong> Animal makes a sound Dog barks<\/p>\n\n\n\n<p><strong>When to use super:<\/strong><\/p>\n\n\n\n<ul>\n<li>When you want to keep the parent&#8217;s behavior and add something extra on top of it<\/li>\n\n\n\n<li>Common in framework development and extending library classes<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Overriding vs Overloading: What Is the Difference<\/strong><\/h2>\n\n\n\n<p>Beginners often confuse overriding with overloading. They are completely different things.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Feature<\/strong><\/td><td><strong>Overriding<\/strong><\/td><td><strong>Overloading<\/strong><\/td><\/tr><tr><td>Definition<\/td><td>Child redefines parent&#8217;s method<\/td><td>Same class has multiple methods with same name but different parameters<\/td><\/tr><tr><td>Class involved<\/td><td>Two classes (parent and child)<\/td><td>Same class<\/td><\/tr><tr><td>Parameters<\/td><td>Must be identical<\/td><td>Must be different<\/td><\/tr><tr><td>Return type<\/td><td>Must match<\/td><td>Can be different<\/td><\/tr><tr><td>When decided<\/td><td>At runtime<\/td><td>At compile time<\/td><\/tr><tr><td>Inheritance needed<\/td><td>Yes<\/td><td>No<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Overloading example (same class, different parameters):<\/strong><\/p>\n\n\n\n<p>Java code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Calculator {\n    int add(int a, int b) { return a + b; }\n    int add(int a, int b, int c) { return a + b + c; }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Overriding example (child redefines parent method):<\/strong><\/p>\n\n\n\n<p>Java code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    void sound() { System.out.println(\"Some sound\"); }\n}\nclass Cat extends Animal {\n    @Override\n    void sound() { System.out.println(\"Meow\"); }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Real-World Example: Multiple Animals<\/strong><\/h2>\n\n\n\n<p>This example shows how overriding makes code clean and practical when dealing with multiple subclasses.<\/p>\n\n\n\n<p>Java code<\/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    @Override\n    void sound() {\n        System.out.println(\"Dog barks\");\n    }\n}\n\nclass Cat extends Animal {\n    @Override\n    void sound() {\n        System.out.println(\"Cat meows\");\n    }\n}\n\nclass Cow extends Animal {\n    @Override\n    void sound() {\n        System.out.println(\"Cow moos\");\n    }\n}\n\nclass Main {\n    public static void main(String&#91;] args) {\n        Animal a;\n\n        a = new Dog();\n        a.sound(); \/\/ Dog barks\n\n        a = new Cat();\n        a.sound(); \/\/ Cat meows\n\n        a = new Cow();\n        a.sound(); \/\/ Cow moos\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>Notice that the variable a is of type Animal but it holds different subclass objects. Java figures out which sound() to call at runtime based on the actual object. This is runtime polymorphism in action.<\/p>\n\n\n\n<p>Do check out HCL GUVI\u2019s <a href=\"https:\/\/www.guvi.in\/zen-class\/java-full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=overriding-in-java-simple-beginners-guide-with-examples\" target=\"_blank\" rel=\"noreferrer noopener\">Java Full Stack Development Course<\/a> if you want to master Java, Spring Boot, backend development, APIs, databases, and full stack application building with hands-on projects and live mentor support. This industry-focused program helps beginners and working professionals build strong real-world development skills and become job-ready for full stack developer roles.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Cannot Be Overridden in Java<\/strong><\/h2>\n\n\n\n<ul>\n<li><strong>Static methods:<\/strong> Static methods belong to the class itself, not to objects. They can be hidden but not overridden.<\/li>\n\n\n\n<li><strong>Private methods:<\/strong> Private methods are not inherited, so the child class has no access to them.<\/li>\n\n\n\n<li><strong>Final methods:<\/strong> The final keyword locks a method. It cannot be changed in any subclass.<\/li>\n\n\n\n<li><strong>Constructors:<\/strong> <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-constructors-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">Constructors<\/a> are not methods and cannot be overridden. They can only be called using super().<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Tips for Beginners<\/strong><\/h2>\n\n\n\n<ul>\n<li><strong>Always write @Override:<\/strong> It saves you from silent bugs where you think you are overriding but actually you created a new method with a typo in the name.<\/li>\n\n\n\n<li><strong>Remember the three musts:<\/strong> Same name, same parameters, same return type. If any of these is different, it is not overriding.<\/li>\n\n\n\n<li><strong>Do not reduce access:<\/strong> If the parent method is public, keep it public in the child.<\/li>\n\n\n\n<li><strong>Use super when needed:<\/strong> If you want both the parent and child behavior, call super.methodName() inside the overriding method.<\/li>\n\n\n\n<li><strong>Test with parent reference:<\/strong> Try assigning a child object to a parent reference variable. The overriding method should still run from the child class. If it does, your overriding is working correctly.<\/li>\n<\/ul>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px; margin: 22px auto;\">\n  <h3 style=\"margin-top: 0; font-size: 22px; font-weight: 700; color: #ffffff;\">\ud83d\udca1 Did You Know?<\/h3>\n  <ul style=\"padding-left: 20px; margin: 10px 0;\">\n    <li>Overriding in Java is the foundation of runtime polymorphism, which is one of the four pillars of object-oriented programming alongside encapsulation, inheritance, and abstraction.<\/li>\n    <li>The @Override annotation was introduced in Java 5 (released in 2004) and has been a recommended best practice ever since.<\/li>\n    <li>Method overriding allows Java applications to achieve dynamic method dispatch, enabling flexible and reusable code in large-scale software development.<\/li>\n  <\/ul>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Overriding in Java is not just a concept for exams. It is something you will use every single time you work with <a href=\"https:\/\/www.guvi.in\/blog\/what-is-inheritance-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">inheritance<\/a> in a real project. Whether you are building an app, working on a framework, or writing business logic, overriding lets you customize behavior without rewriting code from scratch.<\/p>\n\n\n\n<p>Start by practicing the Animal and Dog example. Then try creating your own parent and child classes. Override two or three methods and call them using a parent reference to see runtime polymorphism in action. Once that clicks, move on to using super, then explore abstract classes where overriding becomes mandatory.<\/p>\n\n\n\n<p>Understanding overriding in Java deeply will make inheritance, polymorphism, and design patterns all much easier to learn.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1778648461473\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>1. What is overriding in Java in simple words?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Overriding in Java means a child class writes its own version of a method that already exists in the parent class. When you call that method on a child object, the child&#8217;s version runs instead of the parent&#8217;s version.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1778648483780\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>2. What are the rules of overriding in Java?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The overriding method must have the same name, same parameters, and same or compatible return type as the parent method. The access level cannot be reduced. Static, private, and final methods cannot be overridden.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1778648502274\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>3. What is the difference between overriding and overloading in Java?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Overriding happens between a parent and child class where the child redefines an inherited method with the same signature. Overloading happens in the same class where multiple methods share the same name but have different parameters.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1778648521441\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>4. Why do we use @Override in Java?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>@Override tells the compiler that you intend to override a method. If your method signature does not match the parent method, the compiler gives an error. It prevents silent bugs and makes code easier to read.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1778648541360\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>5. Can we override static and private methods in Java?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No. Static methods are not overridden, they are hidden. Private methods are not inherited by the child class, so they cannot be overridden at all. Only public and protected instance methods can be overridden.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>When you learn Java, one of the first things you come across is inheritance. A child class can inherit methods from a parent class. But what if the child class wants to do something differently using the same method? That is exactly where overriding in Java comes in. It is a simple concept that makes [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":110709,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[720],"tags":[],"views":"23","authorinfo":{"name":"Jebasta","url":"https:\/\/www.guvi.in\/blog\/author\/jebasta\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/05\/overriding-in-Java-300x115.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/05\/overriding-in-Java-scaled.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/110639"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=110639"}],"version-history":[{"count":2,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/110639\/revisions"}],"predecessor-version":[{"id":110712,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/110639\/revisions\/110712"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/110709"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=110639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=110639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=110639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}