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

  1. Understanding the problem without method overriding
  2. Can we override the static method
  3. Method overloading vs. method overriding

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 and type?

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 and type?

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.


Can we override java main method?

No, because the main is a static method.


Difference between method Overloading and Method Overriding in java

Click me for the difference between method overloading and overriding


More topics on Method Overriding (Not For Beginners)

Method Overriding with Access Modifier

Let's see the concept of method overriding with access modifier.

Exception Handling with Method Overriding

Let's see the concept of method overriding with exception handling.

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

  • Academic Resource

    Aptitude Data Interpretation Verbal Reasoning Non Verbal Reasoning Verbal Ability Programming General Knowledge Puzzle

  • Engineering

    Computer Engineering Electronics and Communication Electrical Engineering Mechanical Engineering Civil Engineering Biotechnology Architecture & Planning

  • Online Test

    Aptitude Test Data Interpretation Test Verbal Reasoning Test Non Verbal Reasoning Test Verbal Ability Test

  • Exams
  • More

    Full Forms हिंदी


Home » JAVA Programming » Methods » Question


Methods

Easy Questions

JAVA Programming

Java Basic

Basic Syntax

Object & Classes

Basic Datatypes

Variable Types

Modifier Types

Basic Operators

Loop Control

Decision Making

Numbers

Characters

Strings

Arrays

Date & Time

Regular Expressions

Methods

Files and I/O

Exceptions

Inheritance

Polymorphism

Abstraction

Encapsulation

Interfaces

Packages

Data Structures

Collections

Generics

Serialization

Networking

Multithreading

Applet

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

    1. Method hiding
    2. Method overloading
    3. Method overriding
    4. Method overloading
    5. None of these

Correct Option: C

Method overriding

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


Your comments will be displayed only after manual approval.

What is the process of defining a method in a subclass having same name and 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 procedure for defining a method in a subclass with the same name and signature type as a method in its superclass?

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 more than one method with the same name in a class differentiated by method signature?

Explanation: Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number.

Is it possible for a subclass to define a method with the same name and parameters as a method defined by the base class?

A subclass can do more than that; it can define a method that has exactly the same method signature (name and argument types) as a method in its superclass. In that case, the method in the subclass overrides the method in the superclass and effectively replaces its implementation, as shown in Figure 6-3.