Can you have a multiple-type variable declaration in Java?

java generics multiple types
java variable types
data types and variables in java
java type
java class type
type declaration java
java union type

I have multiple interfaces and a class that implements all of them. I want to call all the methods defined in these interfaces on an instance of the implementing class, but I don't want to declare the variable as of the class type so that I'm still programming to interfaces.

It seems that I have either to:

  1. Cast the object from one interface type to another to call the appropriate methods.
  2. Create a union interface which extends the others and declare a variable of that type.

I would prefer not to do either of these because 1 is not type-safe and 2 can mean you quickly end up with a lot of 'union' interfaces which add nothing in themselves. Is it possible to have something like:

public interface A {
    public void doThis();

public interface B {
    public void doThat();

public class C implements A, B {
    public void doThis(){;}
    public void doThat(){;}

//Fake calling code:
public void go() {
    A,B dualTypedInstance = new C(); //


It really seems like this should be possible. Thanks.

You can do this:

ImplementsInterfaces impl = new ImplementsInterfaces();
A a = impl;
B b = impl;

Can we have multiple type parameters in generic methods in Java?, The Java Tutorials have been written for JDK 8. you create a generic type declaration by changing the code "public class Box" to As you can see, all occurrences of Object are replaced by T. A type variable can Multiple Type Parameters. Here the type is the Java supported data types, or the class name, or interface name. The identifier is the name of the variable. To initialize a variable in Java, use equal sign and a value. To declare more than one variable of the same type, use a comma to separate. Example - Declaring Variables in Java In the following Java program, it is declaring multiple variables, and some of them include an initialization.

it's not completely impossible to do this in 2019, though I wouldn't necessarily recommend the solution in production code, and it also carries the caveat that the object has to be immutable. For example, to create a class that effectively has an instance variable that implements InterfaceA and InterfaceB, which have methods methodA() and methodB(), respectively, you can do something like

public interface MyInterface {
  public void methodA();
  public void methodB();

  public static <O extends InterfaceA & InterfaceB> MyInterface of(O obj){
      return new MyInterface(){
          public void methodA(){

          public void methodB(){

This creates an anonymous inner class that effectively has a final instance variable of types InterfaceA and InterfaceB, without demanding any particular implementation of that dependency.

Generic Types (The Java™ Tutorials > Learning the Java Language , suppose that you are able to define a new class that extends multiple classes. Because interfaces do not contain fields, you do not have to worry about The Java programming language supports multiple inheritance of type, which is the a variable is declared to be the type of an interface, then its value can reference� In Java, you can think of the compiler as the bucket police. It ensures that programmers declare and use variables properly. It ensures that programmers declare and use variables properly. To declare a variable in Java, all that is needed is the data type followed by the variable name :

Why don't you want to instantiate the class (c) that implements (a) and (b)?

Another way you could to this is to create abstract classes out of (a) and (b), and have two abstract methods (one in each as you already have), then you create the class (c) which then extends (instead of implements) the other classes.

Multiple Inheritance of State, Implementation, and Type (The Java , By following these guidelines, you will produce code that is less prone to bugs and runtime Declaring multiple variables in a single declaration could cause confusion about the types of variables and their initial values. Declaration of multiple variables per line can reduce code readability and lead to programmer confusion. When more than one variable is declared in a single declaration, ensure that both the type and the initial value of each variable are self-evident.

One alternative is the creation of a abstract super class:

public abstract class AbstractAB implements A, B {

public class C extends AbstractAB {
    public void doThis(){;}
    public void doThat(){;}


public void go() {
    AbstractAB dualTypedInstance = new C();

38. Do not declare more than one variable per declaration, A variable can be thought of as a container which holds value for you during the life The Java variables have mainly three types : Local, Instance and Static. To declare a variable, you must specify the data type & give the� The number of arguments can be found out using a.length, the way we find the length of an array in Java. Note: A method can have variable length parameters with other parameters too, but one should ensure that there exists only one varargs parameter that should be written last in the parameter list of the method declaration.

Interfaces should contain a distinct abstraction unit, so that they can be used separately, i.e. without knowledge of their implementation and without dependence on other interfaces that could possibly be implemented in a class. Interfaces can extend other interfaces, but the hierarchy is not a thing in itself, it is something that should have some meaning within your application. So, if you are forced to make many artificial 'marker' interfaces, the odds are quite high that you are doing something wrong. Last but not least, you can have an abstract base class that can implement the necessary interfaces.

Let me illustrate this with a simple example. Suppose you are creating a frond-end for an application. You can think of different panels that can be draggable, or realizable. Putting it in java terms, there can be two interfaces: Draggable with a drag() method and Resizable with redize() method. Under a drag event your API shouldn't be aware of the implementating class details, the only thing it should care of is, does this class implement Draggable, or not. But it you'd like to have a concrete implementation that has both interfaces implemented in a possibly different ways, you can create an abstract class as A basis for concrete implementations: abstract class BasePanel extends Panel implements Draggable, Resizable. This way, you declaed your class in a way that would be hsed in a separate 'drag' a d 'resize' context. On the other hand, if you wanted to have a context that would be aware of them both you could create another interface that extends both basic interfaces and possibly adds another methods like public interface GuiUnit extends Draggable, Resizable.

Finally, my idea was to show you that you should avoid design situations when you expect conditional casting between unrelated interfaces and either base your implementations on exactly one interface that you expect to deal with, or on some set of abstract classes. If you think that it's not enough - you'd be better off rethinking your design considerations.

Java Variables and Data Types with EXAMPLE, An intersection type combines multiple types into one. You will mostly see intersection types used for mixins and other concepts that don't fit in the The -- strictNullChecks flag fixes this: when you declare a variable, it doesn't automatically� [one_half last=”no”] [/one_half]Variables in Java Programming is something that can be changed, such as a characteristic or value. If we consider programming concept then we can say that variable is something which can store a value. It is container for storing a value.Variable name may be given by programmer.

Advanced Types � TypeScript, Declaring multiple variables of the same data type - Learn Java. Kakra Detome. Loading Duration: 11:09 Posted: Mar 21, 2017 The maximum value is 2147483647. If you want a minus number the lowest value you can have is -2147483648. If you want larger or smaller numbers you can use another number variable type: double. You'll meet them in the next part of the course. <-- Sharing your Programmes | Double Variables --> Back to the Java Contents Page

30. Declaring multiple variables of the same data type, We now have a local variable number whose value is 42 and whose type is Int ( because that's the type of Then, you can declare it with val (for "value") instead: . The generic type “object” is really the smallest mutuality all objects in java possess. So you may declare a variable “object myObject” and assign any object to it. So this could be helpful if you have e.g. a method which has an object as argument and it might be of several types - so you pass the object in this generic way.

Declaring variables, You have to tell Java the type of the variable because Java needs to know how many bits to use and how to represent the value. The 3 different primitive types are� Java Variables. Variables are containers for storing data values. In Java, there are different types of variables, for example: String - stores text, such as "Hello". String values are surrounded by double quotes; int - stores integers (whole numbers), without decimals, such as 123 or -123