Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Multiple Inheritance In Java

Reply
Thread Tools

Multiple Inheritance In Java

 
 
nirmal
Guest
Posts: n/a
 
      06-19-2005
hi everyone
this is my first post since i joined the group a week ago.
I m working with java for a year now since i come from a C++
background i can't help comparing the two languages.
While studying for java i found many books stating that java does not
support multiple inheritance i don't fully agree with this notion
(please don't hate me for that).
Though it is true that one cannot extend more than one class at a time
which makes perfect sense
since all the classes in java are derived from Object class and
multiple extensions would
cause multiple objects of Object to exist in the code which could be
undesirable.However java could
have provided a concept similar to virtual base classes in C++ but it
would have to be implicit like the extension to
the Object class and moreover multiple calls to the virtual base
class's(Object) constructor would have to be avoided.
(in C++ the compiler donot call the constructor of a virtual base class
from immediate derivations
rather it allows the class which is extending multiple derivations of
the virtual base class to call its
constructor directly, something which is not legal for ordinary base
classes) .Java could not provide a solution like this
since the only way to call the superclass constructor is by keyword
super which can call the constructor of
the immediate superclass only.So it is true that allowing multiple
extensions would have caused more problems
than it could solve.Still while creating patterns multiple inheritance
is sometimes inevitable.The solution to this
problem is interface.An interface in java is used to create a model of
a class,it just declare the behaviour(methods) that
a class must implement.It is worth noting that code reusabilty ,a
criteria generally used to define inheritance,while implementing an
interface is nil.That is why implementing an interface is not qualified
as inheritance.However if you have ever indulged yourself in creating
design patterns you would know that inheritance is more than just code
reusability rather in my opinion code reusability is just a consequence
of implementing inheritance in programming languages. Java allows a
class to implement more than one interface in this way a class can
implement 'behaviours' of multiple classes and thus to an extent
solving the multiple inheritance problem(without providing code
reusability).
So the question is that is it alright to say that java does not
support multiple inheritance?

nirmal.

 
Reply With Quote
 
 
 
 
Edwin Martin
Guest
Posts: n/a
 
      06-19-2005
nirmal wrote:

> Java allows a
> class to implement more than one interface in this way a class can
> implement 'behaviours' of multiple classes and thus to an extent
> solving the multiple inheritance problem(without providing code
> reusability).
> So the question is that is it alright to say that java does not
> support multiple inheritance?


Yes. With interfaces, you do not *inherit* functionality.

Edwin Martin

--
http://www.bitstorm.org/edwin/en/
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      06-19-2005
nirmal wrote:

> this is my first post since i joined the group a week ago.


BTW, I would have found your post a lot easier to read if you'd added spaces
after the punctuation in the usual way.



> Though it is true that one cannot extend more than one class at a time
> which makes perfect sense
> since all the classes in java are derived from Object class and
> multiple extensions would
> cause multiple objects of Object to exist in the code which could be
> undesirable.


There are other approaches to multiple inheritance (MI) that don't use the C++
mechanism. In C++ terms, every base class could be 'virtual', for instance. I
suspect (or rather, hope) that if Java had been defined to have MI, then the
designers would not have duplicated C++'s overcomplicated and fragile semantics
in this respect.


> So it is true that allowing multiple
> extensions would have caused more problems
> than it could solve.


This is probably true, but the difficulties are more likely (in my guess as to
how the Java designers were thinking) to be over-complication of the language
for /users/ of the language, rather than for /implementors/ of it. MI is not
widely regarded as a Good Thing (though there are dissenting opinions -- my
own, for instance .


> So the question is that is it alright to say that java does not
> support multiple inheritance?


It depends on what /sort/ of inheritance you are talking about. Java allows
inheritance of multiple /types/ (though the interface mechanism, as you noted),
but does not allow multiple inheritance of /implementation/. Java takes a
moderately confused approach to types, in that both interfaces /and/ class
names can be used as type names, yet classes are (or perhaps should be) more
about /implementation/ than about type. If you consider a hypothetical
Java-like language that only allowed interface names (and primitives) to be
used as type names, then classes would be used only for implementing behaviour
(not for specifying the externally visible API -- the type) then it would be
clearer how that language had MI for types, and SI for implementations. Still,
for most purposes, if MI is discussed, the context is more likely to be about
implementation inheritance, so -- without further clarification -- it would be
normal to say that Java does not have MI. (But if you say it on this newsgroup
then /someone/ is bound to start going on about interfaces, so it is probably
easier in the long run to be clear about which kind of inheritance you are
talking about if you make that kind of statement

-- chris


PS. My own opinion is that MI is fine for type, and it is also fine for
implementation -- the problems start when you try to mix both kinds of MI at
once, as in C++.



 
Reply With Quote
 
Leon
Guest
Posts: n/a
 
      06-19-2005

"nirmal" <> wrote in message
news: oups.com...

> So the question is that is it alright to say that java does not
> support multiple inheritance?


It is. Inheritance differs from implementation

In the case of multiple inheritance, superclass1::method() differs from
superclass2::method().

In the case of multiple implementation of the same method, interface1.method()
does not differ from interface2.method(). Both are the same method.

Greetings, Leon.


 
Reply With Quote
 
Wibble
Guest
Posts: n/a
 
      06-20-2005
nirmal wrote:
> hi everyone
> this is my first post since i joined the group a week ago.
> I m working with java for a year now since i come from a C++
> background i can't help comparing the two languages.
> While studying for java i found many books stating that java does not
> support multiple inheritance i don't fully agree with this notion
> (please don't hate me for that).
> Though it is true that one cannot extend more than one class at a time
> which makes perfect sense
> since all the classes in java are derived from Object class and
> multiple extensions would
> cause multiple objects of Object to exist in the code which could be
> undesirable.However java could
> have provided a concept similar to virtual base classes in C++ but it
> would have to be implicit like the extension to
> the Object class and moreover multiple calls to the virtual base
> class's(Object) constructor would have to be avoided.
> (in C++ the compiler donot call the constructor of a virtual base class
> from immediate derivations
> rather it allows the class which is extending multiple derivations of
> the virtual base class to call its
> constructor directly, something which is not legal for ordinary base
> classes) .Java could not provide a solution like this
> since the only way to call the superclass constructor is by keyword
> super which can call the constructor of
> the immediate superclass only.So it is true that allowing multiple
> extensions would have caused more problems
> than it could solve.Still while creating patterns multiple inheritance
> is sometimes inevitable.The solution to this
> problem is interface.An interface in java is used to create a model of
> a class,it just declare the behaviour(methods) that
> a class must implement.It is worth noting that code reusabilty ,a
> criteria generally used to define inheritance,while implementing an
> interface is nil.That is why implementing an interface is not qualified
> as inheritance.However if you have ever indulged yourself in creating
> design patterns you would know that inheritance is more than just code
> reusability rather in my opinion code reusability is just a consequence
> of implementing inheritance in programming languages. Java allows a
> class to implement more than one interface in this way a class can
> implement 'behaviours' of multiple classes and thus to an extent
> solving the multiple inheritance problem(without providing code
> reusability).
> So the question is that is it alright to say that java does not
> support multiple inheritance?
>
> nirmal.
>

Hey, if you define a duck to be a pig, then pigs can fly.
 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      06-20-2005
> So the question is that is it alright to say that java does not
> support multiple inheritance?
>


Yes.


 
Reply With Quote
 
Lucy
Guest
Posts: n/a
 
      06-20-2005

"Hal Rosser" <> wrote in message
news:GWrte.115470$ t...
> > So the question is that is it alright to say that java does not
> > support multiple inheritance?
> >

>
> Yes.
>
>

al·right
al·right (ôl-rìt¹) adverb
Non-Standard.
All right: "Alright, it might be fun to hunt tigers" (Carl Icahn). See Usage
Note at all right.



 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      06-20-2005

"Lucy" <> wrote in message
news:OeqdnfLnINeddSvfRVn-...
> All right: "Alright, it might be fun to hunt tigers" (Carl Icahn). See

Usage
> Note at all right.
>


might be fun to hunt tigers - till you find one. He may have the fun. and
his next meal is you.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
multiple inheritance and java generics flaw? Murat Tasan Java 1 06-22-2005 07:34 PM
MULTIPLE INHERITANCE IN JAVA? nirmal Java 5 06-20-2005 11:12 AM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM
Multiple inheritance equivanlent in Java/CSharp Hung Jung Lu Java 5 11-20-2003 12:06 AM



Advertisments