0
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)"

1
  • 2
    Your 'First' class does not have a showSalary() function... Commented Sep 27, 2013 at 13:31

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.