Method Overriding in Java Example
Overview
An instance method in a child class that has the same method signature of the parent class is sail to be overriding the parent’s class method. Using this feature we can provide a child-specific implementation for the method. We can use the @Ovevrride annotation when we are overriding the method.
Method signature = access specifier <method return type> name of the method( method parameters)
Coding Listing
/** * */ package com.testingdocs.tutorial; /** * @author TestingDocs * */ public class Document { public void printDoc() { System.out.println("Print Parent Document..."); } }
Child class
package com.testingdocs.tutorial; public class TestPlan extends Document { @Override public void printDoc() { //Overridden method System.out.println("Print Child TestPlan..."); } public static void main(String[] args) { TestPlan tp=new TestPlan(); tp.printDoc(); } }
Screenshot
—
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :