Why can’t I define a static method in a Java interface?

Java 8 permits static interface methods With Java 8, interfaces can have static methods. They can also have concrete instance methods, but not instance fields. There are really two questions here: Why, in the bad old days, couldn’t interfaces contain static methods? Why can’t static methods be overridden? Static methods in interfaces There was no … Read more

When to use an interface instead of an abstract class and vice versa?

I wrote an article about that: Abstract classes and interfaces Summarizing: When we talk about abstract classes we are defining characteristics of an object type; specifying what an object is. When we talk about an interface and define capabilities that we promise to provide, we are talking about establishing a contract about what the object … Read more

How do you find all subclasses of a given class in Java?

Scanning for classes is not easy with pure Java. The spring framework offers a class called ClassPathScanningCandidateComponentProvider that can do what you need. The following example would find all subclasses of MyClass in the package org.example.package ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.addIncludeFilter(new AssignableTypeFilter(MyClass.class)); // scan in org.example.package Set<BeanDefinition> components = provider.findCandidateComponents(“org/example/package”); for (BeanDefinition component : … Read more

How do you declare an interface in C++?

To expand on the answer by bradtgmurray, you may want to make one exception to the pure virtual method list of your interface by adding a virtual destructor. This allows you to pass pointer ownership to another party without exposing the concrete derived class. The destructor doesn’t have to do anything, because the interface doesn’t … Read more

Multiple Inheritance in C#

Consider just using composition instead of trying to simulate Multiple Inheritance. You can use Interfaces to define what classes make up the composition, eg: ISteerable implies a property of type SteeringWheel, IBrakable implies a property of type BrakePedal, etc. Once you’ve done that, you could use the Extension Methods feature added to C# 3.0 to … Read more

Type List vs type ArrayList in Java [duplicate]

Almost always List is preferred over ArrayList because, for instance, List can be translated into a LinkedList without affecting the rest of the codebase. If one used ArrayList instead of List, it’s hard to change the ArrayList implementation into a LinkedList one because ArrayList specific methods have been used in the codebase that would also … Read more

tech