2

I want to fill values in my arraylist of abstract class type.Below is my code

public abstract class Account {
    private int accountId;
    private int customerId;
    private double balance;

    public Account(int accountId, int customerId, double balance) {
        this.accountId = accountId;
        this.customerId = customerId;
        this.balance = balance;
    }

    public abstract double deposit(double sum);
    public abstract double withdraw(double sum);
}

Above is my abstract class. Now I have another class bank in which i want to define and declare an arraylist in which i can fill my values . I declared arraylist as

ArrayList<Account> al=new ArrayList<>();

Now i want to pass values to this arraylist for further use but i couldnot as we cannot instantiate abstract class.I tried this code to fill values in class with main method but couldnot get it because of above reason

Account ac= new Account(1,100,25000);
    ArrayList<Account>ac= new ArrayList<Account>();
    ac.add(ac);
5
  • 1
    Sure, you need to create a concrete class extending Account (even an anonymous one if you want) . An abstract class has no use without concrete implementations, because some behavior has not been defined. Commented Apr 1, 2016 at 13:15
  • 4
    Note that this question doesn't really have anything to do with ArrayList - just the abstract class and the attempt to create an instance of it are enough. Commented Apr 1, 2016 at 13:19
  • As you have said, you cant instantiate. So the line Account ac= new Account(1,100,25000); is impossible. You have to create another class extending your abstract class. Commented Apr 1, 2016 at 13:20
  • As a secondary matter, I've reformatted your code to be a lot more readable - I'd advise using a format like this for future questions. Most IDEs will reformat the code for you automatically if you ask them to. Commented Apr 1, 2016 at 13:21
  • Another thing is that this will not compile because you are using ac variable twice. Commented Apr 1, 2016 at 13:23

5 Answers 5

1

The whole point of your abstract class is to factorize some code in your application. Using it as a super type is in my opinion a bad practice since you should be using interfaces for that.

To get a complete response to your problem, I would:

Create an interface: Account.java

public interface Account {
    public double deposit(double sum);
    public double withdraw(double sum);
}

Create an abstract class: AbstractAccount.java

public abstract class AbstractAccount {
    protected int accountId;
    protected int customerId;
    protected double balance;
    public Account(int accountId, int customerId, double balance) {
        this.accountId = accountId;
        this.customerId = customerId;
        this.balance = balance;
    }
}

And finally provide a default implementation for your interface BankAccount.java

public class BankAccount extends AbstractAccount implements Account {
    public Account(int accountId, int customerId, double balance) {
        super(accountId, customerId, balance);
    }
    public double deposit(double sum) {
        this.balance += sum;
    }
    public double withdraw(double sum) {
        this.balance -= sum;
    }
}

Then you should manipulate:

List<Account> accounts = new ArrayList<Account>();
accounts.add(new BankAccount(1, 1, 10000));

and never care about the implementing type :)

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

Comments

1

Abstract classes can't be instantiated, but they can be extended. If the child class is concrete, it can be instantiated.

You would also have to implement both your abstract methods to make the class concrete.

Read more here: Java inheritance.

Comments

1

You can add this following code just to get you started :

public class ConcreteAccount extends Account{
    public ConcreteAccount (int accountId, int customerId, double balance) {
        super(accountId, customerId, balance);
    }

    public abstract double deposit(double sum) {
       //implementation here
    }
    public abstract double withdraw(double sum) {
       //implementation here
    }
}

Then after that, you can have :

Account ac= new ConcreteAccount(1,100,25000);
ArrayList<Account> acList= new ArrayList<Account>();
acList.add(ac);

3 Comments

I followed your code but it gave me compilation error may be because we cannot assigned parent class object to child class reference.but on using ConcreteAccount ac=new ConcreteAccount(1,2,5000) it works fine
oh my bad... I've switched them. It is already fixed. It should work already when compiled. @Er.Naved Ali
I found it correct now..Thanks for the help :)@JanLeeYu
0

Marking a class abstract means it might have unimplemented methods, therefore, you're not able to create an instance of an abstract class directly due to undefined behavior. What you can do is to define a non-abstract class which extends your Account class and implement the two abstract methods in Account. Something like class BankAccount extends Account { implementations }. After that, you can create instances of class BankAccount and add them into your ArrayList instance. Other instances of class that extends Account can also be added into your ArrayList instance.

Comments

-1
import java.util.ArrayList;
import java.util.Random;

abstract class Disease {
  private String name;
  private int duration;

  public Disease(String name) {
    this.name = name;
    this.duration = new Random().nextInt(12) + 3;
  }

  abstract String typeDisease();

  public String getName() {
    return name;
  }

  public int getDuration() {
    return duration;
  }
}

class Virus extends Disease {
  private boolean vaccinated;

  public Virus(String name, boolean vaccinated) {
    super(name);
    this.vaccinated = vaccinated;
  }

  @Override
  String typeDisease() {
    return "Virus";
  }

  public boolean isVaccinated() {
    return vaccinated;
  }
}

class Bacteria extends Disease {
  private String antibiotic;

  public Bacteria(String name, String antibiotic) {
    super(name);
    this.antibiotic = antibiotic;
  }

  @Override
  String typeDisease() {
    return "Bacteria";
  }

  public String getAntibiotic() {
    return antibiotic;
  }
}

class Patient {
  private String name;
  private String surname;
  private String mbo;
  private ArrayList<Disease> diseases = new ArrayList<>();

  public Patient(String name, String surname, String mbo) {
    this.name = name;
    this.surname = surname;
    this.mbo = mbo;
    diseases.add(new Virus("Chicken pox", false));
    diseases.add(new Virus("Corona", true));
    diseases.add(new Bacteria("Angina", "Penicillin"));
  }

  public int countSickness() {
    int totalDuration = 0;
    for (Disease disease : diseases) {
      totalDuration += disease.getDuration();
    }
    return totalDuration;
  }

  public void printIbolesti() {
    System.out.println("Patient: " + name + " " + surname + "(" + mbo + ")");
    for (Disease disease : diseases) {
      System.out.println("Disease: " + disease.getName() + " Type: " + disease.typeDisease() + " Duration: " + disease.getDuration());
    }
  }
}

public class PatientTest {
  public static void main(String[] args) {
    Patient patient = new Patient("John", "Doe", "123456789");
    patient.printIbolesti();
    System.out.println("Sick days: " + patient.countSickness());
  }
}

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.