GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 201 of 502    Print  
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.

  
Total Answers and Comments: 11 Last Update: May 29, 2006     Asked by: Mohammad 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: raghavendra.ls
 


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
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
February 14, 2006 03:41:03   #1  
raghavendra.ls Member Since: February 2006   Contribution: 3    

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();
}
}


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
February 14, 2006 04:14:09   #2  
sunil        

RE: How can we inherit the two independent classes in ...
extend class b from class a then extend the class c fom class b
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
February 14, 2006 05:59:48   #3  
veeravadivelan Member Since: February 2006   Contribution: 5    

RE: How can we inherit the two independent classes in ...
Multiple Inheritance is not possible in Java.Instead of Multiple inheritance we can implement the interface that contains the Abstact methods.
 
Is this answer useful? Yes | No
February 15, 2006 17:02:34   #4  
Jignesh        

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.
 
Is this answer useful? Yes | No
February 16, 2006 05:18:47   #5  
sunil_dalvi82 Member Since: February 2006   Contribution: 9    

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. }
 
Is this answer useful? Yes | No
February 16, 2006 23:09:43   #6  
sunil_dalvi82 Member Since: February 2006   Contribution: 9    

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.
 
Is this answer useful? Yes | No
March 01, 2006 09:50:54   #7  
Prasath S.N        

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).

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

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


 
Is this answer useful? Yes | No
March 19, 2006 23:09:02   #8  
deepa        

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.

We can use interface instead of this.


 
Is this answer useful? Yes | No
May 10, 2006 02:30:17   #9  
cnu.sinu@gmail.com        

RE: How can we inherit the two independent classes in ...

Ragavendra

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

Again thanks for your answer.


 
Is this answer useful? Yes | No
May 29, 2006 03:15:44   #10  
gaurav        

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.

The sloution can be-

(assume class A B exists)

class c extends A

{


 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape