user2128433
user2128433

Reputation: 31

cannot find the symbol error in inheritance program in java

import java.io.*;
import java.lang.*;
import java.util.*;
class First 
{
    public int No;
    public String Name; 
    void getDetails() throws IOException
    {
    Scanner sc = new Scanner(System.in);
    No = sc.nextInt();
    Name = sc.nextLine();
    }   
    void showDetails() throws IOException
    {
    System.out.println("The No and Name entered is: " +No  +"\t" +Name);
    }
}

class Second extends First
{
    public double Salary;   
    public void showSalary() throws IOException
    {
    BufferedReader br = new BufferedReader ( new InputStreamReader (System.in) );
    Salary = Double.parseDouble(br.readLine() );
    System.out.println(" The salary for no:" +No +"is" +Salary);
    }
}

class Demo_Inheritance 
{
    public static void main(String s[]) throws IOException
    {
     Second sd = new Second();
     sd.getDetails();
     sd.showDetails();
     sd.showSalary();
    First fs = new First();
    fs.getDetails();
    fs.showDetails();
    fs.showSalary(); }
}

When i execute this coding, im getting the error in the last line as " fs.showSalary() "-> cannot find the symbol ". (dot)"

Upvotes: 0

Views: 356

Answers (2)

Rohit Jain
Rohit Jain

Reputation: 213193

Compiler always resolve the method invocation or field access, based on the declared type of the reference on which it is accessed.

Since declared type of fs is First, compiler will look for the method declaration of showSalary() in First class, but it can't find one. And hence it gives the compiler error.

Upvotes: 2

Reimeus
Reimeus

Reputation: 159754

The method showSalary is not defined in First. You need to either add the method or use an instance of Second to use the method

Upvotes: 2

Related Questions