class - Java : If A extends B and B extends Object, is that multiple inheritance -
i had interview, , asked question.
interviewer - java support multiple inheritance?
me - no
interviewer - each class in java extends class object (except class object) , if externally extend 1 class like
class extends b{ // code here }
then can class extend class b , class object, means multiple inheritance. how can java not support multiple inheritance?
me - class b extends class object, when extend class b in class class extends class object indirectly. multi-level inheritance, not multiple inheritance.
but answer did not satisfy him.
is answer correct? or wrong? happens internally?
my answer correct?
yes, mostly, , in context describe. not multiple inheritance:
it's said is, single inheritance multiple levels.
this multiple inheritance: inheriting 2 or more bases don't have "is a" relationship each other; inheriting unrelated lines, or lines had diverged (in java, since object
base, latter):
(image credits: http://yuml.me in "scruffy" mode)
internally happens actually?
just said: there multiple levels. when compiler resolving member on instance:
obj.member
...it looks see if type of obj
(which in case class, classb
) has member
, either because provides directly or has through inheritance. @ runtime, jvm uses member
object has.
the reason said "mostly" above java has interfaces, , of java 8 has "default methods" on interfaces. complicates things bit, answer levels correct in context of described interviewer saying object
, classa
, , classb
.
interfaces have made possible, in java, have "is a" relationship 2 different types: class type inherits from, , of several interface types implements. interfaces without default methods aren't multiple inheritance in practical way (the class has provide implementation), did make possible class have multiple "is a" relationships unrelated type trees. (i'm not academic, it's possible academic argue provide multiple inheritance in academic way.)
with java 8, interfaces can provide default implementations of methods define, blurs lines @ practical level. let's @ bit more deeply:
say have classa
:
class classa { void dosomething() { // code here } }
and interface1
:
interface interface1 { default void dosomethingelse() { // requires java 8 // code here } }
and classb
:
class classb extends classa implements interface1 { }
classb
inherits implementation of dosomething
classa
. also gets "default" version of dosomethingelse
interface1
. didn't implement in classb
, classb
isn't abstract: has dosomethingelse
. gets interface. used word "gets" rather "inherits" there, looks lot inheriting default method.
this multiple-inheritance "light" (as in "light beer"). end-run around thornier problems true multiple inheritance, like:
- what should type of
super
be? (java 8's answer:classa
) - what order run constructors in? (java 8's answer: single-lineage constructor chaining, interfaces don't have constructors.)
- do run constructors inherit more once, more once? (java 8's answer: can't inherit constructors more once, interfaces don't have them.)
- what happens if inherit multiple methods same signature? (java 8's answer: if 1 of them base class, that's 1 that's used; base class's implementation can override default method of multiple interfaces. if have multiple default methods same signature different interfaces @ compile-time, it's compile-time error. if interface has been changed without class being recompiled , situation arises @ runtime, it's runtime
incompatibleclasschangeerror
exception listing conflicting default methods.)
Comments
Post a Comment