File: Test.java
import java.io.*;
class Addition {
void add(int c, int d) {
System.out.println("The first ans is: " + (c + d));
}
void add(double c, double d) {
System.out.println("The second ans is: " + (c + d));
}
}
public class Test {
public static void main(String[] args) {
Addition obj = new Addition();
obj.add(20, 30);
obj.add(30.2, 40.4);
}
}
Output:
$ javac Test.java
$ java Test
The first ans is: 50
The second ans is: 70.6
When integers are passed as parameters the first add method is called which has integer data type as parameters and when decimal values are passed the second add method is called which has double data type as parameters.
File: Test.java
import java.io.*;
class Master {
public void fnct() {
System.out.println("Master Class");
}
}
class Servant extends Master {
//Overriding method
public void fnct() {
System.out.println("Servant Class");
}
}
public class Test {
public static void main(String args[]) {
Servant obj = new Servant();
obj.fnct();
}
}
Output:
$ javac Test.java
$ java Test
Servant Class
Servant
is the child class of Master
and the overridden method is fnct()
. obj
refers to an object of class Servant
, and when it is used to call the method fnct()
, the overridden method is executed from the class Servant
.
Help us improve this content by editing this page on GitHub