i'm trying create simple java inheritance program along use of super , keyword. showing here marks of student in 3 subjects 2 semesters sem1 , sem2. want show total marks ie. s1t(sem1 total), s2t(sem 2 total) , grand total..
//student record keeping system class sem1 { int a,b,c,s1t,s1gt; sem1(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } void total() { s1t=a+b+c; s1gt=s1t; } void display() { system.out.println("s11: "+a); system.out.println("s12: "+b); system.out.println("s13: "+c); system.out.println("s1total: "+s1t); system.out.println("s1gtotal: "+s1gt); system.out.println(""); } } class sem2 extends sem1 { int p,q,r,s2t,s2gt; sem2(int p,int q,int r) { this.p=p; this.q=q; this.r=r; } void total() { s2t=p+q+r; s2gt=s2t+s1t; } void display() { super.display(); system.out.println("s21: "+p); system.out.println("s22: "+q); system.out.println("s23: "+r); system.out.println("s2total: "+s2t); system.out.println("s2gtotal: "+s2gt); system.out.println(""); } }
here main class
class studentrcd { public static void main(string abc[]) { sem1 obj = new sem1(10,20,30); obj.total(); obj.display(); sem2 obj1 = new sem2(20,30,40); obj1.total(); obj1.display(); } }
error: constructor sem2 in class sem2 cannot applied given types; { ^ required: int,int,int found: no arguments reason: actual , formal argument lists differ in length
kindly me out here..
i don't think want 2 separate classes. 2 classes same, except computation of semester 2 total. may work better have 2 separate instances of 1 class, , compute full-year total separately.
if did want 2 classes, related inheritance, you'd either need call super() in sem2's constructor, because sem1 lacks default constructor. require provide additional values in sem2's constructor, since marks semester 1 differ of semester 2.
class sem2 extends sem1 { int p,q,r,s2t,s2gt; sem2( int a, int b, int c, int p,int q,int r) { super( a, b, c ); this.p=p; this.q=q; this.r=r; }
Comments
Post a Comment