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.
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(); } }
Above answer was rated as good by the following members: pravinsharma
RE: How can we inherit the two independent classes in ...
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(); } }
RE: How can we inherit the two independent classes in ...
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.
RE: How can we inherit the two independent classes in ...
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. }
RE: How can we inherit the two independent classes in ...
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.
RE: How can we inherit the two independent classes in ...
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).
RE: How can we inherit the two independent classes in ...
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.
RE: How can we inherit the two independent classes in ...
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.