Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 9 of 10 · Java Generics & Type Safety
Interview question

Can you overload methods that differ only by generic type parameters? क्या आप methods को सिर्फ generic type parameters के अंतर से overload कर सकते हैं?

Answer

No - because of type erasure, two methods that differ only in their generic type parameter end up with the IDENTICAL erased signature at compile time, causing a 'duplicate method' compile error.

class Processor {
    public void process(List<String> list) {
        System.out.println('Processing strings');
    }

    // COMPILE ERROR: 'process(List<Integer>)' has the same erasure
    // as 'process(List<String>)' - after erasure, BOTH become
    // process(List) with no distinguishing information
    // public void process(List<Integer> list) {
    //     System.out.println('Processing integers');
    // }
}

// WORKAROUND 1: use different method names
class ProcessorFixed {
    public void processStrings(List<String> list) {
        System.out.println('Processing strings');
    }
    public void processIntegers(List<Integer> list) {
        System.out.println('Processing integers');
    }
}

// WORKAROUND 2: pass a Class<T> token to distinguish at runtime
class ProcessorWithToken {
    public <T> void process(List<T> list, Class<T> type) {
        System.out.println('Processing ' + type.getSimpleName());
    }
}
new ProcessorWithToken().process(List.of('a', 'b'), String.class);

// Note: methods DIFFERING in the RAW type (not just generic parameter)
// CAN coexist - this is true overloading, not affected by erasure
class ValidOverload {
    public void process(List<String> list) { }
    public void process(Set<String> set) { }  // OK - List vs Set are different raw types
}

नहीं - type erasure की वजह से, generic type parameter में अलग होने वाले दो methods compile time पर IDENTICAL erased signature पाते हैं, 'duplicate method' compile error आती है।

class Processor {
    public void process(List<String> list) {
        System.out.println('Strings process हो रहे हैं');
    }

    // COMPILE ERROR: erasure के बाद दोनों process(List) बन जाते हैं
    // public void process(List<Integer> list) {
    //     System.out.println('Integers process हो रहे हैं');
    // }
}

// WORKAROUND 1: अलग method names
class ProcessorFixed {
    public void processStrings(List<String> list) {
        System.out.println('Strings process हो रहे हैं');
    }
    public void processIntegers(List<Integer> list) {
        System.out.println('Integers process हो रहे हैं');
    }
}

// WORKAROUND 2: Class<T> token pass करें
class ProcessorWithToken {
    public <T> void process(List<T> list, Class<T> type) {
        System.out.println('Processing ' + type.getSimpleName());
    }
}
new ProcessorWithToken().process(List.of('a', 'b'), String.class);

// Raw type में अलग methods coexist कर सकते हैं
class ValidOverload {
    public void process(List<String> list) { }
    public void process(Set<String> set) { }  // ठीक है - List vs Set अलग raw types
}

Was this answer clear?