Why can't parameter types be loosened in overridden methods?

This code is invalid:

interface Foo
{
    public void foo(final String string);
}

public class Bar implements Foo
{
    // Error: does not override.
    @Override public void foo(final Object object)
    {
    }
}

Because every String is obviously an Object, I would expect this code to be perfectly fine: any code that depends on giving foo() a String would stil function when foo() actually takes an Object.

It appears, though, that method signatures have to be identical to those of the methods they're overriding. Why?

Answers


What if

interface Foo
{
    void foo(String string);
    void foo(Object string);
}

Then which method is overridden by Bar?


Need Your Help

Creating Nested Objects - Ignore save if unique but still build children

ruby-on-rails ruby-on-rails-3

I'm having some difficulty creating nested objects between a one-to-many relationship. My models are Artist and Song, an artist can have many songs and a song must belong to an artist.

Using the ShadowRoot of a component onAttach

dart angular-dart

So I have this angular dart component:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.