Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here - staff is the super class/base class.
teacher,typist,officer are the sub class/derived class.
regular and casual are the derived class of typist.

I am facing an issue related to on calling the objects of different methods into the main ones. do I need to change to name of the object or use scanner as static.






The output -

demo.java:112: error: cannot find symbol
sc1.displayStaff();
^
symbol: variable sc1
location: class demo
demo.java:113: error: cannot find symbol
sc1.displayteacher();
^
symbol: variable sc1
location: class demo
demo.java:116: error: cannot find symbol
sc2.displayStaff();
^
symbol: variable sc2
location: class demo
demo.java:117: error: cannot find symbol
sc2.displayspeed();
^
symbol: variable sc2
location: class demo
demo.java:120: error: cannot find symbol
sc5.displayStaff();
^
symbol: variable sc5
location: class demo
demo.java:121: error: cannot find symbol
sc5.displaycasual();
^
symbol: variable sc5
location: class demo
demo.java:124: error: cannot find symbol
sc3.displayStaff();
^
symbol: variable sc3
location: class demo
demo.java:125: error: cannot find symbol
sc3.displaygrade();
^
symbol: variable sc3
location: class demo
8 errors

What I have tried:

Java
import java.util.*;
class staffs
{
int code;
String name;
staffs(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the code ??");
code = sc.nextInt();
System.out.println("Enter the name ??");
name = sc.nextLine();
}
void displayStaff(){
System.out.println("Code = "+code);
System.out.println("Name = "+name);
}}


class teacher extends staffs
{
String subject;
int pub;
teacher(){
super();
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter the subject ??");
subject = sc1.nextLine();
System.out.println("Enter the publication date ??");
pub = sc1.nextInt();
}
void displayteacher(){
System.out.println("Subject = "+subject);
System.out.println("Name = "+pub);
}}



class typist extends staffs
{
int speed;
typist(){
super();
Scanner sc2 = new Scanner(System.in);
System.out.println("Enter the speed in wpm ??");
speed = sc2.nextInt();
}
void displayspeed(){
System.out.println("The speed = "+speed+" wpm");
}}




class officer extends staffs
{
String grade;
officer(){
super();
Scanner sc3 = new Scanner(System.in);
System.out.println("Enter the grade ??");
grade = sc3.next();
}
void displaygrade(){
System.out.println("The grade = "+grade);
}}




class regular extends typist
{
int wages;
regular(){
super();
Scanner sc4 = new Scanner(System.in);
System.out.println("Enter the wages ??");
wages = sc4.nextInt();
}}



class casual extends typist
{
int wages;
casual(){
super();
Scanner sc5 = new Scanner(System.in);
System.out.println("Enter the wages ??");
wages = sc5.nextInt();
}
void displaycasual(){
System.out.println("The wages = "+wages);
}}







class demo
{
 public static void main(String[] args)
 {
  int ch;
  Scanner abc = new Scanner(System.in);
  System.out.println("Enter ur choice 1/2/3/4 ?");
  ch=abc.nextInt();
  switch(ch)
  {
   case 1: teacher a = new teacher();
	   sc1.displayStaff();
  	   sc1.displayteacher();
	   break;
   case 2: typist b = new typist();
	   sc2.displayStaff();
  	   sc2.displayspeed();
	   break;
   case 3: casual c = new casual();
	   sc5.displayStaff();
  	   sc5.displaycasual();
	   break;
   case 4: officer d = new officer();
	   sc3.displayStaff();
  	   sc3.displaygrade();
	   break;
  default: System.out.println("Invalid Choice");
	}
    }
}
Posted
Updated 2-Jul-22 23:22pm

1 solution

Let's think about what your code does.
We have two cars between us: my car (a black Mercedes) and your car (a green BMW).
They have a lot of attributes in common: they both have four wheels, the both have a drivers seat, they both have a glovebox.

Suppose you put your mobile in the glovebox of your car, and we go for a ride in mine. Can you access your mobile by opening my glovebox?
Of course you can't - you know that each car is a separate entity and the glovebox isn't "shared" by all cars!

So you also know that if you want to access your mobile, you need to open your car, open that glovebox, and retrieve it.

Classes act the same way.When you declare a variable in a class, it is part of that class, and isn't accessible outside it unless you reference the specific instance of the class.

But what you have done is worse: you have created the Scanner variables inside the class constructor method - and all variables created in a method are local to that method: they are destroyed as soon as the method exits. It's as if the glovebox in a car was emptied automatically when you exit the vehicle! Your mobile that you put in your car's glovebox has been discarded, and you can't access it ever again!

So look at your code:
case 1: teacher a = new teacher();
    sc1.displayStaff();
    sc1.displayteacher();
    break;
You create an instance of a teacher, but you never try to use it: instead, you assume that one of the (now destroyed) variables will exist in the ether because you have created it: that a mobile phone will magically appear on the table beside you when you buy a car!

Try using the instance of a teacher you created instead - and go back to your course notes and read them again!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900