Last active
December 5, 2017 17:42
-
-
Save aaomidi/1f6a66de73c9e2048f9a7958d389f84c to your computer and use it in GitHub Desktop.
Funky Java :)
This file contains 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
public abstract class AbstractClass { | |
public void printClass() { | |
System.out.println(this.getClass().getName()); | |
System.out.println(getClass().getName()); | |
} | |
} |
This file contains 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
// Here's an alternative to abstract class. | |
public interface AbstractClass { | |
default void printClass() { | |
System.out.println(this.getClass().getName()); | |
System.out.println(getClass().getName()); | |
} | |
void foo(); | |
} | |
public class Main { | |
public static void main(String... args) { | |
AbstractClass cat = new Cat(); | |
AbstractClass dog = new Dog(); | |
AbstractClass abstractClass = () -> {}; | |
cat.printClass(); | |
dog.printClass(); | |
abstractClass.printClass(); | |
} | |
} | |
// Same Cat and Dog classes using implements instead of extends with empty foo methods. | |
// This one is even funkier (But java 8+ only). |
This file contains 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
public class Cat extends AbstractClass {} |
This file contains 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
public class Dog extends AbstractClass {} |
This file contains 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
public class Main { | |
public static void main(String... args) { | |
AbstractClass cat = new Cat(); | |
AbstractClass dog = new Dog(); | |
AbstractClass abstractClass = new AbstractClass() {}; | |
cat.printClass(); | |
dog.printClass(); | |
abstractClass.printClass(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment