Page 1 of 1

About Question enthuware.ocajp.i.v8.2.2012 :

Posted: Sat Mar 17, 2018 10:48 pm
by nagarjuna19
Hi All,

Can anyone explain, why java interface is of Type and class has state.

"Java supports multiple type inheritance but not multiple state inheritance."

Thanks,
Nagarjuna

Re: About Question enthuware.ocajp.i.v8.2.2012 :

Posted: Sat Mar 17, 2018 10:52 pm
by admin
interface, class, and enum are all type of types. When you define any of these, you are actually defining a type. Class has instance fields, which means, only class can define state.
Since a class can implement multiple interfaces, that means the class in inheriting multiple types. Since a class can extend from only 1 class, Java cannot inherit multiple states.

Re: About Question enthuware.ocajp.i.v8.2.2012 :

Posted: Sun Mar 18, 2018 5:59 pm
by nagarjuna19
Thank you

Re: About Question enthuware.ocajp.i.v8.2.2012 :

Posted: Thu Oct 10, 2019 2:25 pm
by TamasBuri
Hi,

I am a little bit confused. You wrote :
"This is an important concept and is explained in more detail here: https://docs.oracle.com/javase/tutorial ... tance.html"
I readed that topic but I don't understand. It says:
"One significant difference between classes and interfaces is that classes can have fields whereas interfaces cannot."

But I know interface has fields. They are implicitly public static final fields.

In JLS 8 chapter 9.3 (Field (Constant) Declarations) page 285 says the same:
"Every field declaration in the body of an interface is implicitly public, static,
and final. "

Which one is true?

Re: About Question enthuware.ocajp.i.v8.2.2012 :

Posted: Thu Oct 10, 2019 10:47 pm
by admin
JLS is more precise.
But the oracle tutorial is not wrong either because from the context of that tutorial, it is clear that the Oracle tutorial is talking about "state of an object" i.e. instance fields, which only classes can have.

Re: About Question enthuware.ocajp.i.v8.2.2012 :

Posted: Fri Oct 11, 2019 3:54 am
by TamasBuri
Thank you