Reputation: 257
In Java, Can an object have several different classes ?
If yes , how should Inheritance and Interface influence to it?
Upvotes: 1
Views: 958
Reputation: 108937
Yes
class A{}
class B extends A{}
void foo()
{
B b = ...; // here object b is of type B and A(by inheritance)
}
Upvotes: 1
Reputation: 112346
It's not a very well-posed question. Any object can be of one and only one class (ignoring the non-object primitives like int.) On the other hand, a class may have an arbitrary number of superclasses, so your class can match the "is a" relationship of an artitrary number of other classes.
On the interface question, all an interface brings in is a "contract" -- you're making a promise to implement certain methods or have certain properties. This is somewhat similar to multiple inheritance but not really multiple inheritance. Again, implementing an interface means the class has another "is a" property, but it doesn't mean it's fully bringing in the other classes.
Upvotes: 5