What is the process of defining a method in a subclass having same name & type signature?

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.

In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding

  • Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
  • Method overriding is used for runtime polymorphism

Rules for Java Method Overriding

  1. The method must have the same name as in the parent class
  2. The method must have the same parameter as in the parent class.
  3. There must be an IS-A relationship (inheritance).
What is the process of defining a method in a subclass having same name & type signature?

Understanding the problem without method overriding

Let's understand the problem that we may face in the program if we don't use method overriding.

Test it Now

Output:

Problem is that I have to provide a specific implementation of run() method in subclass that is why we use method overriding.


Example of method overriding

In this example, we have defined the run method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method are the same, and there is IS-A relationship between the classes, so there is method overriding.

Test it Now

Output:


A real example of Java Method Overriding

Consider a scenario where Bank is a class that provides functionality to get the rate of interest. However, the rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate of interest.

What is the process of defining a method in a subclass having same name & type signature?

Java method overriding is mostly used in Runtime Polymorphism which we will learn in next pages.

Test it Now

Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9

Can we override static method?

No, a static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it later.


Why can we not override static method?

It is because the static method is bound with class whereas instance method is bound with an object. Static belongs to the class area, and an instance belongs to the heap area.

UGC-NET | UGC NET CS 2016 July – II | Question 14

Improve Article

Save Article

Like Article

  • Difficulty Level : Basic
  • Last Updated : 19 Apr, 2018

  • Read
  • Discuss
  • Courses
  • Practice
  • Video
  • Improve Article

    Save Article

    When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass _____ the method in the superclass.
    (A) Overloads
    (B) Friendships
    (C) Inherits
    (D) Overrides


    Answer: (D)

    Explanation:

    • When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass overrides the method in the superclass.
    • Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. Overloading is related to compile time (or static) polymorphism..
    • Friend Class A friend class can access private and protected members of other class in which it is declared as friend..
    • The capability of a class to derive properties and characteristics from another class is called Inheritance.

    So, option (D) is correct.


    Quiz of this Question

    My Personal Notes arrow_drop_up

    Save

    Please Login to comment...

    Recommended textbooks for you

  • Database System Concepts

    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan

    Publisher:McGraw-Hill Education

    Starting Out with Python (4th Edition)

    Digital Fundamentals (11th Edition)

  • C How to Program (8th Edition)

    Author:Paul J. Deitel, Harvey Deitel

    Database Systems: Design, Implementation, & Manag...

    Author:Carlos Coronel, Steven Morris

    Publisher:Cengage Learning

    Programmable Logic Controllers

    Author:Frank D. Petruzella

    Publisher:McGraw-Hill Education

  • What is the process of defining a method in a subclass having same name and same signature as a method in its superclass?

    Database System Concepts

    ISBN:9780078022159

    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan

    Publisher:McGraw-Hill Education

    What is the process of defining a method in a subclass having same name and same signature as a method in its superclass?

    Starting Out with Python (4th Edition)

    ISBN:9780134444321

    Author:Tony Gaddis

    Publisher:PEARSON

    What is the process of defining a method in a subclass having same name and same signature as a method in its superclass?

    Digital Fundamentals (11th Edition)

    ISBN:9780132737968

    Author:Thomas L. Floyd

    Publisher:PEARSON

    What is the process of defining a method in a subclass having same name and same signature as a method in its superclass?

    C How to Program (8th Edition)

    ISBN:9780133976892

    Author:Paul J. Deitel, Harvey Deitel

    Publisher:PEARSON

    What is the process of defining a method in a subclass having same name and same signature as a method in its superclass?

    Database Systems: Design, Implementation, & Manag...

    ISBN:9781337627900

    Author:Carlos Coronel, Steven Morris

    Publisher:Cengage Learning

    What is the process of defining a method in a subclass having same name and same signature as a method in its superclass?

    Programmable Logic Controllers

    ISBN:9780073373843

    Author:Frank D. Petruzella

    Publisher:McGraw-Hill Education

    What is the process of defining a method in a subclass having same name & type signature?

    Explanation: When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass overrides the method in the superclass.

    What is the process of defining a method in subclass having same name and type signature Mcq?

    2. What is the process of defining a method in a subclass having same name & type signature as a method in its superclass? Explanation: None.

    What is the process of defining a function in a subclass?

    A subclass inherits the methods of its superclasses. In a subclass definition, you can override any instance method that the subclass inherits by defining an instance method with the same signature as the inherited method.

    What is the name of defining a method in a subclass Having Same name & type signature as a method in its superclass?

    Instance Methods An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.