Java – Method name collision in interface implementation

No, there is no way to implement the same method in two different ways in one class in Java. That can lead to many confusing situations, which is why Java has disallowed it. interface ISomething { void doSomething(); } interface ISomething2 { void doSomething(); } class Impl implements ISomething, ISomething2 { void doSomething() {} // … Read more

Abstract class in Java

An abstract class is a class which cannot be instantiated. An abstract class is used by creating an inheriting subclass that can be instantiated. An abstract class does a few things for the inheriting subclass: Define methods which can be used by the inheriting subclass. Define abstract methods which the inheriting subclass must implement. Provide … Read more

How can I get a list of all the implementations of an interface programmatically in Java?

I have been searching for a while and there seems to be different approaches, here is a summary: reflections library is pretty popular if u don’t mind adding the dependency. It would look like this: Reflections reflections = new Reflections(“firstdeveloper.examples.reflections”); Set<Class<? extends Pet>> classes = reflections.getSubTypesOf(Pet.class); ServiceLoader (as per erickson answer) and it would look … Read more

Interface type check with Typescript

You can achieve what you want without the instanceof keyword as you can write custom type guards now: interface A{ member:string; } function instanceOfA(object: any): object is A { return ‘member’ in object; } var a:any={member:”foobar”}; if (instanceOfA(a)) { alert(a.member); } Lots of Members If you need to check a lot of members to determine … Read more

Custom fonts and XML layouts (Android)

You can extend TextView to set custom fonts as I learned here. TextViewPlus.java: package com.example; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Typeface; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; public class TextViewPlus extends TextView { private static final String TAG = “TextView”; public TextViewPlus(Context context) { super(context); } public TextViewPlus(Context context, AttributeSet attrs) { super(context, attrs); setCustomFont(context, … Read more

What does “program to interfaces, not implementations” mean?

Interfaces are just contracts or signatures and they don’t know anything about implementations. Coding against interface means, the client code always holds an Interface object which is supplied by a factory. Any instance returned by the factory would be of type Interface which any factory candidate class must have implemented. This way the client program … Read more

When to use: Java 8+ interface default method, vs. abstract method

There’s a lot more to abstract classes than default method implementations (such as private state), but as of Java 8, whenever you have the choice of either, you should go with the defender (aka. default) method in the interface. The constraint on the default method is that it can be implemented only in the terms … Read more

tech