Home Interface, abstract class and inhereting subclass with same Method, that gets different type arguments, wich is used?
Reply: 0

Interface, abstract class and inhereting subclass with same Method, that gets different type arguments, wich is used?

user1125
1#
user1125 Published in April 22, 2018, 12:50 am

I have these interface and classes:

public interface AlternateLight {

     public String change(long time);
}

public abstract class AbstractLight {

    public String change(int time) {
        return "AbstractLight Method was used";
    }
}

public class DistinctAlternateLight extends AbstractLight implements AlternateLight {

    public String change(long time) {
        return "DistinctAlternateLight Method was used";
    }
}

Now I call the method change() with following main-method:

public class WhatUsedTest {

    public static void main(String[] args) {
        AlternateLight al = new DistinctAlternateLight();
        System.out.println(al.change(100));
    }
}

It prints "DistinctAlternateLight Method was used", but why? I thought since I didn't put a "L" behind the 100 as argument, it would call the method of the abstract class, because its method takes integers. With missing out on the "L", I guessed the compiler wouldn't handle the 100 as a long value and call the method taking longs, but it does. Why is that so?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300849 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO