What is Method Overriding | Java Object Oriented Programming | Runtime Polymorphism | IGNOU MCS024
Channel: PsychoTech Institute™
Date: 2023-11-11

Method overriding is a fundamental concept in object-oriented programming that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This enables a subclass to customize the behavior of inherited methods to suit its own requirements. Method overriding is a key feature of polymorphism in object-oriented programming. Here are the key points to understand about method overriding:

1. Method Signature:
   - Method overriding involves redefining a method in the subclass with the same name, return type, and parameter list as the method in the superclass.
   - The method signature consists of the method name and its parameter list.

2. Access Modifiers:
   - In the subclass, the overridden method should not have a more restrictive access modifier than the method in the superclass.
   - It is permissible to have a less restrictive access modifier (e.g., changing from `private` to `protected` or from `protected` to `public`).

3. Return Type:
   - The return type of the overridden method must be the same as, or a subtype of, the return type of the superclass method. In Java, covariant return types are allowed in overriding.

4. Method Overriding Rules:
   - The overridden method in the subclass must be a part of an inheritance hierarchy, with the subclass extending the superclass.
   - The method in the subclass must have the same name, parameter list, and return type as the method in the superclass.
   - The access modifier in the subclass must be the same or less restrictive than the one in the superclass.
   - You cannot override `final`, `static`, or `private` methods, as they cannot be inherited.

5. @Override Annotation:
   - In Java, it's a good practice to use the `@Override` annotation when overriding methods. It helps ensure that the method is intended to override a superclass method, and the compiler will give an error if the method is not correctly overridden.

6. Calling Superclass Method:
   - If you want to invoke the superclass's method from the overridden method in the subclass, you can use the `super` keyword followed by the method name.

Here's a simple example of method overriding in Java:

class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Dog(); // Polymorphic assignment
        myAnimal.makeSound(); // Calls the Dog class's overridden method
    }
}


In this example, the `makeSound` method is overridden in the `Dog` class. When an instance of `Dog` is assigned to a reference of the superclass `Animal`, and the `makeSound` method is called, it executes the `Dog` class's version of the method.

Method overriding allows you to create specialized behavior for subclasses while maintaining a common interface or method signature in the superclass. It is a powerful mechanism for achieving polymorphism and code extensibility in object-oriented programming.


Best reference books for Object Oriented Programming: https://amzn.to/47dYoZp 
Books for IGNOU BCS-031 C++ Programming: https://amzn.to/3FGbCT3
Books for MCS-024 IGNOU Java Programming: https://amzn.to/3u1pIfq 

Thank you for watching!

polymorphism overloading and overriding, polymorphism overloading and overriding in java, polymorphism method overriding in java, method overloading and overriding in java in hindi, method overloading and overriding in java, java 17, java project, 2d array in java, java for web development, how to use java in visual studio code, java main method explained, java asynchronous programming, java tutorial for beginners, java practice programs for beginners, handling exceptions in java, introduction to java programming, java programming full course, java fundamentals for beginners, what is object-oriented programming, object-oriented thinking, object-oriented programming java in hindi, object-oriented programming java playlist, difference between object-oriented and, oop concepts and java programming, java classes and constructors, java classes for beginners, java object-oriented programming tutorial, static class in java, getter and setter method in java, java methods explained, how to write a method in java, methods in java for beginners, java functions and methods, static method in java, java constructors and methods, understanding classes in java, static methods and variables in java, instance variables and methods in java, when to use static methods in java, Java for JavaScript developers, Java for front-end development tutorial, How to use Eclipse for Java programming


#psychotech #PsychoTechInstitute #java #programming #objectorientedprogramming #methods #methodoverriding #methodoverloading