1

In Java, Can an object have several different classes ?

If yes , how should Inheritance and Interface influence to it?

2

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for a thorough yet concise answer. Java's "Simple white paper" has this to say about multiple inheritance: "Multiple inheritance--and all the problems it generates--was discarded from Java. The desirable features of multiple inheritance are provided by interfaces."
1

Yes

class A{}

class B extends A{}

void foo()
{
  B b = ...; // here object b is of type B and A(by inheritance)

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.