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
About Question enthuware.ocajp.i.v8.2.2012 :
Moderator: admin
-
- Posts: 5
- Joined: Sat Mar 17, 2018 6:21 pm
- Contact:
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.2012 :
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.
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.
-
- Posts: 5
- Joined: Sat Mar 17, 2018 6:21 pm
- Contact:
-
- Posts: 6
- Joined: Thu Sep 26, 2019 3:19 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.2012 :
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?
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?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.2012 :
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.
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.
-
- Posts: 6
- Joined: Thu Sep 26, 2019 3:19 am
- Contact:
Who is online
Users browsing this forum: No registered users and 44 guests