wood burning stoves 2.0*
The moose likes Java in General and the fly likes Object class by default break to rule of multipul inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Object class by default break to rule of multipul inheritance" Watch "Object class by default break to rule of multipul inheritance" New topic
Author

Object class by default break to rule of multipul inheritance

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

As in java we can't extends multipul classed to save from multipul inheritance ... As from my knowledge Object class is the super class for every class in java and it extends in implicitly then don't you think that it breaks the contract for multipul inheritance when we extend other class ???...

please clear my doubt...

Kaustubh


No Kaustubh No Fun, Know Kaustubh Know Fun..
subhash kumar
Ranch Hand

Joined: Jul 14, 2010
Posts: 63
I think no.
multiple inheritance says
if there are A,B,C classes, then C extending both A and B is called the multiple inheritance
but if C extending B and B extending A is not the multiple inheritance but is multilevel inheritance.
Same is the case of Object Class .


Subhash Kumar
Attitude is everything
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Kaustubh G Sharma wrote:As in java we can't extends multipul classed to save from multipul inheritance ... As from my knowledge Object class is the super class for every class in java and it extends in implicitly then don't you think that it breaks the contract for multipul inheritance when we extend other class ???...

please clear my doubt...

Kaustubh


Otherway round isn't true. In java, class can only extends another class directly, But that class can be extended by multiple classes.

For a real world example, A son can have only one Father, but a Father can have multiple Sons!

Is it clear?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Abimaran Kugathasan wrote:For a real world example, A son can have only one Father, but a Father can have multiple Sons!

Interesting comparison, but you should be aware that the meaning of the word "inheritance" in object oriented programming is very different from the biological meaning of the word. Pretending that they are analogous is often very confusing.

In object oriented programming, when class B is a subclass of class A, it means that B is an A (the Liskov substitution principle). To be more precise, it means that B is a specialized kind of A.

For example, you could have a class Animal with a subclass Mammal. That fits with the object oriented meaning of inheritance: a Mammal is a certain kind of Animal. Some people name classes "Parent" and "Child" (or "Father" and "Son") - that's wrong and confuses the biological meaning of "inheritance" with the object oriented meaning, because a Child is not a special kind of Parent.

Your analogy doesn't really hold for multiple inheritance, because there are programming languages (C++, for example) which do allow multiple inheritance - does that mean that in C++ a Son can have multiple Fathers?

Kaustubh G Sharma: Multiple inheritance means that a class has multiple superclasses at the same level - not that it has multiple superclasses at different levels of the class hierarchy. Java doesn't have multiple inheritance, because you almost never need it in practice and because it causes difficult problems such as the diamond problem. (In C++ this was solved by adding an extra feature to the language: virtual inheritance, making the language more complex than it already was...).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Hi Kaustubh,

Let us solve this by example.

You have a class A which extends nothing but Object.

Now you want A to extend B. Ok ... But then B has to extend the Object class implicitly.

You decide to put a class C above B , but then C will have to extend Object.

So at any point of time in your class hierarchy you will always have Object at the top level.

You can extend One and Only one class & through this class you can inherit its parent behavior as applicable by visibilty
so on so forth till you reach the Object class.

Also when we say java does not support "mutiple inheritance" it means a class can have
only one direct parent class which it can inherit.

Hope this clarifies the doubt.


Regards,
Avishkar Nikale
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Consider the code below. The classes Animal, Date and MyIdea have nothing in common except
that we want to use them in a java program. So they all share methods from parent class Object:
wait() notify(), hashCode(), equals(), getClass(), finalize() and so on. Animal, Date and MyIdea
each use single inheritance. Jim ... ...


BEE MBA PMP SCJP-6
sharma pankaj
Greenhorn

Joined: Jul 21, 2010
Posts: 6
Jesper Young wrote:
Abimaran Kugathasan wrote:For a real world example, A son can have only one Father, but a Father can have multiple Sons!

Interesting comparison, but you should be aware that the meaning of the word "inheritance" in object oriented programming is very different from the biological meaning of the word. Pretending that they are analogous is often very confusing.

In object oriented programming, when class B is a subclass of class A, it means that B is an A (the Liskov substitution principle). To be more precise, it means that B is a specialized kind of A.

For example, you could have a class Animal with a subclass Mammal. That fits with the object oriented meaning of inheritance: a Mammal is a certain kind of Animal. Some people name classes "Parent" and "Child" (or "Father" and "Son") - that's wrong and confuses the biological meaning of "inheritance" with the object oriented meaning, because a Child is not a special kind of Parent.

Your analogy doesn't really hold for multiple inheritance, because there are programming languages (C++, for example) which do allow multiple inheritance - does that mean that in C++ a Son can have multiple Fathers?

Kaustubh G Sharma: Multiple inheritance means that a class has multiple superclasses at the same level - not that it has multiple superclasses at different levels of the class hierarchy. Java doesn't have multiple inheritance, because you almost never need it in practice and because it causes difficult problems such as the diamond problem. (In C++ this was solved by adding an extra feature to the language: virtual inheritance, making the language more complex than it already was...).


Superb......Clean and Clear........ thanks man! you solve my problem
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

So at any point of time in your class hierarchy you will always have Object at the top level.


Got it... but As class A implicitly extends Object (Father of all the classes) whether it is not use extend keyword for it and again it extends another class say B...In this case it is inheriting more than one class one is explicitly while one implicitly ....This thing confuse me lot.....

kaustubh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Suppose you have:

class A
class B extends A

Then class A implicitly extends Object. Note that class B does not extend class Object directly, and it doesn't need to; class A, which B extends, already extends Object, so there is no need for class B to extend Object again. This all doesn't have anything to do with multiple inheritance. There's no "hidden" multiple inheritance anywhere.

I suspect that you're making it more complicated than it really is...
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

1)
Class A implicitly extends Object
{-....-}

2)
Class A extends B
{-...-}

Fianlly what we have

Class A implicitly extends Object + explicitly extends Class B....

Is what I am asking please correct me If I am wrong

Thanks...
Kaustubh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

As I said, you're imagining problems that aren't there.

A class can have only one direct superclass. If it isn't specified, then the direct superclass of the class is Object. Otherwise, the direct superclass is what's specified with "extends".

There is no "hidden" multiple inheritance. If class A extends B, then class A does not directly extend Object. There is no "class A implicitly extends Object + explicitly extends Class B".
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Thank you very much Jesper your last post really help me to understand the fact.... Appritiate you for it...

Thanks da...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Object class by default break to rule of multipul inheritance