An object of a subclass can be used wherever its superclass object is
used. This is commonly known as polymorphism (from a Greek word meaning “many
forms”). In simple terms, polymorphism means that a variable of a supertype can refer to
a subtype object.
Polymorphism means that a variable of a supertype can refer to a subtype object.
dasvis das wrote:I believe superclass cannot be of subclass type. While subclass can be of superclass type and holds superclass type object too with subclass object.
In the code, may I know what is the difference in two statements,
Agree; welcome (again).Stephan van Hulst wrote:Welcome to CodeRanch!
Remember that polymorphism can only be seen at work in instance methods. It doesn't apply to fields, nor to static methods.. . . when you call a method on such a variable . . .
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Carey Brown wrote:Typo error. Check your method names.
Stephan van Hulst wrote:Don't use floating point values for anything other than imprecise measurements. Your account balance should be an integer and your interest rate either an integer or a BigDecimal.
IDs should be strings, not integers.
Make the ID and creation time of an account final.
Make all methods that are not intended to be overridden final. When in doubt, make it final.
Perform parameter validation in your mutators. You should not be able to withdraw or deposit negative amounts. Also, check for integer overflows.
Validate class invariants by throwing exceptions. When you try to withdraw more than the overdraft limit allows, throw an exception.
Don't use long sequences of string concatenations. Use a formatter instead.
Nothing up my sleeve ... and ... presto! A tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
|