I've just learned the OOP concept of abstraction, and I was wondering if I was implementing it correctly in my code and if you guys can point out any bad practices.
test1.java
package OOPConcepts;
import java.util.Scanner;
public class test1 {
static public Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String fName, lName;
int age;
Student newPerson = new Student();
input.close();
}
}
Person.java
package OOPConcepts;
public class Person
{
static private int pNumber = 0;
private int pID;
public int age;
private String type;
public String fName, lName;
Person()
{
pNumber++;
pID = pNumber;
System.out.println("Enter first name:");
String fName = test1.input.next();
System.out.println("Enter last name:");
String lName = test1.input.next();
System.out.println("Enter age:");
int age = test1.input.nextInt();
type = "person";
describe(fName, lName, age);
System.out.println("Made a " + type);
}
public void setType(String type){
this.type = type;
}
public String getType()
{
return this.type;
}
public void describe(String fName, String lName, int age)
{
this.fName = fName;
this.lName = lName;
this.age = age;
System.out.println("Name: " + this.fName + " " + this.lName + "|Age: " + this.age);
}
}
Student.java
package OOPConcepts;
public class Student extends Person{
static private int sNumber = 0;
private int studentID;
public String major;
Student()
{
sNumber++;
studentID = sNumber;
setType("student");
System.out.println("Who is also a " + getType());
System.out.println("Enter major: ");
String major = test1.input.next();
this.major = major;
}
public void setStudentID(int idNumber)
{
this.studentID = idNumber;
}
public int getStudentID()
{
return this.studentID;
}
}
Student
andPerson
is not a correct relationship. While in natural language we can say a student is a type of person, but to model it in software a person is a student when they're enrolled at a school. To elaborate on what I'm describing, what if this person goes to two schools? A class such asclass Student { +School school, +Person person, int studentNumber }
may map better to reality. \$\endgroup\$