Why cant we call base class constructor without creating object of derived class in multiple inheritance (java) -
this question has answer here:
class { public a() {...} } class b extends { public b(string s) {...} } class c extends b { public c() {...} } class const { public static void main(string a[]) { b object=new b("hello"); // gives error. } }
why compiler giving error ->
error.java:18: error: constructor b in class b cannot applied given types; class c extends b ^ required: string
found: no arguments
reason: actual , formal argument lists differ in length
1 error
you have call constructor of child
constructor creates object of type, if call parent constructor able create parent instace. thats why not allowed , have rewrite c class follows:
class c extends b { public c(string s) { super(s); ... } }
note: error in case:
c object=new c("hello"); // gives error.
Comments
Post a Comment