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

Moderator: admin

Post Reply
nagarjuna19
Posts: 5
Joined: Sat Mar 17, 2018 6:21 pm
Contact:

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

Post by nagarjuna19 » Sat Mar 17, 2018 10:48 pm

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

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Mar 17, 2018 10:52 pm

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.
If you like our products and services, please help us by posting your review here.

nagarjuna19
Posts: 5
Joined: Sat Mar 17, 2018 6:21 pm
Contact:

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

Post by nagarjuna19 » Sun Mar 18, 2018 5:59 pm

Thank you

TamasBuri
Posts: 6
Joined: Thu Sep 26, 2019 3:19 am
Contact:

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

Post by TamasBuri » Thu Oct 10, 2019 2:25 pm

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?

admin
Site Admin
Posts: 8465
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Thu Oct 10, 2019 10:47 pm

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.
If you like our products and services, please help us by posting your review here.

TamasBuri
Posts: 6
Joined: Thu Sep 26, 2019 3:19 am
Contact:

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

Post by TamasBuri » Fri Oct 11, 2019 3:54 am

Thank you

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests