JetBrains Academy: The keyword super - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: The keyword super

Bank account:

You are given a class named BankAccount. The class has two fields: number and balance.

Define two classes which inherit from the BankAccount:

  • CheckingAccount containing the double field fee.
  • SavingAccount containing the double field interestRate.

Each new class should have a constructor with three parameters to initialize all fields. Do not forget to invoke the superclass constructor when creating objects.

class BankAccount {

    protected String number;
    protected Long balance;

    public BankAccount(String number, Long balance) {
        this.number = number;
        this.balance = balance;
    }
}

class CheckingAccount extends BankAccount {
    
    protected double fee;

    public CheckingAccount (String number, Long balance, double fee){
        super(number, balance);
        this.fee = fee;
    }
}

class SavingAccount extends BankAccount {

    protected double interestRate;

    public SavingAccount (String number, Long balance, double interestRate){
        super(number, balance);
        this.interestRate = interestRate;
    }
}

Publications:

Below you can see four classes: Publication, Newspaper, Article and Announcement.

You need to override the method getDetails() in the classes inherited from the class Publication. These classes should use getDetails() from Publication to get information about the title and append their own additional data.

class Publication {

    private String title;

    public Publication(String title) {
        this.title = title;
    }

    public String getDetails() {
        return "title=\"" + title + "\"";
    }

}

class Newspaper extends Publication {

    private String source;

    public Newspaper(String title, String source) {
        super(title);
        this.source = source;
    }

    public String getDetails(){
        return super.getDetails() + ", source=\"" + source + "\"";
    }

}

class Article extends Publication {

    private String author;

    public Article(String title, String author) {
        super(title);
        this.author = author;
    }

    public String getDetails(){
        return super.getDetails() + ", author=\"" + author + "\"";
    }

}

class Announcement extends Publication {

    private int daysToExpire;

    public Announcement(String title, int daysToExpire) {
        super(title);
        this.daysToExpire = daysToExpire;
    }

    public String getDetails(){
        return super.getDetails() + ", daysToExpire=" + daysToExpire;
    }

}