• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

multiplt inheritence in java

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have "A" class in which i define two methods add()and substract().
I have another class "B" in which i declared another two methods multiply()and divide().

Now i have a class "C",in which i want to use all the four methods.

Whether it is possible or not.
 
Marshal
Posts: 73252
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Favour composition over inheritance."

That's one possible way to do it.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by j srinivas:
i have "A" class in which i define two methods add()and substract().
I have another class "B" in which i declared another two methods multiply()and divide().

Now i have a class "C",in which i want to use all the four methods.

Whether it is possible or not.



In java you can not inherit more than one class.
To achieve multiple inheritance, you should use interfaces(in which you can declare methods(definition is not allowed))
class A is fine. Change your class B to interface B, syntax:-
interface B { ....}
and then use following code:-

class C extends A implements B
{
...
...
}
[ May 20, 2008: Message edited by: Nisha Puri ]
 
lowercase baba
Posts: 12991
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In java you can not inherit more than one class.



Because this comes up occasionally, let me clarify a little. You cannot DIRECTLY extend from more than one class.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multiple Inheritance is not possible in Java.

What you are trying to do is possible (in a sense).


(typing off the cuff, any syntax errors are left as an exercise for the reader)
 
j srinivas
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all....


but is it really impossible...
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you try it and see what the compiler tells you?
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by j srinivas:
thanks all....


but is it really impossible...



joe here
class A
{ void add(int a,int b){}void subract(int a,int b){}
}
class B extends A
{ void multiply(int a,int b){}void divide(int a,int b){}
}
class c extends b
{ B obj=new B();
obj.add(); obj.subract(); obj.multiply(); obj.divide();

}
 
Campbell Ritchie
Marshal
Posts: 73252
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not called multiple inheritance. You only have each class declaring it extends one class. C extends B and B extends A is called single inheritance.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by j srinivas:
but is it really impossible...



Yes, it is impossible. That doesn't mean that the only solution is multiple inheritance... which is the point.
[ May 22, 2008: Message edited by: paul yule ]
 
Author
Posts: 3450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java only supports multiple interface inheritance and does not support multiple inheritance. Favor composition as recommended in the Gang Of Four design patterns.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by arulk pillai:
Java only supports multiple interface inheritance and does not support multiple inheritance. Favor composition as recommended in the Gang Of Four design patterns.



Java supports implementing multiple interfaces. Implementation and inheritance are not the same.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic