Calling a Class


I have 3 Classes, Class A, Class B and Class C.

Class A has makes calls to 2 methods func1() which is found in Class B and func2() found in Class C.

Class A extends Class B, so now func1() is accessable.

but How does Class A Call func2() which is in class c without, as java does not support multiple inheritance.

also i don't want multi-level inheritance, where in Class B extends Class C, that's not advisable.

how do i solve the problem?
astonMartonAsked:
Who is Participating?
 
valipotorConnect With a Mentor Commented:
use a inner class that extends C
in the inner class you can calll any methods from B and C
then create a wrapper method for the methods of the inner class

public class A extends B
{
   class innerA exends C
   {
      public void execute()
     {
        func1();
        func2();
     }
   }
   public void execute()
   {
    innerA a=new innerA();
    innerA.execute();
   }
}

valipotor
0
 
armoghanCommented:
instantiate the class c in Class A and call the method
0
 
armoghanCommented:
Implement interfaces
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
thomas908Commented:
class A extends B {
   new C().func2();
}

class B {
   func1() {}
}

class C {
   func2() {}
}
0
 
expertmbCommented:
have an object of Class c in your Class A.
0
 
thomas908Commented:
or
class A extends B {
  C c = new C();
   c.func2();
}
0
 
Giant2Commented:
abstract Class D
func2 //here the body of the method

Class B extends D
func1 //here the body of the method

Class C extends D

Class A extends B
//here you can use both func1 and func2

Hope this help you.
Bye, Giant.
0
 
expertmbCommented:
>>where in Class B extends Class C, that's not advisable
0
 
objectsCommented:
> How does Class A Call func2() which is in class c without, as java does not support multiple inheritance.

It can't, without having an instance of class C.
0
 
Giant2Commented:
expertmb,

I told:
Class B extends D
and not: Class B extends Class C
0
 
expertmbCommented:
giant2,

>>and not: Class B extends Class C
i guess he dont want to extend B from anyother class.
0
 
Giant2Commented:
expertmb,

>>>and not: Class B extends Class C
>i guess he dont want to extend B from anyother class.

following what astonMarton told in the request, the only restriction is not on anyother, but only on C.
So, astonMarton must say what is the correct interpretation (I believe).

;-)
0
 
RuadRauFlessaCommented:
If your solution prohibits it try looking at static methods. It allows you to call the method without having an instance of the class. However you will only be able to read and or write static variables within the class which the static method resides.

public class A {
  public void M1 () {
    //now call the methods directly through the class name rather than an instance of B or C
    //however if you have an instance of B or C you can also call it from the instance.
    B.M2 ();
    C.M3 ();
  }
}

public class B {
  private int count; // you cannot address this in M2
  private static int count2; //you can address this in M2

  public static void M2 () {
    //do whatever method needs to do
  }
}

public class C {
  private int count; // you cannot address this in M3
  private static int count2; //you can address this in M3

  public static void M3 () {
    //do whatever method needs to do
  }
}

Is there a specific reason why you need to extend from Class B???
0
 
valipotorCommented:
my mistake ;)

  public void execute()
   {
    innerA a=new innerA();
    a.execute();
   }
0
 
Giant2Commented:
???
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.