Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback, please email oracle-forums_moderators_us@oracle.com

Re: Abstract Class

NickR2600-OracleMay 24 2018 — edited May 29 2018

Great question!  You're absolutely correct.  Abstract classes may contain methods with implementations.  These are called concrete methods.  Abstract classes may also contain methods without implementations.  These are called abstract methods.  To make a method abstract, you'd replace everything within the curly braces with a semi-colon.  So for example, an abstract printDetails() method would look like this:

public void printDetails();

Concrete classes, like SavingsAccount and CheckingAccount, can't contain abstract methods.  That means at some point in the inheritance structure, you'll need write a version of these methods with an implementation.  Otherwise, you won't be able to create instances of those classes.  In the case of SavingsAccount, this would be:

public void printDetails(){

     System.out.println(accountType +" #" +accountNum);

     System.out.println("Account Owner: " +accountOwner);

     System.out.println("Balance: $" +balance);

     System.out.println("Interest Rate: " +interestRate);

     System.out.println("");

}

And in CheckingAccount this would be:

public void printDetails(){

     System.out.println(accountType +" #" +accountNum);

     System.out.println("Account Owner: " +accountOwner);

     System.out.println("Balance: $" +balance);

     System.out.println("");

}


You'll notice both methods print the account owner and balance.  Generally, we want to reduce duplication like this.  It makes the code more maintainable if this functionality were written in a single place rather than duplicated.  This is because if we realize we've made a mistake or need to have additional functionality, we'd only need to make edits in one place.  This makes the printDetails() method of the Account super class the perfect place write this functionality once.  But doing so means the method can no longer be abstract.

Comments
Post Details
Added on May 24 2018
3 comments
580 views