How can we inherit the two independent classes in one class? for example, we have two independent classes like Class A and B. I want to inherit the methods of Class A and B.

Showing Answers 1 - 11 of 11 Answers


class A
{
 public void show(){System.out.println("A");}
}
class B
{
 public void show1(){System.out.println("B");}
}
class c
{
 A a=null;
 B b=null;
 c()
 {
  a=new A();
  b=new B();
 }
 public void show2()
 {
  a.show();
  b.show1();
 }
 
}
public class test
{
 public static void main(String args[])
 {
  c test=new c();
  test.show2();
 }
}

sunil

  • Feb 14th, 2006
 

extend class b from class a then extend the class c fom class b

Jignesh

  • Feb 15th, 2006
 

Good answer. Basically what he is looking for is multiple inheritance through classes which is not possible. Using interface one can achieve this sophestically as you can implement multiple interfaces but can extend only 1 class.

  Was this answer useful?  Yes

Multiple inheritance is not allowed in Java, so inheriting properties of two independent class is not possible as such in java. But linear inheritance is available in java, Thus you can inherit properties of two different classes. As shown in following program.//this program illustrates how to inherit properties of two classes. class A //independent class A not related to class B in any way{int a,b; //declaration of variables of Class A as per your requirements A()//Constructor of a independent class A { } int add() //methods of independent class A { return(a+b); } }class B extends A //make class B inherit class A{int c,d; //declaration of variables of Class B as per your requirements B() { } int sub() //methods of class B { return(d-c); }}class Demo extends B //This class thus inherits properties of both classes{ int e; //variables of Demo class which inherits both class A and B. Demo() //constructors of Demo class { a=1; //we have easily b=2; //inherited properties c=3; //of both the classes as variables a,and b come from class A d=4; //and c and d come from class B. e=5; } void show() //shows the properties { System.out.println("a= "+a); //Inherited System.out.println("b= "+b); //the properties System.out.println("c= "+c); //of Class A and Class B System.out.println("d= "+d); System.out.println("e= "+e); } public static void main(String args[]) { Demo obj=new Demo(); obj.show(); //calls to display properties of Demo System.out.println("Addition= "+obj.add()); //calling the methods System.out.println("subtraction= "+obj.sub()); //of inherited classes } //is also easy. }

  Was this answer useful?  Yes

I saw my prev post, in the program all spaces between lines were removed so it has become impossible to understand. Adding again,w/o comment to save space.class A {int a,b;int add(){return(a+b);}}class B extends A {int c,d;int sub(){return(d-c);}}class Demo extends B {int e;Demo() { a=1; b=2; c=3; d=4; e=5; }void show() {System.out.println("a="+a+" b="+b+" c="+c+" d="+d+" e="+e); } public static void main(String args[]) { Demo obj=new Demo(); obj.show(); System.out.println("Addition= "+obj.add()); System.out.println("subtraction= "+obj.sub()); } } //hope mohmad you get this.

  Was this answer useful?  Yes

Prasath S.N

  • Mar 1st, 2006
 

In the above program Class A and Class B are dependent(caz...b inherits A).But the question is how can we inherit TWO INDEPENDENT CLASS (Class A should not 've any relation with B Class).

Just think about this Sunil......!!

If i am wrong correct me.....!!

  Was this answer useful?  Yes

deepa

  • Mar 19th, 2006
 

There might be situation that two independent classes may contain the mathod with same name. If we inherit them in the same class, ambiguity error might occur. So we cant extend more than one class.

We can use interface instead of this.

  Was this answer useful?  Yes

cnu.sinu@gmail.com

  • May 10th, 2006
 

Ragavendra,

Your answer is so nice.It will help in many situations.

Again thanks for your answer.

  Was this answer useful?  Yes

gaurav

  • May 29th, 2006
 

but dear deepa,the question is we have two independent classes and not interfaces.We can't implement the classes.We have to extend the classes and that we can do only one at a time.

The sloution can be-

(assume class A,B exists)

class c extends A

{

  Was this answer useful?  Yes

gaurav

  • May 29th, 2006
 

dear deepa please read the question carefully.We have two classes and two interfaces.I we had two interfaces then surely we may have implemented it.

The solution may be like this

(assume class A and B exists)

class C extends A

{

     //this class now has all characteristics of class A

     class D extends B //inner class

     {

        //now this also has also has all properties of class B

      }

}

I this way we can use the properties of class A and B.Correct me i am wrong

  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions