0

I am new to java and am having a lot of issues making a multilevel inheritance program.

I'm trying to make a program where my main class (AU) is broken down into subclasses depending on what the user types.

My issue is when I try to call my second level (Part_Time_Student) subclass from my first level subclass (Student).

Whenever I try to call it, it just recalls the first level subclass(the one I'm currently in).

I noticed that if I make my second level subclass(Part_Time_Student) extend the main superclass(AU) it works, but I would prefer to make it extend student.

I realize this is a very complicated post (especially since I don't know the terminology), but I hope my code is easy enough to follow.

AU.java

public class AU {
    Scanner input;
    static String name;
    static Long numb;    

public AU() {

}

public void Name() {
        input=new Scanner(System.in);
        System.out.println("What is the member's name");
        String nam = input.nextLine();
        AU.name=nam;
        System.out.println(nam +" has been added");         
}

public void Phone() {
    input=new Scanner(System.in);
    System.out.println("What is the member's phone number");
    Long number = input.nextLong();
    AU.numb=number;
    System.out.println(number+ " has been saved");
}
}

Main.java

public class Main {
    private static Scanner input;

    public static void main(String[] args) {
        AU au = new AU();
        au.Name();
        au.Phone();        
        while (3==3) {
            System.out.println("What is your role at the University?");
            input=new Scanner(System.in);
            String determ=input.nextLine();
        if (determ.toUpperCase().equals("STUDENT")) {
            Student student=new Student();
            break;
        }
        else if (determ.toUpperCase().equals("STAFF")) {
            break;
        }
        else if (determ.toUpperCase().equals("FACULTY")) {
            break;
        }
        else if (determ.toUpperCase().equals("TESTER")) {
            break;
        }
        else {
            System.out.println("Invalid Response");

        }
        }
        System.out.println("yay");
}}

Part_Time_Student.java

public class Part_Time_Student extends Student {

    public Part_Time_Student() {
        System.out.println("it switched");
        System.out.println(GPA);
        System.out.println(Assign);
        System.out.println(name);
    }
        public void tester() {
        System.out.println("test");
        }
    }

Student.java

public class Student extends AU {
    static double GPA=5;
    static String Assign;
    public Student() {
        super();
        System.out.println(name);
        System.out.println("Are you a full-time student or part-       student(type part or full)");
        input=new Scanner(System.in);
        while (3==3) {
            String get=input.nextLine();
            switch (get.toUpperCase()) 
            {
            case "PART":{
                Student.Assign="Part";
                Part_Time_Student Studentp = new Part_Time_Student();
                break;
            }
            case "FULL":{
                Student.Assign="Full";
                break;
            }
            default :{
                System.out.println("Invalid ");

            }}}
    }
        public void gpa(String grade,long credits) {

            System.out.println(name+numb);

        name=name;
    }
    public void Welcome() {
        System.out.println("Welcome Student");      
    }
}

Output: What is the member's name

-Test

Test has been added

What is the member's phone number

-540

540 has been saved

What is your role at the University?

-student

Test

Are you a full-time student or part-student(type part or full)

-part

Test

Are you a full-time student or part-student(type part or full)

As you can see the command "Part_Time_Student Studentp = new Part_Time_Student();" is just recalling the student class over an over.

11
  • What is the problem you are trying to solve? It seems like you are trying to use inheritance for the sake of it, and it's not immediately clear from your example why? When you say "Whenever I try to call it, it just recalls the first level subclass" - remember, subclasses inherit from their parents, so unless you're overriding methods in subclasses, calling an inherited method from a parent will invoke it on the parent. Commented Feb 10, 2016 at 23:28
  • 2
    What print statements you are getting on console? Commented Feb 10, 2016 at 23:31
  • I am doing an inheritance program for my class, so I'm trying to use it as much as possible (even though it is not always necessary). Anyway, the output I'm getting is: (Going to put in original post) Commented Feb 10, 2016 at 23:37
  • Is there a reason you have the infinite while loop in your Student constructor? Commented Feb 10, 2016 at 23:38
  • 1
    @RileyCarney, Don't those break statements apply to the switch and not the while loop? Commented Feb 10, 2016 at 23:45

2 Answers 2

1

If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Sign up to request clarification or add additional context in comments.

Comments

0

Move your statement Part_Time_Student Studentp = new Part_Time_Student(); out of while loop, infact out of the constructor. When you are constructing your Student class, and inside when you invoke a constructor of subclass, it in turn calls its super class which is Student, so it goes over and over. So it will keep asking you same question again and again.

Remove the Part_Time_Student constructor from :

case "PART":{
        Student.Assign="Part";
        // Part_Time_Student Studentp = new Part_Time_Student(); //remove this
        break;
    }

Change this if condition in your Main class below to:

if (determ.toUpperCase().equals("STUDENT")) 
{
   Student student=new Student();
   if ( student.Assign.equalsIgnoreCase( "Part" ) )
   {
      Part_time_Student part_time_student = new Part_time_Student();
   }
   break;
}

5 Comments

Thank you so much for the help! The only problem now is it prints/requests input twice before going to Part_Time_Student. Is there a way I can make it so you only have to type your student status once?
I think I figured it out, but let me know if there is a better solution. public Student() { } public void run() { System.out.println(name); System.out.println("Are you a full-time student or part-student(type part or full)"); input=new Scanner(System.in); String get=input.nextLine(); switch (get.toUpperCase()) { case "PART":{ Student.Assign="Part"; System.out.println("Set"); break;
You need to re-think about the design, don't get inputs in your constructors. After you construct the object, then get input from user. So move the question "Are you full time... " from the constructor of Student and put it after Student student = new Student(); in your Main. Then depending on user input, you can call new Part_time_Student ()
Alright, I'll go ahead and make those changes. Thanks for all your help!
If you can, you can mark either this or the below one as an answer.

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.