Can a class have two methods with the same name but different in parameters called?

Can a class have two methods with the same name but different in parameters called?
Educative Answers Team

Overloading occurs when two or more methods in one class have the same method name but different parameters.

Overriding occurs when two methods have the same method name and parameters. One of the methods is in the parent class, and the other is in the child class. Overriding allows a child class to provide the specific implementation of a method that is already present in its parent class.​

The two examples below illustrate their differences:

The table below highlights their key differences:

  • Must have at least two methods by the same name in the class.

  • Must have a different number of parameters.

  • If the number of parameters is the same, then it must have different types of parameters.

  • Overloading is known as compile-time polymorphism.

  • Must have at least one method by the same name in both parent and child classes.

  • Must have the same number of parameters.

  • Must have the same parameter types.

  • Overriding is known as runtime polymorphism​.

Example codes

Overriding

Take a look at the code below:

class Dog{

public void bark(){

System.out.println("woof ");

}

}

class Hound extends Dog{

public void sniff(){

System.out.println("sniff ");

}

public void bark(){

System.out.println("bowl");

}

}

class OverridingTest{

public static void main(String [] args){

Dog dog = new Hound();

dog.bark();

}

}

In this overriding example, the dog variable is declared to be a Dog. During compile-time, the compiler checks if the Dog class has the bark() method. As long as the Dog class has the bark() method, the code compiles. At run-time, a Hound is created and assigned to dog, so, ​ it calls the bark() method of Hound.

Overloading

Take a look at the code below:

class Dog{

public void bark(){

System.out.println("woof ");

}

//overloading method

public void bark(int num){

for(int i=0; i<num; i++)

System.out.println("woof ");

}

}

In this overloading example, the two bark methods can be invoked using different parameters. The compiler knows that they are different because they have different method signatures​ (method name and method parameter list).

RELATED TAGS

overloading

overriding

classes

Copyright ©2022 Educative, Inc. All rights reserved

  • Remove From My Forums

  • Question

  • In Ruby, you can have two methods with the same name and same set of parameters. When executed, the recent version is compiled and executed.

    How C# handles this? Can I have two similar methods in C#?

Answers

  • In Ruby, classes are never closed, and you can extend a class with new methods. If a method happens to have the same signature as an already existing method, the method will be replaced.

    C# does not allow this. Although you can define partial classes in C#, the compiler handles them differently from Ruby: all the parts of the class are first collected into the final class, and then compiled. If you have two parts that both define the same method with the same signature, you will get a compilation error ("Type 'Foo' already defines a member called 'Bar' with the same parameter types").

    The difference is, that in Ruby, the extension is done at runtime, while in C#, the parts are collected at compile time. If you want to mimic something like the Ruby functionality, you could look at the Strategy pattern, for instance, which allows you to extend the functionality of your class at runtime.


    Hey, look! This system allows signatures of more than 60 cha

    • Marked as answer by Wednesday, August 22, 2012 1:20 AM

  • So this means, C# doesn't support method versioning.

    What do you call method versioning?

    If you mean changing the behavior of an inherited method, that's done by overriding the method: you write the method in the derived class and add the 'override' modifier.

    If you mean modifying a method dynamically, you can use a delegate field or property as though it was a method, and assign a different method to it at runtime:

    class ModifiableClass
    {
        public Func<int, int> SomeMethod { get; set; }
        public ModifiableClass()
        {
            SomeMethod = (int x) =>
            {
                return 2 * x;
            };
        }
    }
    class Program
    {
        public static void Main()
        {
            ModifiableClass c = new ModifiableClass();
            Console.WriteLine("SomeMethod doubles the value: " + c.SomeMethod(42));
            c.SomeMethod = (int x) =>
            {
                return 3 * x;
            };
            Console.WriteLine("SomeMethod now triples the value: " + c.SomeMethod(42));
        }
    }

    If you mean adding a method to an object which doesn't have it to start with, you need dynamic objects:

    class Program
    {
        public static void Main()
        {
            dynamic e = new ExpandoObject();
            e.SomeMethod = (int i) =>
                {
                    Console.WriteLine("The value is {i}", i);
                };
            e.SomeMethod(42);
        }
    }

    • Marked as answer by Lisa Zhu Wednesday, August 22, 2012 1:21 AM

  • You can have two similar methods in c# but the arguments should be different.

    • Marked as answer by Lisa Zhu Wednesday, August 22, 2012 1:20 AM

  • Short Answer: No.

    You can't have two methods with the same name and the same parameters. The compiler wouldn't know which Method to use if you call it.

    You didn't specify your problem, but maybe one of this two soultions can help you:

    First, Overloading a Method (I'm sure you know of this). The names of the methods are the same, but they got a different parameter list.

    Second, Named and Optional parameters (maybe even in combination with overloading). For more on this topic, read here.

    I hope this useful to you.

    Regards,

    Steve.

    • Marked as answer by Lisa Zhu Wednesday, August 22, 2012 1:20 AM

Can a class have two methods with the same name but different in parameters is called?

If a class has multiple methods having same name but parameters of the method should be different is known as Method Overloading.

When two methods have the same name but different parameters?

The practice of defining two or more methods within the same class that share the same name but have different parameters is called overloading methods.

Can a class have two methods with the same name?

Yes, we can define multiple methods in a class with the same name but with different types of parameters.

Can two methods have the same name but different return type?

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.