• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance of Object class.  RSS feed

 
Harsimran Singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need some views on this OOP statement: A java class cannot extend more than one class. So we always extend only one. But again said all of the classes we create are by default EXTENDS from Object class. That means by default any type of sub class actually extending 2 classes, i.e Object class and the super class (which the sub class inheriting to.) Is that TRUE?

- Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsimran Singh wrote:
I need some views on this OOP statement: A java class cannot extend more than one class. So we always extend only one. But again said all of the classes we create are by default EXTENDS from Object class. That means by default any type of sub class actually extending 2 classes, i.e Object class and the super class (which the sub class inheriting to.) Is that TRUE?


The ability to extend more than one class, a.k.a. multiple inheritance, is the ability to extend any two classes (that is not final, of course) of your choosing. Can this work with any two classes you choose? Try it.

BTW, if you scroll down to the "threads other view" section, you can see other topics where this question has come up before.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsimran Singh wrote:Hi,

I need some views on this OOP statement: A java class cannot extend more than one class. So we always extend only one. But again said all of the classes we create are by default EXTENDS from Object class. That means by default any type of sub class actually extending 2 classes, i.e Object class and the super class (which the sub class inheriting to.) Is that TRUE?

- Thanks


What they're saying is not allowed is the one on the right:


You can have a straight line through multiple ancestor classes to Object, but you can't have two or more different paths from your class to Object
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to say this is that a Java class can directly extend only one class. But it can indirectly extend many more, including Object.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Eshwin Sukhdeve
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see if you are using inheritence in your program like this

class MyClass extends TestClass
{
}

then why are thinking that MyClass is extending two classes TestClass and Object.in this
case MyClass is extending only one class that is TestClass.so somewhere you have create this class

class TestClass
{
}

in this case by default TestClass is extending Object class.

so if you are extending the TestClass from MyClass.Object class fetaurs will come automatically.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eshwin Sukhdeve wrote:see if you are using inheritence in your program like this

class MyClass extends TestClass
{
}

then why are thinking that MyClass is extending two classes


He was thinking that since MyClass extends TestClass, and TestClass extends object, and MyClass IS-A TestClass, that meant that MyClass was extending TestClass and Object. However, a few of us have explained that this is not the case.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!