Monday, 15 August 2011

oop - Why can't I assign a derived class object to base class object in java? -



oop - Why can't I assign a derived class object to base class object in java? -

here code:

class base of operations { public void abc() { system.out.print("what's dude"); } } class derived { public void abc() { system.out.print("wad "); } } public class superreftosub { public static void main(string args[]) { base of operations b=new base(); derived d=new derived(); base of operations b=d; b.abc(); } }

you must utilize superclass-subclass relation:

so:

class derived extends base of operations { @override public void abc() { system.out.print("wad "); } }

in weakly-typed programming paradigms (like instance duck-typing), indeed not necessary. java typed (well java not typed, that's discussion) , allows assigning extended types , can give new definition method using @override annotation.

java oop subclass superclass

No comments:

Post a Comment