Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

java.lang.Object

 
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why don't interfaces inherit from java.Lang.Object ?

and what does this statement mean - though an instance of an object that implements an interface inherits java.lang.
Object, the interface itself doesn’t; otherwise, Java would support multiple inheritance
for objects, which it doesn’t.


especially the text in bold
 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:why don't interfaces inherit from java.Lang.Object ?



Considering that it is not possible to ever create an instance that doesn't subclass directly (or indirectly) from the Object class, what would it matter? All instances can access the methods of the Object class (access permissions aside, of course), no matter what reference you use.

Henry
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok get it !

but why is it said that : otherwise java would support multiple inheritence
what has that got to do with an interface not inheriting from Object ?
 
Enthuware Software Support
Posts: 4450
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface and Object class are two entirely different concepts. An interface defines a behavior while an Object defines the an actual "object", a real "thing". It is nonsensical to expect an interface to inherit an Object. It is like expecting a "building plan" to extend a brick.
So to answer your question, "otherwise java would support multiple inheritence
what has that got to do with an interface not inheriting from Object ?", multiple inheritance has got nothing to do with interface not inheriting from Object.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found that statement in the oca book. so couldn't understand why it was really there
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:i found that statement in the oca book. so couldn't understand why it was really there


Which one? And please provide some details about where you've found that statement (chapter, page number,...)
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its on page 348 of the e - book by jeanne and soctt, sybexbook , in answers to the review questions of chapter 5 - question 10.

i'm posting below the question and the answer with the particular statement highlighted.

10. Which statements are true for both abstract classes and interfaces? (Choose all that apply)
A. All methods within them are assumed to be abstract.
B. Both can contain public static final variables.
C. Both can be extended using the extend keyword.
D. Both can contain default methods.
E. Both can contain static methods.
F. Neither can be instantiated directly.
G. Both inherit java.lang.Object.



ANSWER :
10. B, C, E, F. Option A is wrong, because an abstract class may contain concrete methods.
Since Java 8, interfaces may also contain concrete methods in form of static or
default methods. Although all variables in interfaces are assumed to be public static
final, abstract classes may contain them as well, so option B is correct. Both abstract
classes and interfaces can be extended with the extends keyword, so option C is correct.
Only interfaces can contain default methods, so option D is incorrect. Both
abstract classes and interfaces can contain static methods, so option E is correct. Both
structures require a concrete subclass to be instantiated, so option F is correct. Finally,
though an instance of an object that implements an interface inherits java.lang.
Object, the interface itself doesn’t; otherwise, Java would support multiple inheritance
for objects, which it doesn’t.
Therefore, option G is incorrect.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the text in bold in the above post is exactly what i had put up in my original post.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone please answer to this
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PatienceIsAVirtue
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:Finally, though an instance of an object that implements an interface inherits java.lang.Object, the interface itself doesn’t; otherwise, Java would support multiple inheritance for objects, which it doesn’t.[/b] Therefore, option G is incorrect.


Although I can see why the authors are throwing in the multiple inheritance for objects here, I don't really agree with the explanation.

Let's assume an interface extends java.lang.Object (which again it doesn't), you could have the following two interfaces (the syntax is wrong of course as an interface can't extend a class):Now you could create a class Ball which implements both interfacesAnd because both interfaces extend from java.lang.Object, the class Ball (indirectly) extends from two classes which is not allowed/supported in Java. But in this example it's actually only one class (namely java.lang.Object) and that's why I don't really agree.

But if an interface could extend from java.lang.Object, it could also extend from any other class (and then you would definitely have multiple inheritance). Let's update the interface declarationsNow the class Ball (which still implements both interfaces Moveable and Bounceable) would (indirectly) extend from multiple (two) classes (MoveableObject and BounceableObject) which is not allowed/supported in Java.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmmm.......... fine then ! thanks a lot !
 
Attractive, successful people love this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic