File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes multiplt inheritence in java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "multiplt inheritence in java" Watch "multiplt inheritence in java" New topic
Author

multiplt inheritence in java

j srinivas
Greenhorn

Joined: Apr 08, 2008
Posts: 16
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
"Favour composition over inheritance."

That's one possible way to do it.
Nisha Puri
Greenhorn

Joined: Nov 30, 2007
Posts: 8
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 ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9950
    
    6

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.


Never ascribe to malice that which can be adequately explained by stupidity.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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)


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
j srinivas
Greenhorn

Joined: Apr 08, 2008
Posts: 16
thanks all....


but is it really impossible...
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4166
    
    3

Why don't you try it and see what the compiler tells you?


luck, db
There are no new questions, but there may be new answers.
joseph prabhu
Ranch Hand

Joined: Feb 26, 2008
Posts: 162
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
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
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.
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
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 ]
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3189
Java only supports multiple interface inheritance and does not support multiple inheritance. Favor composition as recommended in the Gang Of Four design patterns.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4166
    
    3

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiplt inheritence in java
 
Similar Threads
Database Designs
local method inner class
Unable to add an element to an arraylist using extended class
Overloading and type conversion
OO