inheritance - Can I override using subclass object as parameter in Java? -
i have classes follows
public class useful { public void f(object a) { system.out.println("in base f"); } public void g(string a) { system.out.println("in base g"); } } public class moreuseful extends useful { public void f(string a) { system.out.println("in derived f"); } public void g(object a) { system.out.println("in derived g"); } }
i trying override base class method changing parameters in derived class. in method moreuseful.f() using subclass parameter (string) against object in base class. in method moreuseful.g() using superclass parameter (object) against string in base class.
is possible override these way? of above 2 cases correct overriding?
no, that's not possible, , violation of lsp.
by inheriting class, express subclass moreuseful
useful
, therefore exposes all functionality useful
exposes. if removed ability invoke f
object, you'd break promise.
you free overload f
have f(string s)
method though.
Comments
Post a Comment