This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package inheritanceIsaAndHasA.hasA; | |
| public class Animal { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package polymorphism; | |
| public interface Animatable { | |
| void animate(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package polymorphism.compileTime; | |
| public class PolymorphismExample { | |
| public void doSomething(Worker worker) { | |
| System.out.println("I'm a worker"); | |
| } | |
| public void doSomething(Teacher teacher) { | |
| System.out.println("I'm a Teacher"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package polymorphism.runTime; | |
| import java.util.ArrayList; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| //https://gist.github.com/rajeevprasanna/8500647 | |
| public class PolymorphismExample { | |
| public static void main(String[] args) { | |
| List workers = new ArrayList(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package polymorphism.superclassMethodInvoker; | |
| public class Animal { | |
| public void eat() { | |
| } | |
| public void printYourself() { | |
| // Useful printing code goes here | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package polymorphism.superclassMethodInvoker.exception; | |
| public class Animal { | |
| public void eat() throws Exception { | |
| // throws an Exception | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package polymorphism.overloadingAndoverriding; | |
| public class Animal { | |
| public void eat() { | |
| System.out.println("Generic Animal Eating Generically"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package polymorphism.overloadingWithoutOverriding; | |
| public class Bar extends Foo { | |
| // Don’t be fooled by a method that’s overloaded but not overridden by a | |
| // subclass. It’s perfectly legal to do the following: | |
| void doStuff(String s) { | |
| } | |
| // The Bar class has two doStuff() methods: the no-arg version it inherits |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package typecasting; | |
| public class Animal { | |
| void makeNoise() { | |
| System.out.println("generic noise"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package interfaceExtension; | |
| public class Ball implements Bounceable { | |
| public void bounce() { | |
| } // Implement Bounceable's methods | |
| public void setBounceFactor(int bf) { | |
| } |