Reputation: 3
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.
Upvotes: 0
Views: 132
Reputation: 14228
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;
}
Upvotes: 0
Reputation: 4466
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
Upvotes: 1