how to implement the method which have parameter same but different signature

Related searches

I have to implement the function which have the same functionality but different return type and the parameter of the function is also same.

public static List<Base> remove(List<Subclass> arrange ) {
    List<Base>update = new ArrayList<>();

    for(Subclass arranging : arrange){
        //For-loop body here
    }
    return update;
}

public static List<Subclass> remove(List<Subclass> arrange ) {
    List<Subclass>update = new ArrayList<>();

    for(Subclass arranging : arrange){
        //For-loop body here
    }
    return update;
}  

Here Base and Subclass are the classes already defined.

Only one method should be there named remove because the functionality is same so redundancy will occur if I implement the same method twice just because of different datatype

If you have a method which has the same logic with different parameter types you can create a generic version of such method. In your case such a method would look like:

    public static <T> List<T> remove(List<T> arrange) {
        List<T> update = new ArrayList<>();

        for (T arranging : arrange) {
            //For-loop body here
        }
        return update;
    }

Then you can use this method with any T (Base or Subclass) and the method will work with the elements of the list pass as argument and return the appropriate type as well:

        List<Subclass> one = ...;
        one = remove(one);

        List<Base> two = ...;
        two = remove(two);

Hope this helps.

how to implement the method which have parameter same but , A user can implement function overloading by defining two or more functions in a C# can distinguish the methods with different method signatures. If both methods have the same parameter types, but different return type,� I have to implement the function which have the same functionality but different return type and the parameter of the function is also same. you acknowledge that

What you need here is called generic method.

A single generic method declaration that can be called with arguments of different types. The generic function you need will look like this:

public static < E > List<E> remove( List<E> arrange ) {...}

In the case that there are more than one generic type and one is the subclass of the other (e.g. BaseClass and SubClass), the declaration will look like

public static < E, F > List<E> remove( List<F extends E> arrange ) {...}

For more information, you can refer to https://www.tutorialspoint.com/java/java_generics.htm

C#, Method overloadingEdit. In a class, there can be several methods with the same name. However they must have a different signature. The signature of a method� No, its an error. If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously. According to JLS (§8.4.2) methods with same signature is not allowed in this case. Two methods or constructors, M and N, have the same signature if they have, the same name the same type parameters (if any) (§8.4.4), and after adopting the formal parameter types of N to the type parameters of M, the same formal

I see you are in a situation where two methods does almost the same thing but present the result differently to the caller. Use generics if the domain of return type is larger. public static E remove(List<E> arrange)

If the return types are limited you might develop a relationship between Base and SubClass. And use co-variances to deal with multiple return types.

Overloading and Overriding, That is, it allows you to define different methods that have the same name, but that different method signatures, is satisfied by the additional input parameter in relationship to each other, and they can, in fact, each implement behavior that� There are some rules which you need to follow for implementing Method Overloading. Rule 1: Change the method signature. The most important rule for method overloading in Java is to change the method signature. The method signature means a number of parameters, types of parameters and the sequence of parameters.

Method overloading, Overloading methods and constructors. A class definition can provide multiple methods, including constructors, that have the same name but different signatures. (determined by use of the STATIC option in the METHOD statement ) does not Regardless of the arguments, a constructor always instantiates a class of the� C# can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameters list (i.e. the number of the parameters, order of the parameters, and data types of the parameters) within the same class.

Overloading methods and constructors, The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile time error. If both methods have same parameter types, but different return type, then it is not possible.

Method overload is when you have two methods of the same class with the same name and with different parameter types. Method overriding is when a subclass redefines a method of its superclass, of

Comments
  • You could use one method with generics in case the only difference is the return type.
  • Possible duplicate of How to have Java method return generic list of any type?
  • It is not possible to do it if they have the same name. How would the compiler be able to know which one to execute when you are calling remove(List)? The one which is returning List<Base> or List<Subclass>?
  • Java does not support return-type-based method overloading. Rename the method and call one from another, if you want to avoid redundancy. link
  • You can create a common method call remove(List<T> arrange) and call it from both the classes.