Skip to content

Instantly share code, notes, and snippets.

@aaomidi
Last active December 5, 2017 17:42
Show Gist options
  • Save aaomidi/1f6a66de73c9e2048f9a7958d389f84c to your computer and use it in GitHub Desktop.
Save aaomidi/1f6a66de73c9e2048f9a7958d389f84c to your computer and use it in GitHub Desktop.
Funky Java :)
public abstract class AbstractClass {
public void printClass() {
System.out.println(this.getClass().getName());
System.out.println(getClass().getName());
}
}
// 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).
public class Cat extends AbstractClass {}
public class Dog extends AbstractClass {}
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