• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Object class by default break to rule of multipul inheritance

 
Ranch Hand
Posts: 1283
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...).
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ... ...
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1283
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1283
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1283
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Jesper your last post really help me to understand the fact.... Appritiate you for it...

Thanks da...
 
You get good luck from rubbing the belly of a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic