1

i'm trying to create a simple java inheritance program along with the use of super and this keyword. showing here the marks of a student in three subjects for 2 semesters sem1 and sem2. i want to show total marks ie. S1T(Sem1 total), S2T(Sem 2 total) and also 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 is 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 be applied to given types; { ^ required: int,int,int found: no arguments reason: actual and formal argument lists differ in length

kindly help me out here..

3
  • in your Sem2 constructor, your first line should be super(p,q,r); Commented Jul 15, 2013 at 17:46
  • i don't test it but i think your code even compile cause your parent class doesn't have constructor without parameters, you have to call super(int , int ,int ) in child class Commented Jul 15, 2013 at 17:46
  • Thankyou for the help.. i got it right. Commented Jul 15, 2013 at 18:23

4 Answers 4

1

I don't think you really want two separate classes. The two classes are mostly the same, except for the computation of the semester 2 total. It may work better to have two separate instances of one class, and compute the full-year total separately.

If you did want two classes, related by inheritance, then you'd either need to call super() in Sem2's constructor, because Sem1 lacks a default constructor. This would likely require you to provide additional values in Sem2's constructor, since the marks for semester 1 differ from those 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;
   }
Sign up to request clarification or add additional context in comments.

1 Comment

why not protected? now if they are in the same package has access
1

You forgot to call constructor of the super class in Sem2. e.g. you should have:

Sem2(int p,int q,int r)
{
    super(p,q,r);
    this.p=p;
    this.q=q;
    this.r=r;
}

Comments

1

Firstly, as commented, your compilation problem is due to not calling the Sem1 constructor in your Sem2 constructor since Sem1 does not have a 0 argument / default constructor. You need to add super(p,q,r); as the first line of your Sem2 constructor.

Secondly, and more importantly, you appear to be using inheritance incorrectly. There appears to be no reason to have a Sem2 object that inherits from Sem1. I imagine you could simply have two instance of a Semester object instead of having essentially identical Sem1 and Sem2 classes.

In fact, having Sem2 as a subclass of Sem1 breaks the general guidelines for inheritance since Sem2 is NOT a Sem1 (there is no "is a" relationship). Sem1 and Sem2 are both semesters, however, so you could have a Semester class and then Sem1 and Sem2 subclasses if there was unique logic required for each of them.

1 Comment

sir, i got it right.. i'm learning the language and thanks for a great suggestion.. thankyou.
0

Sem2 needs to call the super constructor super(int, int, int). Sem2 either needs to take three more grades, or a SEM1 which it can use to call a super constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.