Reputation: 1
I have three classes Person class,Main class and Donar class,
Person class
public class Person{
private String name;
private String dateOfBirth;
private String gender;
private String mobileNumber;
private String bloodGroup;
Person(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public void displayPersonDetails( )
{
System.out.println("Name : " + name);
System.out.println("Date of Birth : " +dateOfBirth);
System.out.println("Gender : " +gender);
System.out.println("Mobile Niumber : " +mobileNumber);
System.out.println("Blood Group : "+bloodGroup);
}
}
Donor class
class Donor extends Person
{
private String bloodBankName;
private String donorType;
private String donationDate;
public Donor() {
}
public String getBloodBankName() {
return bloodBankName;
}
public void setBloodBankName(String bloodBankName)
{
this.bloodBankName = bloodBankName;
}
public String getDonorType() {
return donorType;
}
public void setDonorType(String donorType) {
this.donorType = donorType;
}
public String getDonationDate() {
return donationDate;
}
public void setDonationDate(String donationDate) {
this.donationDate = donationDate;
}
public void displayDonationDetails( ) {
System.out.println("Donation Details :");
super.displayPersonDetails();
System.out.println("Blood Bank Name : "+bloodBankName);
System.out.println("Donor Type : "+donorType);
System.out.println("Donation Date : "+donationDate);
}
}
Main class
package Eboxex1;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Donor d=new Donor();
Person p=new Person();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name :");
p.setName(sc.nextLine());
System.out.println("Enter Date of Birth :");
p.setDateOfBirth(sc.nextLine());
System.out.println("Enter Gender :");
p.setGender(sc.nextLine());
System.out.println("Enter Mobile Number :");
p.setMobileNumber(sc.nextLine());
System.out.println("Enter Blood Group :");
p.setBloodGroup(sc.nextLine());
System.out.println("Enter Blood Bank Name :");
d.setBloodBankName(sc.nextLine());
System.out.println("Enter Donor Type :");
d.setDonorType(sc.nextLine());
System.out.println("Enter Donation Date :");
d.setDonationDate(sc.nextLine());
d.displayDonationDetails();
sc.close();
}
}
Output
I don't know what was the error is.
when we run the main method means it will ask for the details from the user then details like Name, Date of Birth, Gender, Mobile Number, Blood Group
are goes to the Person
class (getter and setter method) then details like Blood Bank Name, Donor Type, Donation Date
are goes to the Donor
class (getter and setter method) Then the method displayDonationDetails
are called in the main class. In this method, we have to call the details in the Person class. but the method cannot fetch the details of the Person
class to Donor
class method
Upvotes: 0
Views: 597
Reputation: 11
You are creating two seperate objects: Person "p" and Donor "d" (which due to inheritance is a Person itself).
Then you set all the person data (name, date of birth, gender etc.) on the "p" object and all the donor data (donor type, donation date etc.) on the "d" object.
So at the end of the main-method your objects look like this:
Person "p":
|---------------|---------------|
| Attribute | Value |
|---------------|---------------|
| name | jano |
| dateOfBirth | 5/11/1998 |
| gender | Female |
| mobileNumber | 456437538 |
| bloodGroup | A+ve |
|---------------|---------------|
Donor "d" (Donor inherits from Person class):
|---------------|---------------|
| Attribute | Value |
|---------------|---------------|
| name | null |
| dateOfBirth | null |
| gender | null |
| mobileNumber | null |
| bloodGroup | null |
| bloodBankName | Bload assur e |
| donorType | ssssssssssag |
| donationDate | 5/787/989 |
|---------------|---------------|
And then via the call to method "displayDonationDetails" you display only the data of Donor "d". So everything works as expected.
Only create the Donor object "d" and set all values for it, then everything works as you expect it to do.
Upvotes: 1
Reputation: 355
When you are using inheritence, you automatically get the properties of the parent class. So you only have to create child class object.
Your main class should look like this : -
public class Main {
public static void main(String[] args){
Donor d=new Donor();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name :");
d.setName(sc.nextLine());
System.out.println("Enter Date of Birth :");
d.setDateOfBirth(sc.nextLine());
System.out.println("Enter Gender :");
d.setGender(sc.nextLine());
System.out.println("Enter Mobile Number :");
d.setMobileNumber(sc.nextLine());
System.out.println("Enter Blood Group :");
d.setBloodGroup(sc.nextLine());
System.out.println("Enter Blood Bank Name :");
d.setBloodBankName(sc.nextLine());
System.out.println("Enter Donor Type :");
d.setDonorType(sc.nextLine());
System.out.println("Enter Donation Date :");
d.setDonationDate(sc.nextLine());
d.displayDonationDetails();
sc.close();
}
}
Upvotes: 0