Hello Im practicing inheritance and could use some help gett

Hello. I\'m practicing inheritance and could use some help getting it going. I have a class called Person, and I need to create a class called Doctor. Doctor should inherit Person, and store aditional information such as a specialty. The class Doctor also needs to add appropriate constructors and methods to initialize, access, and manipulate the data members. Can someone do this for me? The rest of the practice are similar and having one to base off of would be a huge help. Thank you.

Person.java
--------------------

public class Person {
  
   private String lastName;
   private String firstName;
  
   public Person(String last, String first)
   {
       this.lastName = last;
       this.firstName = first;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   public String getFirstName() {
       return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }
  
   public String toString()
   {
       return lastName + \", \" + firstName;
   }

}

-------------------------

Solution

Please follow the code and comments for description :

CODE :

Person.java :

public class Person { // person class

private String lastName; // global initialisations
private String firstName;

public Person(String last, String first) { // coonstructor
this.lastName = last;
this.firstName = first;
}

public String getLastName() { // getters and setters methods
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

@Override
public String toString() { // string method that returns the combined names
return lastName + \", \" + firstName;
}
}

Doctor.java :

public class Doctor extends Person { // doctor class that inherits the person class

String Speciality;
  
public Doctor(String last, String first) { // constructor
super(last, first);
}

@Override
public String toString() { // method that return the data
return \"Doctor{\" + \"Speciality=\" + Speciality + \'}\';
}

public String getSpeciality() {
return Speciality;
}

public void setSpeciality(String Speciality) {
this.Speciality = Speciality;
}
  
}

PersonTest.java :

import java.util.Scanner;

public class PersonTest { // driver class for a test
public static void main(String[] args) {
String lname, fname;
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter the Last Name : \"); // prompt for the user to get the data
lname = sc.nextLine();
System.out.println(\"Enter the First Name : \");
fname = sc.nextLine();
  
Person p = new Person(lname, fname); // creating an object for the respective class and passing the data
String res = p.toString();
  
Doctor d = new Doctor(lname, fname); // creating an object for the respective class and passing the data
System.out.println(\"Enter the Speciality : \");
String sp = sc.nextLine();
d.setSpeciality(sp);
String spec = d.toString();
System.out.println(\"Full Name : \"+res); // displaying the results
System.out.println(spec);
}
}


OUTPUT :

Enter the Last Name :
John
Enter the First Name :
Kennedy
Enter the Speciality :
Dentist
Full Name : John, Kennedy
Doctor{Speciality=Dentist}


Hope this is helpful.

Hello. I\'m practicing inheritance and could use some help getting it going. I have a class called Person, and I need to create a class called Doctor. Doctor sh
Hello. I\'m practicing inheritance and could use some help getting it going. I have a class called Person, and I need to create a class called Doctor. Doctor sh
Hello. I\'m practicing inheritance and could use some help getting it going. I have a class called Person, and I need to create a class called Doctor. Doctor sh

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site