I have written a code in java for the following scenario.
There is a class called Student and it has registration number and name as instance variables and student count as a class variable. Student class has two methods called display which is supposed to display the details of a particular student and displaycount which will display the number of students. Student is a super class and which has two sub classes called BCS Student & BIT Student. Each of the subclasses has private data members of its own. Subclasses are supposed to override display method of the superclass by adding its additional data. Below is my code



class Student{
    protected int RegNo;
    public static int studcount=0;
    protected String Name;

    public Student(int R, String N){
    RegNo=R;
    Name=N;
    studcount++;

    }

    public void display(){
    System.out.println("Student");
    }

    public static void displaycount(){
    System.out.println("No of Students ="+"\t"+studcount);
    }


    }

    class BCS_Student extends Student{
    private int Mem_No;
    public BCS_Student(int M){
   ** super.Student (int RegNo, String Name);**
    Mem_No=M;


    }

    public void display(){
    System.out.println(RegNo+"\t"+Name+"…

    }



    }

    class BIT_Student extends Student{
    private float GPA;

    public BIT_Student(float G){
    **super.Student(int RegNo, String Name);**
    GPA=G;

    }
    public void display(){
    System.out.println(RegNo+"\t"+Name+"…
    }

    class StudMain{
    public static void main(String args[]){
    BCS_Student BCS1=new BCS_Studnet(101,"Gihan",903456);
    BCS1.display();
    super.displaycount();
    BIT_Studnet BIT1=new BIT_Studnet(208,"Kaveen",12.56);
    BIT1.display();
    super.displaycont();


    }



    }

    }



When I compile the code compiler says there are errors (error lines are bold)
Have I written the code correctly ?
Are there any syntax errors or problems with my logic?
Please help.
Thanks in advance !

Here's what my compiler says:

Implicit super constructor Student() is undefined. Must explicitly invoke another constructor

In your class Student you have just one construcor, and it's a custom constructor with two arguments. In order to call that super constructor, it seems that you need to have explicitly in your class Student the default constructor as well. Your observations are correct, it does yelds errors on the "bolded" lines...
Here's my lecture course on Java about classes: it might help you. Click Here

Edited 4 Years Ago by Lucaci Andrew

This article has been dead for over six months. Start a new discussion instead.