In this program i have input name and code in super class and print it, it also print in sub class Manager I created the object for super class to input name and code, and another object to input Manager's Data, but in the output the Name is showing null and code is 0, why is this happening??

class Employee{

    private int code;
    private String name;
    
    protected String input(){
    int l=0;
    byte[] b=new byte[255];
    String S;
    
    try{
    l=System.in.read(b,0,255);
    }
    catch (Exception r){}
    S=new String (b,0,l-2);
    return S;
    }
    
    protected void get(){
    System.out.print ("Name: ");
    name=input();
    System.out.print ("Code: ");
    code=Integer.parseInt(input());
    }
    
    protected void show(){
    System.out.println("Name: " + name);
    System.out.println("Code: " + code);
    }
}

class Manager extends Employee{  // Manager Profile
    private int salary;
    private String education,experience;
    
    protected void getdata(){
    System.out.print ("Salary (in Digits): ");
    salary=Integer.parseInt(super.input());
    System.out.print ("Experience (in Letters): ");
    experience=super.input();
    System.out.print ("Education (in Letters): ");
    education=super.input();
    }
    
    protected void showdata(){
    super.show();
    System.out.println("Salary(PKR): " + salary + " PKR");
    System.out.println("Education: " + education);
    System.out.println("Experience: " + experience );
    }
}
class EmployeeeMain{
public static void main(String args[]){
	Employee employee = new Employee();
        employee.get();
	Manager manager = new Manager();
	System.out.println ("x-----------------------x\n\nManager's Profile\n");
	manager.showdata();
	}
}

See the output:

Name: abc
Code: 43
----------------------
Enter Manager's Data
----------------------
Salary (in Digits): 232
Experience (in Letters): asdasd
Education (in Letters): adsasd
x-----------------------x

Manager's Profile

Name: null
Code: 0

Salary(PKR): 232 PKR
Education: adsasd
Experience: asdasd

Recommended Answers

All 7 Replies

You need to call manager.get(). Both employee and manager are separate instances of Employee.

sorry the correct code is this,
why should i call manager.get ?? there is no method get()inside manager class,
i want my program to ask for name and code in object "employee" and show it in object "manager"

class Employee{

    private int code;
    private String name;
    
    protected String input(){
    int l=0;
    byte[] b=new byte[255];
    String S;
    
    try{
    l=System.in.read(b,0,255);
    }
    catch (Exception r){}
    S=new String (b,0,l-2);
    return S;
    }
    
    protected void get(){
    System.out.print ("Name: ");
    name=input();
    System.out.print ("Code: ");
    code=Integer.parseInt(input());
    }
    
    protected void show(){
    System.out.println("Name: " + name);
    System.out.println("Code: " + code);
    }
}

class Manager extends Employee{  // Manager Profile
    private int salary;
    private String education,experience;
    
    protected void getdata(){
    System.out.print ("Salary (in Digits): ");
    salary=Integer.parseInt(super.input());
    System.out.print ("Experience (in Letters): ");
    experience=super.input();
    System.out.print ("Education (in Letters): ");
    education=super.input();
    }
    
    protected void showdata(){
    super.show();
    System.out.println("Salary(PKR): " + salary + " PKR");
    System.out.println("Education: " + education);
    System.out.println("Experience: " + experience );
    }
}
class EmployeeeMain{
public static void main(String args[]){
    Employee employee = new Employee();
        employee.get();
    Manager manager = new Manager();
System.out.println ("\n----------------------\nEnter Manager's Data\n----------------------");
   manager.getdata();
    System.out.println ("x-----------------------x\n\nManager's Profile\n");
    manager.showdata();
    }
}

Re-read cale's post.
You call getData() for Salary Experience Education
but nowhere for a manager do you call get() for Name and Code

By calling manager.get you are actually referencing the get method of Employee (since Manager is an Employee. To prove that put a print out along th

e lines of

System.out.println("Calling get from Employee");

in the Employee's get method.

Also in the EmployeeMain, you do not need to instanciate the Employee object (you are not using it), so your main code should look like:

class EmployeeeMain
{
	public static void main(String args[])
	{
		Manager manager = new Manager();
                manager.get(); // This will fill in the name and code
		System.out.println ("\n----------------------\nEnter Manager's Data\n----------------------");
		manager.getdata(); 
		System.out.println ("x-----------------------x\n\nManager's Profile\n");
		manager.showdata();
    }
}

By calling manager.get you are actually referencing the get method of Employee (since Manager is an Employee. To prove that put a print out along th

e lines of

System.out.println("Calling get from Employee");

in the Employee's get method.

Also in the EmployeeMain, you do not need to instanciate the Employee object (you are not using it), so your main code should look like:

class EmployeeeMain
{
	public static void main(String args[])
	{
		Manager manager = new Manager();
                manager.get(); // This will fill in the name and code
		System.out.println ("\n----------------------\nEnter Manager's Data\n----------------------");
		manager.getdata(); 
		System.out.println ("x-----------------------x\n\nManager's Profile\n");
		manager.showdata();
    }
}

i know what are you saying , but i dont want this, suppose if i have two persons,
manager and Worker,
I want my program to ask for the name and the code only once and then show that name and code in the profile of both Manager and Worker, i hope you understand what i'm trying to say

If this were my code I would eliminate the confusing and unnecessary distinction between get() and getData() by naming both methods getData() and starting manger.getData() with a call to super.getData();
That way you have the same interface to get all the data for any member of this class hierarchy, with zero duplicated code.

i know what are you saying , but i dont want this, suppose if i have two persons,
manager and Worker,
I want my program to ask for the name and the code only once and then show that name and code in the profile of both Manager and Worker, i hope you understand what i'm trying to say

These two persons both have the same name and code? Surely not!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.