Method Overriding in Java:
We studied about method overloading in Java.Now the next is Method Overriding in Java. Here the number of arguments and datatype and all are same.
- Used in parent class and child class.
- Parent class method is Overriding by Child class.
- number of arguments are same.
- datatype of arguments are also same.
- Method name is same.
- The example is when a method in a Interface have to behave differently in different scenarios.
Example:
package com.jobberindia;
public interface Test {
void performSomeTask(int a,int b);
}
package com.jobberindia;
import java.io.File;
import java.util.*;
public class HelloWorld implements Test{
@Override
public void performSomeTask(int a, int b) {
// TODO Auto-generated method stub
System.out.println("subtracting "+a+" from "+b+" is: "+(a-b));
}
public static void main(String[] args) {
Test t = new HelloWorld();
t.performSomeTask(55, 15);
}
}
No comments:
Post a Comment