inheritance - How does the java compiler know of inherited methods? -
we use inheritance in java abstract out similar behavior in superclass , let sub classes inherit it. 1 of advantages of , have 1 copy of method maintain (i.e in superclass).
class animal { public void makenoise() { } public void sleep() { } } class cat extends animal { // override makenoise method public void makenoise() { } } class someclass { public static void main(string args[]) { cat fluffy = new cat(); fluffy.sleep(); } }
i trying understand how java compiler knows of sleep()
method cat
type reference. there can't copy of method in cat
subclass (it defeats purpose of having in superclass , letting subclasses inherit it). information stored in other place ?
when compiler sees fluffy.sleep()
first looks in cat
class public instance method called sleep
takes no parameters. since doesn't find it, moves inheritance chain animal
, , same check on animal
. finds there, good.
this information isn't "stored" anywhere except in code, , java byte code.
Comments
Post a Comment