Java Reference
In-Depth Information
because inside BClass , the methods of AClass are unknown. In order to access the
non-static methods of AClass , BClass has to have an instance of AClass .Inthe
listing below we change the constructor of BClass to have an instance of AClass
as an argument. Then that can be used to call the methods of AClass .In CClass
an instance of AClass is created and then given to BClass .
1.
class AClass{
2.
3.
public AClass(){
4.
}
5.
6.
public int methodA1(){ ){
7.
// commands
8.
}
9.
10.
}
11.
12.
-----------------------------------
13.
14.
class BClass{
15.
16.
private AClass aClass;
17.
18.
public BClass(AClass ac){
19.
aClass = ac;
20.
// now an AClass is known
21.
}
22.
23.
public void methodB1(){
24.
...
25.
int n=aClass.methodA1();
26.
// methodA1() known as a
27.
// method of aClass
28.
}
29.
}
30.
31.
-----------------------------------
32.
33.
class CClass{
34.
35.
public CClass{
}
36.
37.
38.
public static void main(String[] a){
AClass ac = new AClass();
39.
40.
// ac is instance of AClass
41.
Search WWH ::




Custom Search