java inheritance and constructor -


class a{     int ax;int ay;      a(int x, int y){         ax=x;         ay=y;     }   void show(){         system.out.println("value of ax : "+ax+" value of ay : "+ay);  } }  class b extends a{         int bx,by;      b(int bx,int by){         this.bx=bx;         this.by=by;     }      void show(){         super.show();         system.out.println("value of bx : "+bx+" value of : "+by);     } }  public class progl2q2{      public static void main(string s\[\]){         m=new a(10,20);         b b=new b(20,30);         m.show();         b.show();     } } 

its showing error this:

progl2q2.java:36: error: constructor in class cannot applied given types;         m=new a(10,20);             ^   required: no arguments   found: int,int   reason: actual , formal argument lists differ in length 1 error  c:\users\labuser\desktop\saurabh\prog list2> 

you have problem in constructors.

in program have written b extends a, , don't have default constructor of instead have parametrized constructor of a, wrong.

when make variable of class, if class extends other class constructor initializes first, in case there no way initialize constructor of parent class, program failing.

so, solution can either have default constructor in class or need call parametrized constructor b.

here first solution

class {     int ax;     int ay;     public a() {         super();     }     a(int x, int y) {         ax = x;         ay = y;     }     void show() {         system.out.println("value of ax : " + ax + " value of ay : " + ay);     } } class b extends {     int bx, by;     b(int bx, int by) {         this.bx = bx;         this.by = by;     }     void show() {         super.show();         system.out.println("value of bx : " + bx + " value of : " + by);     } } public class test {     public static void main(string s[]) {         m = new a(10, 20);         b b = new b(20, 30);         m.show();         b.show();     } } 

here output:::

value of ax : 10 value of ay : 20

value of ax : 0 value of ay : 0

value of bx : 20 value of : 30

as can see when b object initialized, constructor has been called.

here second solution::

class {     int ax;     int ay;      a(int x, int y) {         ax = x;         ay = y;     }     void show() {         system.out.println("value of ax : " + ax + " value of ay : " + ay);     } } class b extends {     int bx, by;      public b(int x, int y, int bx, int by) {         super(x, y);         this.bx = bx;         this.by = by;     }      void show() {         super.show();         system.out.println("value of bx : " + bx + " value of : " + by);     } } public class test {     public static void main(string s[]) {         m = new a(10, 20);         b b = new b(20, 30);         m.show();         b.show();     } } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -