2

Say I have a super class with the method backup() that backs up all the objects in an instance variable cardlist. I subclass that class. I want to use the same functionality already written there but I want it to act on the subclasses version of list.

in the subclass I just wrote this:

@Override
public void backUpFlashCards() throws IOException {
    super.backupFlashCards(currentCards);

}  

which calls this method in the superclass:

public void backupFlashCards(ArrayList<Card> cards) throws IOException{
    CARDS = cards;
    backUpFlashCards();
}

... so I basically set the instace variable to what I want it to be in the superclass. Which seems backward(or maybe upside down). Is this the way it should be done ? If I removed the override and then if some other code called that method on the subclass, which would polymorphically called the superclass method how could I have the superclass reference the right instance variable ? What's my problem here and is there a general solution ?

edit:

public class Database implements FlashCardProvider {
   protected String DBURL = "jdbc:mysql://localhost:3306/FlashCardShark?useUnicode=true&useSSl=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
   protected String pw = "Basketball12";
   protected String user = "root";
   private  ArrayList<Card> CARDS = new ArrayList<>();

   @Override
    public void backUpFlashCards() throws IOException {
    int counter = 1;
    Date today = new Date();
    DateFormat df = DateFormat.getInstance();
    String todayFile = df.format(today).replaceAll(" ", "-");
    String todayFile1 = todayFile.replaceAll("/", "-");

    Path p1 = Paths.get("/home/maxbisesi/FlashCardShark/FlashCards/", todayFile1 + ".txt");
    Files.createFile(p1);
    System.out.println(p1);

    BufferedWriter bw = new BufferedWriter(new FileWriter(p1.toFile()));
    // make sure you close this BufferedWriter
    ArrayList<Card> cards = CARDS;

    try {
        bw.write(df.format(today));
        bw.newLine();
        bw.write("Flash Cards \n");
        bw.newLine();

        for (Card c : cards) {
            bw.write("================================================\n");
            bw.write("-" + counter + "-\n");
            bw.write("================================================\n");
            bw.write(c.getCard());
            bw.newLine();
            bw.write("================================================\n");
            bw.write(c.getAnswer());
            bw.newLine();
            bw.write(c.getCategory());
            bw.write("================================================\n");
            counter++;
        }

        bw.close();

    } catch (IOException e) {
        System.out.println("Problem saving cards");
        e.printStackTrace();
    }
}
}

public interface FlashCardProvider {
    void addFlashCard(Card c);
    ArrayList<Card> pullCards();
    void updateRatings(ArrayList<Card> cards);
    void updateFlashCard(Card newCard);
    void backUpFlashCards() throws IOException;
    Card searchByID(int id);
    ArrayList<Card> searchByCategory(String cat);
    String[] getAllCategories();  
    ArrayList<Card> findKeywords(String word);
}


public class LearnOrDieDAO extends Database { 
   private ArrayList<Card> currentCards = new ArrayList<>();

   @Override
    public void backUpFlashCards() throws IOException {
      ...
    }
 }

I'm thinking maybe the subclass could just store all it's cards in the superclass list.

My basic question is: If you want to extend a class, how can you do so that the you don't have to re write functionality to use a subclass instance variable, or if you have to do that what are you doing wrong ?

MadMax
  • 605
  • 1
  • 6
  • 19
  • Possible duplicate of [Polymorphism vs Overriding vs Overloading](https://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading) – abdulwasey20 Feb 22 '19 at 18:07
  • "Say I have a super class with..." Please show the code to go along with your verbal description. See [mcve] for some tips on created a code example. – Code-Apprentice Feb 22 '19 at 18:14
  • 2
    Isn't this code producing an infinite recursion? – LppEdd Feb 22 '19 at 18:14

0 Answers0