Home > Software design >  how to access fields of derived class from another derived class in multilevel inheritance
how to access fields of derived class from another derived class in multilevel inheritance

Time:09-17

I am trying to understand multilevel inheritance through this program

import java.util.Scanner;

class Student{
    public
    int rno;
    String stud_name;
    Student(int r, String name){
        rno = r;
        stud_name = name;
    }
    
} 
class sports extends Student{
    public
    int pts;
    sports(int rno, String name, int points){//line 16
        pts = points;
        super(rno,name); //line 18
    }
    
}
class results extends sports{
    public
    int [] mark = new int[2];
    int sum =0;
    results(int rno, String name, int marks[], int points){
        super(rno,name,points);
        this.mark = marks;
        
    }
    
    void display(){
        for(int i = 0; i<3; i  ){
            sum  = mark[i];
        }
            
        System.out.println("Student Name: " super.stud_name);
        System.out.println("Student Roll no" super.rno);
        System.out.println("Sports points: " super.pts);
        System.out.println("Total marks: " sum);
        
        
    }
}


class Main{
    public static void main(String args[]){
     
        Scanner s = new Scanner(System.in);
        int rollno;
        int points;
        String name;
        int[] marks = new int[5];
        
        System.out.println("Enter Student name");
        name = s.nextLine();
        
        System.out.println("Enter Student roll no");
        rollno = s.nextInt();
        
        for(int i = 0; i<3; i  ){
        System.out.println("Enter the" i "th mark");
        marks[i] = s.nextInt();
        }
        
        
        System.out.println("Enter Sports pts");
        points = s.nextInt();
        
        results stud = new results(rollno, name, marks, points);
        stud.display();
        
        
        
    }
}

but I am getting these errors

Main.java:16: error: constructor Student in class Student cannot be applied to given types; sports(int rno, String name, int points){ ^ required: int,String found: no arguments reason: actual and formal argument lists differ in length Main.java:18: error: call to super must be first statement in constructor super(rno,name); ^ 2 errors

When I tried to solve this error I got a new Doubt

1)If super keyword refers to immediate parent class right, then how am I able to access variables rno and stud_name from results class(results class's immediate parent is sports class right not student class). If I understood it wrongly and results class's immediate parent is student class then how do I access the variables in sports class from results class.

And please help me solve this error, I have tried searching for solutions and examples online but I only find programs with 1 child class.

CodePudding user response:

The error itself is easy to solve, just put the super() call at the beginning of the constructor, like the error message told so:

    sports(int rno, String name, int points){
        super(rno,name);
        pts = points;
    }

However, there are many things that's smelly in your code, I'd suggest you to look into them:

  • Clean code: follow the naming conventions, start class names with capital letter like Sport or Result, and don't let them be plurar
  • Understanding the concepts of OOP, inheritance is not for expanding a class with more fields. Inheritance is for extending a class' functionality, so the child can be used wherever the parent could.
    If you want to have new fields in your class, just put a new class in it's fields, like this:
class results {  // no need for inheritance
    ...
    Sports sports;

    results(int rno, String name, int marks[], int points) {
        sports = new Sports(rno, name, points);  // initialize the field
        this.mark = marks;

    }

    void display() {
        ...
        System.out.println("Student Name: "   sports.stud_name);  // you reach it's fields like this
    }
}
  • You should also put different class definitions in different .java files, and look into the use of visibility modifiers
  • Related