0
class Department{

  int dep_id;
  string dep_name;

Department(string dep_id , string dep_name)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter Department ID");
dep_id=input.nextInt();
system.out.println("Enter Department Name");
dep_name=input.nextString();
void Display(){
  System.out.println("Deaprtment ID is : " + dep_id);
  System.out.println("Deaprtment Name is : " + dep_name);
  
}

class Professor extends Person
{         //This person is different class

 string profName;
  Department dept;
 Professor(string profName){
  Scanner input=new Scanner(System.in);
  System.out.println("Enter Professor Name");
   profName=input.nextString();


 }

Now I have to add A data field named dept of type Department A two-argument constructor to initialize both attributes of user-defined values How can I call Department class in Professor Class

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Abdul Rehman
  • 159
  • 1
  • 2
  • 18
  • super(a,b) ??????? – Ravi Oct 08 '17 at 13:09
  • Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) When asking for help, please indent and format your code reasonably and consistently. (It's a good idea to do that for yourself, too.) Also be sure to resolve basic compilation errors prior to asking (unless, of course, it's the compilation error you're asking about). Java is case-sensitive, `string` != `String`. – T.J. Crowder Oct 08 '17 at 13:11
  • in professor class create a constructor to take inputs for professor class , dept_id and dept_name then call the constructor of dept class i.e. dept = new Department(dept_id, dept_name); – Rajiv Kapoor Oct 08 '17 at 13:12
  • Ravi professor class is not subclass hence it's not duplicate of Calling superclass ..... – Rajiv Kapoor Oct 08 '17 at 13:13
  • @RajivKapoor his code was totally messed. So, I didn't read complete question instead just read the question title – Ravi Oct 08 '17 at 13:23

1 Answers1

1

Hope this makes it clear

    class Professor extends Person { // This person is different class

    string profName;
    Department dept;

    Professor(string profName, int dept_id, String dept_name) {
        this.profName = profName;
        dept = new Department(dept_id, dept_name);
    }
}

use scanner in your main class to get these inputs

Rajiv Kapoor
  • 315
  • 1
  • 7