0% found this document useful (0 votes)
52 views3 pages

Method Overriding: Overload

The document discusses method overriding and overloading in Java. For method overriding, it states that the child class method must have the same name and signature as the parent class method. It also lists rules for overriding such as the overridden method must have the same or covariant return type but not a more restrictive access modifier. For method overloading, it explains that methods can have the same name but different parameters, and the compiler determines which version to call based on the arguments. It then gives examples of code for integer and double input/addition using the Scanner class in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views3 pages

Method Overriding: Overload

The document discusses method overriding and overloading in Java. For method overriding, it states that the child class method must have the same name and signature as the parent class method. It also lists rules for overriding such as the overridden method must have the same or covariant return type but not a more restrictive access modifier. For method overloading, it explains that methods can have the same name but different parameters, and the compiler determines which version to call based on the arguments. It then gives examples of code for integer and double input/addition using the Scanner class in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Method overriding

Child class must have same method of parent class


Class parent{
Void test(){
System.out.println(test);
}
}
Classchild{
Void test(){
System.out.println(child);
}
}
Now call it in main function and use it

1.

applies only to inherited methods

2.

object type (NOT reference variable type) determines which overridden


method will be used at runtime

3.

Overriding methods must have the same return type

4.

Overriding method must not have more restrictive access modifier

5.

Abstract methods must be overridden

6.

Static and final methods cannot be overridden

7.

Constructors cannot be overridden

8.

It is also known as Runtime polymorphism.

Method overloading
We use method again and again
class Overload
{

void demo (int a)


{
System.out.println ("a: " + a);
}
void demo (int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}

1.

To call an overloaded method in Java, it is must to use the type and/or number
of arguments to determine which version of the overloaded method to actually
call.

2.

Overloaded methods may have different return types; the return type alone is
insufficient to distinguish two versions of a method. .

3.

When Java encounters a call to an overloaded method, it simply executes the


version of the method whose parameters match the arguments used in the call.

4.

It allows the user to achieve compile time polymorphism.

5.

An overloaded method can throw different exceptions.

6.

It can have different access modifiers.

For integer value input


import java.util.Scanner;
class inta{
public static void main (String[] args) {
Scanner test =new Scanner(System.in);
System.out.print("Enter any Number=" );
int num = test.nextInt();
System.out.print("Enter any Number=" );

int num1 = test.nextInt();


int sum=num+num1;
System.out.println("the number is"+sum);
}
}
For Doulbe/float input value
import java.util.Scanner;
class inta{
public static void main (String[] args) {
Scanner test =new Scanner(System.in);
System.out.print("Enter any Number=" );
double num = test.nextDouble();
System.out.print("Enter any Number=" );
double num1 = test.nextDouble();
double sum=num+num1;
System.out.println("the number is"+sum);
}
}

You might also like