Issue
I'm having a problem with this code when I compile BankAccount the compiler says:
BankAccount.java:2: error: BankAccount is not abstract and does not override abstract
method deposit() in Account public class BankAccount implements Account
and I can't figure the problem and solve it. Can you guys please help me with this?
package bankCustomers;
import bankCustomers.Account;
public interface Account{
public int getAccountNum();
public void deposit();
public int getBalance();
}
class BankAccount implements Account{
private int accountNum;
private int balance;
public int getAccountNum(int accountNumber){
if(accountNumber == 1){
return accountNumber;
}
else{
return 0;
}
}
public void deposit(int depositAmount){
if(depositAmount != 0 && depositAmount > 0){
balance += depositAmount;
}
}
public int getBalance(){
return balance;
}
}
Solution
There are two concepts that you need to learn:
- Method overriding - When a subclass changes the inner-workings of the exact method declared in a parent class, or defines the method from an interface
- Method overloading - When a method with the same name appears in a class with different parameter list and or return type
Let explore the two:
Method Overriding
public class Parent {
...
public void method1() {
// DO SOMETHING
}
}
public class Child extends Parent {
...
@Override
public void method1() {
// DO SOMETHING DIFFERENT
}
}
Notice the the entire method declaration in both classes is the same public void method1(). The annotation @Override helps a developer to know that the method is being overridden. If you get a compilation error when using this annotation, it means that the method being overridden doesn't exist in the super (parent) class. To fix this problem, a developer must do one of two things: 1) Remove the annotation, indicating that the method is a brand new method in the subclass, or 2) Create a method with the exact declaration in the parent class.
Method Overloading
public class MyClass {
...
public void method1() {...}
public String method1() {...} // different return type
public void method1(String s1) {...} // different parameter list
public void method1(String s1, int x) {...} // different parameter list than previous
}
Here, the method is said to be overloaded because it has more than one function (4 functions with the same name). This is similar to the use of the + in Java. When use with numbers, the symbol means addition. When it's used with String objects, it means concatenation.
What does this mean to you?
When defining method overriding, I wrote "defines the method from an interface." In your case, you created an interface with three methods (interface methods are inherently public - no need to declare them as such):
int getAccountNum()void deposit()int getBalance()
This means that classes implementing an interface MUST implement ALL of these methods. Now, you did not understand the error: BankAccount is not abstract and does not override abstract method deposit(). What that means is that your class BankAccount is not an abstract class and therefore, is required to implement the methods included in the interface. Abstract classes are exempt from this rule because you cannot create instances of an abstract class. So, classes extending an abstract class that implement an interface MUST conform to the methods included in said interface. For example:
public interface MyInterface {
void method1();
}
public abstract class MyParent implements MyInterface {
// This is a valid abstract class even though it extends an interface
// Implementing the interface methods is delegated down to the children classes
}
public class MyChild extends MyParent {
@Override
public void method1 () {}
}
Notice that both the interface and the concrete class contain a method void method1(). Lastly, notice that the method in the interface does not have an access modifier, but the implementing class declares it as public. This is a property of interfaces. All (abstract) methods of an interface are public by nature.
Lastly, I want to emphasize the importance of using annotations like @Override. For starters, if you don't override the method correctly, you will know immediately. And, as a side benefit, it is a great way to communicate intent.
Answered By - hfontanez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.