You cannot use what you declare in B inside A.
Only the other way around since it is B that extends A, so B can access what you declare in A
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
make the methods and fields public in the master class?
jbennet
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
make the methods and fields public in the master class?
making the superclass containing all the possible variables and methods of the classes that inherit it?
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
public class A {
public void someMethod_A1() {
}
public void someMethod_A2() {
}
}
public class B extends A {
public void newMethod_B1() {
//BOTH are the same:
someMethod_A1(); //calls the method from super class
super.someMethod_A1(); //exactly the same as the above
}
// BUT if you also declare this:
public void someMethod_A2() {
}
//AND
public void newMethod_B2() {
this.someMethod_A2(); //calls the method from the class B (this class)
super.someMethod_A2(); //calls the method from the class A (super class)
}
}
I didn't quite understand what you are asking in your final post so I posted this example.
Let us know if you have any comments on it.
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
I get that kind of calling. What i want to do is the other way round. I want to access the method in the subclass from the superclass. Is there any way this is possible?
if I understand this correctly, you have (for instance) a class A (superclass) and a class B (subclass) which extends A, and you want B to be able to use a method in A?
just call it, you inherit the data and methods from A, so they're accessible for B
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
You mean this? :
public class A {
public void method_A1 {
//calling method from B
newMethod_B1();
}
}
public class B extends A {
public void newMethod_B1() {
}
}
No it cannot be done. What I wrote above is wrong. Although however you can do this, but has nothing to do with inheritance:
public class A {
public void method_A1 {
B b1 = new B();
b1.newMethod_B1();
}
}
You can create and call any class from anywhere, but like I said it has nothing to do with one class extending the other
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
you propably could code a way to do that, but what would be the use of that?
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433