Solved

Calling a Class

Posted on 2004-09-01
15
162 Views
Last Modified: 2013-11-23

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?
0
Comment
Question by:astonMarton
  • 4
  • 3
  • 2
  • +4
15 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 11960690
instantiate the class c in Class A and call the method
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11960696
Implement interfaces
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11960704
class A extends B {
   new C().func2();
}

class B {
   func1() {}
}

class C {
   func2() {}
}
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11960709
have an object of Class c in your Class A.
0
 
LVL 8

Expert Comment

by:thomas908
ID: 11960710
or
class A extends B {
  C c = new C();
   c.func2();
}
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11960752
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
 
LVL 6

Expert Comment

by:expertmb
ID: 11960758
>>where in Class B extends Class C, that's not advisable
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 11960809
> 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
 
LVL 12

Expert Comment

by:Giant2
ID: 11960860
expertmb,

I told:
Class B extends D
and not: Class B extends Class C
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11960881
giant2,

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

Expert Comment

by:Giant2
ID: 11960894
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
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 11961034
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
 
LVL 6

Accepted Solution

by:
valipotor earned 125 total points
ID: 11961382
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
 
LVL 6

Expert Comment

by:valipotor
ID: 11961425
my mistake ;)

  public void execute()
   {
    innerA a=new innerA();
    a.execute();
   }
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12166828
???
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 71
eclipse java  build path 6 43
how do i compare an object based on two fields 6 48
Cipher Configuration on Apache HTTPD 4 50
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now