June 2018
Beginner
722 pages
18h 47m
English
The instance of operator is very helpful. We have used it several times in this book. But, there are situations that may require us to revisit the decision to use it.
To illustrate this tip, here is some code that can benefit from polymorphism instead of using the intanceof operator:
class ClassBase {}class ClassY extends ClassBase { void method(){
System.out.println("ClassY.method() is called"); }}class ClassZ extends ClassBase { void method(){ System.out.println("ClassZ.method() is called"); }}class SomeClass{ public void doSomething(ClassBase object) { if(object instanceof ...
Read now
Unlock full access