What must be done, never how
An interface is a contract: it declares what must be done, never how. Payable demands two methods, pay and isAvailable, with no body.
An interface is a contract: it declares what must be done, never how.
Payable demands two methods, pay and isAvailable, with no body.
An interface is a contract: it declares what must be done, never how. Payable demands two methods, pay and isAvailable, with no body.
The contract moves down to DigitalPaymentMethod, an abstract class that signs it.
It adds state, the active field, and solves isAvailable for all its subclasses.
But pay still has no body: the mold remains incomplete.
The contract moves down to DigitalPaymentMethod, an abstract class that signs it. It adds state, the active field, and solves isAvailable for all its subclasses. But pay still has no body: the mold remains incomplete.
What happens if main tries to create an object of the abstract class?
The compiler rejects it: an abstract class cannot be instantiated.
That object would have a gap: nobody would know how to run pay.
What happens if main tries to create an object of the abstract class? The compiler rejects it: an abstract class cannot be instantiated. That object would have a gap: nobody would know how to run pay.
DigitalWallet extends the mold: it inherits the state and what was already solved.
And it is obliged to fill the gap: it implements pay with its own balance.
No method is left without a body: the contract is complete.
DigitalWallet extends the mold: it inherits the state and what was already solved. And it is obliged to fill the gap: it implements pay with its own balance. No method is left without a body: the contract is complete.
Now new DigitalWallet() does compile: a complete class yields an object.
The variable method has type Payable: from main only the contract is visible.
Now new DigitalWallet() does compile: a complete class yields an object. The variable method has type Payable: from main only the contract is visible.
main calls isAvailable through the interface, without knowing the implementation.
The call goes down to where it was solved: the abstract class answers true.
main calls isAvailable through the interface, without knowing the implementation. The call goes down to where it was solved: the abstract class answers true.
With pay, the call passes through the gap in the mold and reaches DigitalWallet.
The object's code runs: it subtracts the amount and prints the remaining balance.
With pay, the call passes through the gap in the mold and reaches DigitalWallet. The object's code runs: it subtracts the amount and prints the remaining balance.
The interface says what; the abstract class shares state and code; the concrete one completes.
Whoever uses the contract depends on no concrete implementation.
Any class that signs Payable can take its place: the interface is enough.
The interface says what; the abstract class shares state and code; the concrete one completes. Whoever uses the contract depends on no concrete implementation. Any class that signs Payable can take its place: the interface is enough.