• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Java – Method Overriding

Contents

  • 1 Advantages of Method Overriding:
  • 2 Points to Remember
    • 2.1 Rules of Method Overriding:

A subclass changing the behavior of a method it inherited from its super class is called as Method Overriding.

While overriding a method, a subclass must define it with the same set of method parameters and return type as present in its super class.

package com.techstackjournal.methodoverriding;

public class Shape {

	public void draw(int thickness) {
		System.out.println("Preparing the canvas...");
	}

}
package com.techstackjournal.methodoverriding;

public class Rectangle extends Shape {

	public void draw(int thickness) {
		System.out.println("Drawing Rectangle...");
	}

	public void draw(int thickness, String fillColor) {
		System.out.println("Drawing Rectangle and Filling Color...");
	}

}
package com.techstackjournal.methodoverriding;

public class Application {

	public static void main(String[] args) {

		Rectangle rectangle = new Rectangle();

		rectangle.draw(1);

	}

}
Drawing Rectangle...
  • Here, we are overriding draw method of Shape class in Rectangle class by implementing it again in Rectangle class
  • Calling the draw method using Rectangle reference variable invokes draw method of Rectangle class

Advantages of Method Overriding:

  • Method overriding will allow the subclass to define the method that is specific to the subclass
  • By overriding a method instead of creating a new method a subclass can get benefits of Polymorphism

Points to Remember

Rules of Method Overriding:

  • A subclass must maintain the same set of method arguments and return type
  • If the return type is a class, then the return type can be a sub class of super class return type
  • A subclass cannot reduce the visibility of the inherited method from super class. That means, overriding a public method in super class as a private method in subclass is not legal
  • A subclass can override a method only when that method is visible to it.
  • If a subclass define a method with the same signature as of super class which has defined it as private, we do not call it as overriding.
  • You can increase the visibility of an inherited method from super class. That means, a protected method in super class can be defined as a public method in subclass.
  • A subclass cannot override a final method
  • A subclass cannot override a static method
  • Some additional rules related to exceptions, which we will see in later sections
    • A subclass cannot throw a broader exception than what is declared in the super class
    • But it can throw a narrower exception
    • Overriding method can throw any runtime exception, even if it is not defined in its declaration in super class

Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

  • Java Tutorial
    • Introduction
    • Install Java
    • First Java Program
    • Statements and Comments
    • Packaging the Classes
    • Variables
    • Primitive Data Types
    • Operators
    • if-else statement
    • Loops in Java
    • Arrays
    • break and continue
    • Switch Statement
    • Classes and Objects
    • Methods
    • Encapsulation
    • Constructor in Java: All You Need To Know
    • Inheritance
    • Access Modifiers
    • Static Variables and Methods
    • Final keyword in Java
    • Abstract Class
    • What is an Interface in Java?
    • Method Overloading
    • Java – Method Overriding
    • Polymorphism
    • Java – super Keyword
    • Nested Classes
    • Exception Handling
    • String Class

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in