From The Java Tutorials:
In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement. This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.
Can anyone provide me a basic pseudo type for this. I did not understand the bold lines.
11 Answers 11
Let's declare two interfaces and a class that implements them both:
interface I1 { }
interface I2 { }
class C implements I1, I2 { }
objects can have multiple types
In the following code, it can be seen that a C instance has the type of C as well as I1 and I2:
C c = new C();
boolean isC = (c instanceof C); //true
boolean isI1 = (c instanceof I1); //true
boolean isI2 = (c instanceof I2); //true
Now let's declare a class B which implements I1 as well:
class B implements I1 { }
if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.
If we declare a variable of type I1, we can set it to an instance of C, and then reassign it to an instance of B:
I1 i1 = new C();
i1 = new B();
We can also reassign it to an instance of D, where D extends C:
i1 = new D();
...
class D extends C { }
Comments
Consider the following example:
Serializable s = new ArrayList();
In Java, this is valid code, even though Serializable is an interface, because ArrayList implements Serializable. So in this case, we're treating s as a variable of type Serializable.
Now suppose we follow up the above code with the following:
s = "String object";
This is also valid becauseString also implements Serializable. Since we declared s as type Serializable, it can point to any object that implements that interface.
Comments
objects can have multiple types
Consider the following snippet:
public class MyClass extends ParentClass implements Interface1, Interface2 {
//some code
}
This class can be used in different places as follows:
MyClass m1 = new MyClass();
ParentClass p = new MyClass();
Interface1 i1 = new MyClass();
Interface2 i2 = new MyClass();
variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.
Consider the last two lines in the previous snippet, a variable of type Interface1 can reference any object that implements this interface, so if we have another class implements Interface1, say MyClass2, then
Interface1 i1 = new MyClass();
Interface1 i2 = new MyClasss2();
i1 = i2;
i1 = new MyClass2();
All the previous assignments are valid because MyClass and MyClass2 implements Interface1
Comments
class Ball extends Rubber implements Jumping, Rolling, Squeezing {
public void jump(){}
public void roll(){}
public void squeeze(){}
}
Ball b = new Ball();
Jumping j = new Ball();
j.jump();
//j.roll(); //CTE: Cannot resolve method roll()
((Ball) j).roll(); //but it still can be called if explicit cast to type Ball is used
Comments
String implements multiple interfaces, so it has multiple types:
String s = "A String";
Comparable<String> comp = s;
CharSequece cs = s;
Serializable ser = s;
The interface CharSequence is implemented by multiple classes, so a CharSequence reference can hold all kinds of objects:
CharSequence cs = "A String";
cs = new StringBuilder();
cs = new Segment();
Comments
objects can have multiple types
Example:
public class Foo implements Runnable, Callable<Integer> {
public void run() {}
public Integer call() {return 1;}
}
Foo foo = new Foo();
Runnable r = foo;
Callable<Integer> c = foo;
Example:
if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface
Runnable r = new Foo();
r = Thread.currentThread(); //Thread implements Runnable
Comments
The statements you quote (from where?) are true but misleading -- objects already have multiple types without interfaces.
For example, "bimmelim" has the type String, but it also has the type Object. Interfaces do not change that, except that "bimmelim" also has the type Serializable, CharSequence and others.
Actually, it is possibly debatable whether we should say that "bimmelim" "has" the type Object, but certainly a reference to it will fit into an Object variables.
If a variable is declared to be type of an interface ... for example
CharSequence x ;
... then its value can reference a String object such as "bimmelim", or it could possibly be a StringBuffer, which is another type that implements CharSequence.
Comments
That the following is a correct assignment:
class AClass implements AInterface {
}
AInterface var = new AClass();
Comments
Very basic example-
List<String> list1=new ArrayList<String>();
Since, ArrayList implements List therefore we can use List interface variable i.e list1 to refer to the object created by Arraylist.
Comments
Consider the following class and interface definitions:
public class A { }
public class B extends A implements I { }
public interface I { }
The following statements are all legal:
A first = new A();
B second = new B();
A third = new B();
I fourth = new B();
Because B implements I and extends A, it can be used as a value anywhere an "I" or an "A" is expected.
Comments
Take the Collection interface from the standard Java libraries as an example. Any variable declared as of type Collection can then be assigned an object of a class which implements the Collection interface, e.g. ArrayList, Stack, ... see the linked doc for more examples.