Templates are possible using Java generics. However, in Java, interfaces are much more popular than inheritance. This is because Java does not support multiple inheritance.
In C ++, you can extend several classes.
class X : public A, public B, private C { }
Since C ++ makes no distinction between virtual and non-virtual classes, only virtual and non-virtual members, there is no formal difference between classes that may or may not have virtual methods.
Java, on the other hand, has three different types of classes:
- Concrete (completely non-virtual, all methods specific)
- Abstract (partially virtual, partially not virtual)
- Interface (fully virtual, without specific methods)
In Java, virtual methods are called abstract methods, so I will call them here.
Java also has generics. Generics are similar to templates syntactically, but completely different in terms of implementation. I will leave the introduction study to you (the keyword to use in your search is βerasureβ).
Designations are declared in the class. For example, Java, the equivalent of C ++ vector , is an ArrayList class. Its use is very similar:
List<String> strs = new ArrayList<String>();
Here, List is a kind of supertype of ArrayList . List is an interface, and ArrayList implements it. List itself declares a generic type in its declaration like this:
public interface List<E> ... {
Notice how get returns E Here E not defined specifically. This is a generic type, in which case you can use anything in its place, String , Object , Boolean , your own classes, etc.
ArrayList implements List , and also declares a generic type:
public ArrayList<E> implements List<E>, ... { // ... public E get(int index) { // bounds check return elements[i]; } // ... }
You can also restrict types to generics using type restrictions. This is a bit beyond that, since what you want is not really generics. You need interfaces.
In your example, you first declare an interface with newItem .
public interface A { void newItem(); }
Each method should come from a specific implementation or interface, hence the need for this interface. In particular, this interface will be equivalent to SuperSuper . This is a fully virtual class (interface). Now for your classes:
public class B implements A { @Override public void newItem() {
Your last bit is a bit confusing to someone like me without much experience with C ++, but I assume that you say that you want to create a template type using the newItem method. In Java, this will be another implementation of the A interface, or you can extend B or C and override their methods.
public class D extends B { @Override public void newItem() {
Then using something with the newItem method is as simple as:
A a = new B(); // or "new C()" or "new D()" a.newItem();
Just as you can say
SuperSuper* a = new SubSuperA();
In Java, you can do the same with interfaces, abstract classes, and concrete classes, as I did above.
So, I believe interfaces are the solution to your problem. I highly recommend reading how interfaces are used in Java. They provide powerful typing with no multiple inheritance. I will find a couple of links very quickly.
I hope for help. Let me know if you need any clarification.