I’m trying to make program that has a Person class with one field : name(String) and accessor and mutator. and then make a second class Student ( extends Person class) – one field: id and accessor and mutator of id. then to test it make an object and assign name and id and print out result.
This is what I have so far:
Person Class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package person;
/**
*
* @author Administrator
*/
public class Person {
/**
* @param args the command line arguments
*/
private String name;
public void setName(String n)
{
name = n;
}
public String getName(String n)
{
return name;
}
}
Student class //I think this is the class with the extension:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package person;
public class Student extends Person {
public int idNumber;
Student(String name, int idNumber) {
}
public void setID(int id)
{
idNumber = id;
}
public int getID(int id)
{
return idNumber;
}
}
and then the main class to test the result:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package person;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class MainStudentID {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Students name: ");
String name = keyboard.nextLine();
System.out.println("Enter Students ID Number:");
int idNumber = keyboard.nextInt();
Student Student = new Student(name, idNumber);
System.out.println("Your name is: " + Student.person.getName());
System.out.println("Your id Number is: " + Student.Student.getID());
}
}
I’m not sure what I am doing wrong, and I apologize for my crappy code, first time learning about the topic of class extensions.
You need to set the values of name and id in your Student constructor. You’re not storing them which is why you’re not getting a result when you try to print.
You also need to get rid of the parameters in your get methods.
Person Class:
Student class