0% found this document useful (0 votes)
18 views8 pages

Java Presentation

The document explains method overloading and method overriding in object-oriented programming. Method overloading allows multiple methods with the same name but different parameters within the same class, while method overriding enables a subclass to provide a specific implementation of a method already defined in its superclass. Examples are provided for both concepts, illustrating their usage in Java programming.

Uploaded by

superiorsensai6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Java Presentation

The document explains method overloading and method overriding in object-oriented programming. Method overloading allows multiple methods with the same name but different parameters within the same class, while method overriding enables a subclass to provide a specific implementation of a method already defined in its superclass. Examples are provided for both concepts, illustrating their usage in Java programming.

Uploaded by

superiorsensai6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

METHOD

OVERLOADING AND
METHOD OVERRIDING
METHOD OVERLOADING
METHOD OVERLOADING

Method Overloading is a feature in object-oriented programming (OOP) languages


(like Java, C++, C#) that allows:
•Multiple methods to have the same name but different parameters within the
same class.
•The methods must differ in:
• Number of parameters
• Data types of parameters
• Order of parameters
EXAMPLE OF METHOD OVERLOADING

public class MethodOverloading {


// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Method to add two float numbers
public float add(float a, float b) {
return a + b;
}
// Method to add integers with default values if no arguments are passed
public int add() {
return 10 + 20; // Default values
}
public static void main(String[] args) {
MethodOverloading obj = new MethodOverloading();
// Calling method with integers
System.out.println("Sum of 5 and 10 (integers): " + obj.add(5, 10));
// Calling method with floats
System.out.println("Sum of 3.5 and 7.5 (floats): " + obj.add(3.5f, 7.5f));
// Calling method without arguments (using default values)
System.out.println("Sum with default values: " + obj.add());
}
}
METHOD OVERRIDING
METHOD OVERRIDING

Method Overriding is an object-oriented programming (OOP) concept where:


•A subclass (child class) provides a specific implementation of a method that
is already defined in its superclass (parent class).
•The overridden method in the subclass must have:
• The same name,
• The same return type, and
• The same parameters as the method in the parent class.
EXAMPLE OF METHOD OVERRIDING
//SuperClass

class Animal {

void makeSound() {

System.out.println("Animal makes a sound.");

// Subclass

class Dog extends Animal {

// Overrides the makeSound method of the superclass

@Override

void makeSound() {

System.out.println("Dog barks.");

}
EXAMPLE OF METHOD OVERRIDING

// Main class to test method overriding

public class Main {

public static void main(String[] args) {

// Creating instances of Dog and Cat

Animal dog = new Dog();

Animal cat = new Cat();

// Calling the overridden method

dog.makeSound(); // Output: Dog barks.

cat.makeSound(); // Output: Cat meows.

You might also like