TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java

Java Method Overriding

Overview

In this tutorial, we will learn Java method overriding. Method overriding allows subclasses to have different implementation for the parent class non-final methods.

Do not get confused with Method overloading.

https://www.testingdocs.com/java-method-overloading/

Java Method Overriding

Overridden method in the child class should have the same name and method signature as that of the super class. However, overridden method can provide different implementation code for the method.

We can use the @Override annotation to indicate that the child method declaration is intended to override a method declaration in a super class/ classes.

Example

A simple example to understand the method overriding concept.

public class BaseClass {

 public void print() {
 System.out.println("This is Base class method");
 }
}



public class ChildClass extends BaseClass {

 public static void main(String[] args) {
 //Demo overridden method
 BaseClass b = new BaseClass();
 ChildClass c = new ChildClass();
 b.print();
 c.print();
 }

 @Override
 public void print() {
 System.out.println("This is Child class method");
 }

}

Sample Output

This is Base class method
This is Child class method

 

Notice how the child method overrides the parent class method. The same method print() works in different ways based on the object types. In the next example, we will look into a more real world example of method overriding.

final method

Note that we cannot override final methods of a class in the child class. We can use the final keyword in a method signature to indicate that the method cannot be overridden by the child classes.

We generally mark methods as final to avoid undesirable re-implementations of the method in the sub classes.

public final void finalMethod() {
System.out.println("Child classes cannot override "
+ "this method");
}

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Related Posts

Download Greenfoot Windows

Java /

Download & Install Greenfoot on Windows

Java /

Java Static Code Analysis

Java /

Java Testing Tools

Java /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java /

Exceptions in Java Programs

‹ Java Method Overloading› Java Arrays

Recent Posts

  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • RAPTOR Editions
  • Flowgorithm Conditional Breakpoint Statement
  • Flowgorithm Read Numbers from File Example
  • Search Text File Flowchart Example
  • Flowgorithm Turtle Graphics Symbols
  • Draw Circle using Flowgorithm Turtle
  • Draw Parallel Lines using Flowgorithm Graphics

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version