0

Exception in thread "main" java.lang.Error: Unresolved compilation problem: No enclosing instance of type jasz is accessible. Must qualify the allocation with an enclosing instance of type jasz (e.g. x.new A() where x is an instance of jasz

how to resolve the issue

problem Statement

When working on overriding concepts, We implemented a method for updating balance in Customer Accounts. But we did not write any implementation for Account class, as we can not determine any logic until we knew the type of account. In most cases, this situtation often comes up where we would want the method to be present as part of inheritance hierarchy but we can not determine its implementation in the parent class. This would land up in a bigger issue, if any body tries to create an object of the parent class and invokes the method.

Abstract class to the rescue !!!!

Any method for which implementation is unknown can be marked as Abstract method. (prefix keyword abstract). If any method in the class is marked abstract, then the class as well is marked as abstract. Moreover, Objects cant be created for an abstract class thus addressing the above issue.

Hmmm.. Lets try it out. We need a method to calculate service tax on every transaction. To keep things simple, lets assume its 10% for Savings, 20% for Checking and 5% for Demat. Make the method abstract in Account class and override in child classes.

Create a abstract class Account with 3 private data member variables -accountNumber of type String, balance of type double , holderName of type String and methods - display() and an abstract method calculateServiceTax() with an argument of type int and return type as double;

Create the class CheckingAccount which extends the class Account

Create the class SavingsAccount which extends the class Account .

Create the class DematAccount which extends the class Account .

Use Appropriate Getters Setters for above classes.

Create a driver class named Main which creates an instance of the above mentioned classes. Service Tax must be calculated seperately for all the base class (value must be round to 2 decimal place).

Sample Input and Output 1: [All text in bold corresponds to input and the rest corresponds to output.]

Enter the Account Type

1.Checking Account

2.Saving Account

3.Demat Account

1

Enter the Holder Name

Jimesh Sharma

Enter the Account Number

23845596HJ

Enter the Current Balance

550

Enter the Amount to be Transfer

200

Transfer Completed Successfully !!!

Holder Name :Jimesh Sharma

Account Number :23845596HJ

Your remaining balance is Rs.310.00

Program

import java.io.*;
import java.lang.*;
import java.util.*;
class jasz
{
public abstract class Account
{
    private String accountNumber;
    private double balance;
    private String holderName;
    double trans;


    void display(String s1,String s2,double s3,double s4)
    {
        if(s4>s3)
        {
            System.out.println("No sufficient balance in your account");
        }
        else
        {
            System.out.println("Transfer Completed Successfully !!!");
            System.out.println("Holder Name :"+s1);
            System.out.println("Account Number :"+s2);
            System.out.println("Your remaining balance is Rs."+(s3-s4));
        }
    }
abstract double calculateServiceTax(double balance,double trans);
}
public class CheckingAccount extends Account
{

    @Override
    double calculateServiceTax(double balance, double trans) {
        double c3=((trans*20)/100)+trans;
        return c3;
    }


}
public static void main(String args[])
{
    Scanner s=new Scanner(System.in);
    int a=s.nextInt();
    switch(a)
    {
    case 1:
    {
        System.out.println("Enter the Holder Name");
        String str=s.nextLine();
        System.out.println("Enter the Account Number");
        String str2=s.nextLine();
        System.out.println("Enter the Current Balance");
        double str3=s.nextDouble();
        System.out.println("Enter the Amount to be Transfer");
        double str4=s.nextInt();
        CheckingAccount c=new CheckingAccount();
        Account a2=new CheckingAccount();
        double c3=a2.calculateServiceTax(str3, str4);
        a2.display(str, str2,str3, c3);
    }
        }
}


}
  • 1
    Make `Account` and `CheckingAccount` static, i.e. `public abstract static class Account` and `public static class CheckingAccount`, or declare them in their own files. – Andy Turner Jun 02 '16 at 15:03
  • To add on a bit more, your `case` statment needs to end with a `break`. Further on follow the Java naming convention and start classenames with an uppercase letter. – SomeJavaGuy Jun 02 '16 at 15:07

0 Answers0