The Concept of Method Overriding in Java Programming
    • UG Programs

      Information Technology

      5

    • PG Programs

      Fashion Designings

      1

    • PG Programs

      Architecture and Planning

      0

    • PG Programs

      Performing and Fine Arts

      2

    • PG Programs

      Philosophy and Research

      2

    • PG Programs

      Pharmaceutics Science

      6

    • PG Programs

      Law Studies

      9

    • PG Programs

      Agricultural

      4

    • PG Programs

      Applied Sciences

      6

    • PG Programs

      Hotel & Tourism Management

      1

    • PG Programs

      Computer Science & Applications

      6

    • PG Programs

      Physical Education and Sports

      0

    • PG Programs

      Journalism and Mass Communication

      6

    • PG Programs

      Social Science and Humanities

      2

    • PG Programs

      Health Sciences

      5

    • PG Programs

      Commerce and Management

      19

    • UG Programs

      Architecture & Planning

      3

    • PG Programs

      Engineering & Technology

      29

    • UG Programs

      Performing & Fine Arts

      9

    • UG Programs

      Philosophy & Research

      1

    • UG Programs

      Computer Science And Applications

      11

    • UG Programs

      Fashion Designing

      6

    • UG Programs

      Journalism & Mass Communication

      11

    • UG Programs

      Hospitality & Tourism Management

      8

    • UG Programs

      Physical Education & Sports

      3

    • UG Programs

      Social Science & Humanities

      16

    • UG Programs

      Pharmaceutical Science

      17

    • UG Programs

      Applied Science

      16

    • UG Programs

      Legal Studies

      23

    • UG Programs

      Agriculture

      13

    • UG Programs

      Health Science

      19

    • UG Programs

      Commerce & Management

      50

    • UG Programs

      Engineering and Technology

      81

  • 0 Courses

    Royal University Online

    38 Courses

    Galgotias University Online

    19 Courses

    Sushant University (Formerly Ansal University), Gurgaon Online

    21 Courses

    MAHARISHI MARKANDESHWAR UNIVERSITY Online

    15 Courses

    Rayat Bahra University Online

    36 Courses

    NIILM University, Kaithal, Haryana Online

    15 Courses

    Kalinga University Online

    30 Courses

    OM Sterling Global University Online

    9 Courses

    MVN University Online

    28 Courses

    Noida International University Online

    12 Courses

    Bennett University Online

    23 Courses

    GD Goenka University, Gurugram Online

    22 Courses

    Sanskriti university mathura Online

    4 Courses

    IMT Faridabad Online

    11 Courses

    Rawal Institution and Technology Online

    17 Courses

    Lingaya's Vidyapeeth Online

The Concept of Method Overriding in Java Programming


Ankit Singh

Apr 7, 2023
The Concept of Method Overriding in Java Programming
Java is an object-oriented programming language that allows developers to create classes and objects to build software applications. One of the fundamental concepts of Java programming is method overriding, which is the ability of a subclass to provide its own implementation of a method that is already defined in its parent class. In this article, we will explore the concept of method overriding in Java programming, its syntax, rules, benefits, and limitations.




What is method overriding in Java?

Method overriding is a feature of object-oriented programming languages, including Java, that allows a subclass to provide its own implementation of a method that is already defined in its parent class. In other words, when a subclass inherits a method from its parent class, it can modify the behavior of that method by providing its own implementation of that method. The overridden method in the parent class is replaced by the method in the subclass during runtime.

Syntax of method overriding

To override a method in Java, the subclass must provide its own implementation of that method with the same name, return type, and parameters as the method in the parent class. The syntax of method overriding in Java is as follows:


java

Copy code

class Parent { public void method() { // parent class method implementation } } class Child extends Parent { public void method() { // child class method implementation } }


In this example, the Child class is overriding the method() of the Parent class.

Rules of method overriding

Method overriding in Java follows the following rules:

1. The method in the subclass must have the same name, return type, and parameters as the method in the parent class.


2. The access level of the method in the subclass cannot be more restrictive than the access level of the method in the parent class. For example, if the method in the parent class is public, the method in the subclass cannot be private.


3. The method in the subclass can throw a checked exception that is a subclass of the checked exception thrown by the method in the parent class, or it can throw an unchecked exception.


4. The method in the subclass cannot throw a checked exception that is broader than the checked exception thrown by the method in the parent class.

Benefits of method overriding

Method overriding provides the following benefits in Java programming:


1. It allows a subclass to modify the behavior of a method inherited from its parent class.


2. It enhances code reusability, as the parent class can be used as a blueprint for creating multiple subclasses with similar behaviors.


3. It promotes the principle of polymorphism, which is one of the fundamental concepts of object-oriented programming.

When to use method overriding

Method overriding should be used when we want to modify the behavior of a method inherited from the parent class. It is commonly used in object-oriented programming to implement polymorphism, which is the ability of objects to take on multiple forms.

Conclusion

Method overriding is a powerful feature of Java programming that allows a subclass to modify the behavior of a method inherited from its parent class. It enhances code reusability, promotes polymorphism, and provides a way to implement dynamic binding. However, it has some limitations, such as it can only override non-final and non-private methods of the parent class. In summary, method overriding is an essential concept for Java developers to understand and implement in their software applications.


Frequently Asked Questions (FAQs)


Q. What is the difference between method overloading and method overriding in Java?

Method overloading is a feature of Java programming that allows a class to have multiple methods with the same name but different parameters. Method overriding, on the other hand, is a feature that allows a subclass to modify the behavior of a method inherited from its parent class. In method overloading, the methods have the same name but different signatures, while in method overriding, the methods have the same name and the same signatures.


Q. Can a subclass call the method of its parent class without overriding it?

Yes, a subclass can call the method of its parent class without overriding it by using the super keyword followed by the method name. This is useful when we want to execute the method of the parent class before or after the execution of the method of the subclass.


Q. How does method overriding promote the principle of polymorphism in Java programming?

Method overriding promotes the principle of polymorphism in Java programming by allowing objects of different classes to be treated as objects of a common parent class. When a method is overridden in a subclass, the subclass object can be used in place of the parent class object, and the overridden method will be called instead of the parent class method. This enables the code to be more flexible, reusable, and maintainable.



Mappen is a tech-enabled education platform that provides IT courses with 100% Internship and Placement support. Mappen provides both Online classes and Offline classes only in Faridabad.


It provides a wide range of courses in areas such as Artificial Intelligence, Cloud Computing, Data Science, Digital Marketing, Full Stack Web Development, Block Chain, Data Analytics, and Mobile Application Development. Mappen, with its cutting-edge technology and expert instructors from Adobe, Microsoft, PWC, Google, Amazon, Flipkart, Nestle and Info edge is the perfect place to start your IT education.

Mappen in Faridabad provides the training and support you need to succeed in today's fast-paced and constantly evolving tech industry, whether you're just starting out or looking to expand your skill set.


There's something here for everyone. Mappen provides the best online courses as well as complete internship and placement assistance.

Keep Learning, Keep Growing.


If you are confused and need Guidance over choosing the right programming language or right career in the tech industry, you can schedule a free counselling session with Mappen experts.

Hey it's Sneh!

What would i call you?

Great !

Our counsellor will contact you shortly.