If there are two classes in a java program and we have another java program how can we use the function of 2nd class of first java program to 2nd java program e.g
One java program
Public class A
{
Public class B
{
void a();
void b();
}
}
Second java program
Public class C
{
i want to call void a() and void b() here
}
You can do it by inheritance.
public class C extends B {
public static void main(String args[]){
C foo = new C();
foo.a();
foo.b();
}
}
In C, you would need to create an instance of B. Let's call this instance bTest. You could then call these methods like this:
B bTest = new B();
bTest.a();
bTest.b();
Now, if you made a() and b() static methods, you would call them using the name of the class they are in rather than using an instance of it, as such:
B.a();
B.b();
Keep in mind that you will have to import B if it is not in the same package as C.
Since your inner class is not static one you have to create an object of A first and then create object of B.
A a = new A();
A.B b = a.new B();
b.a();
b.b();
Related
If class A creates an instance of class B, then in class B can I run a method from class A? sorry if the question is poorly worded, I don't know how else I could say it.
In the below code, Class A creates an instance of Class B and you can call Class A's method from Class B's method.
class A {
public void getA() {
System.out.println("In A");
}
public static void main(String[] args) {
B b = new B();
b.getB();
}
}
class B {
public void getB() {
System.out.println("In B");
A a = new A();
a.getA();
}
}
Output:
In B
In A
In class B you can call methods of class A only if A's methods are visible to B. It doesn't matter who created an instance of B.
This tutorial might help: http://www.tutorialspoint.com/java/java_access_modifiers.htm
So you can call it if the method is one of the following:
public
protected (and B is a subclass of A or B is in the same package as A)
no modifier (and B is in the same package as A)
In this program
class a
{
int a=25;
public void aa()
{
System.out.println(a);
}
}
class b extends a
{
int a=2;
public static void main(String[] args) {
b x=new b();
x.aa();
}
}
why does "x.aa()" prints 25 ans why not 2?,what is the reason behind it?
class a
{
int a=25;
public void aa()
{
System.out.println(a);
b();
}
public void b()
{
System.out.println("this should print");
}
}
class b extends a
{
int a=2;
public static void main(String[] args) {
b x=new b();
x.aa();
}
public void b()
{
System.out.println("this should not print");
}
}
and if we consider the above output,then here again the output of b() of above should print "this should print" but we are getting the ouput from sublcass "this should not print"
Class b inherits class A, so when you call x.aa, it is calling method aa of class a. The member a of class a is initialized with 25 so it prints 25. Class a does not know about the member a of class b.
Fields cannot be overridden, and are not virtual. B.a* is independent of A.a, to the point that they can actually have different types. There is no way for B to make A aware of B.a in place of A.a. Instances of B will actually have two fields named a, but one is hidden ("shadowed") by the other. (If desired, code B can refer to its A.a field by writing ((A)this).a. This is because the appropriate a is selected based on the type of ((A)this), which is A, rather than the runtime type of the instance, which would be B or a subtype of B.)
* Note: I have renamed your classes to A and B: per the Java naming conventions, class-names begin with uppercase letters.
b x=new b();
while you will do this this will print out put of the constructor as its giving because its clearly making the object of b class and while in the case of class A in that class that b() act as the method so it wont call when you are making object of class b.
while in the case of
b x=new b();
x.aa();
this it will by default call the inheritance and will print as u getting output.
and one more thing.
public void aa()
{
System.out.println(a);
}
in the case of this it will use the local variable of that particular class so it will print whatever you have defined on that class. in your case that is a=25;
by the way nice question.
method aa() is member of class a. so, method aa() can see/access instance member of class a.
Now, you have inherited class a to b. it means object of class b can access/call aa(). but, it doesn't mean that aa() is allowed to access variable a of class b. thats why method aa() is printing the variable a of it's own class and prints 25.
now about your second program. in class b, you are overriding method b(). while calling an overridden method, the selection of method(whether from sub class or super class) is done on the basis of instance of a invoking object(whether sub class or super class respectively).
you have called method aa() with the instance of class b that is why method b() of class b is called.
at line A a=new B() i am getting error
abstract class A{
}
class B extends A{
A a=new B();
}
public class Test {
public static void main(String[] args) {
B ab=new B();
System.out.println(ab.a);
}
}
I don't know why am i getting that error
You are recursively creating instances of B.
class B extends A{
A a=new B(); // will get called infinitely. So, you get StackOverFlowError
}
A StackOverflowError means that you have too many function calls to fit their data on the stack. Usually it's an indication that you have infinite recursion going on, as you do in this case.
When you are creating the object B in class Test, the class B is creating another object B, over and over.
class B extends A{
A a=new B(); // problem here
}
The fact that you call B's constructor inside of B's declaration is a case of infinite recursion. There's no way for that to work.
You'd instead have to lazy-load the instance on request:
class B extends A {
private A a;
public A getA() {
if(a == null) {
a = new B();
}
return a;
}
}
Obviously this code isn't thread-safe, but it'll at least get you started.
class B extends A{
A a=new B();
}
At line A a=new B(); , again the default constructor of class B is invoked. This keeps happening, and the stack gets full due to frames of B's constructor calls.
In code
class B extends A{
A a=new B();
}
you decided that each instance of B needs to have its own field which will hold other instance of B.
So each time you call new B(), another instance of B needs to be created, but this another instance needs to also have its own another instance of B, and so on.
You can visualize it this way
new B()
|(requires)
+-- A a = new B();
|(requires)
+-- A a = new B();
|(requires)
+-- A a = new B();
|(requires)
+-- ... and so on until stack will overflow.
code as follows:
interface A
{
void test();
}
interface B
{
void test();
}
public class C implements A,B {
public static void main(String[] args) {
C c=new C();
c.test();
}
public void test() {
System.out.println("hai");
}
}
Here o/p is came, but which interface method was invoked?
All that the interfaces require is for you to have a void method called test(), which takes no arguments. The implementation of test() is in your class, not interface.
Additionally, you cannot instantiate an interface with new; however, you could do the following:
A a = new C();
or
B b = new C();
EDIT: It doesn't work if you take the public out of your method header, since interface methods must be public. Despite you not putting public in the interface when declaring the header, interface methods are by default public--this cannot be changed, and putting public in the header inside of the interface is unnecessary.
Your code looks invalid as you cannot instantiate an A.
You can use new C() but at present what you are doing is
A a=new A();
which is invalid.
So you can probably do like
A a = new C();
or
B b = new C();
but you cannot instantiate an interface
EDIT:-
After the edit which you have made in your question, it looks like that the implementation of test() is in your class, not interface.
You cant initiate a interface like you are doing
A a=new A();
You can do like
A a = new C();
then there will be no problem which interface method was invoked..
In Python, class methods can be inherited. e.g.
>>> class A:
... #classmethod
... def main(cls):
... return cls()
...
>>> class B(A): pass
...
>>> b=B.main()
>>> b
<__main__.B instance at 0x00A6FA58>
How would you do the equivalent in Java? I currently have:
public class A{
public void show(){
System.out.println("A");
}
public void run(){
show();
}
public static void main( String[] arg ) {
new A().run();
}
}
public class B extends A{
#Override
public void show(){
System.out.println("B");
}
}
I'd like to call B.main() and have it print "B", but clearly it will print "A" instead, since "new A()" is hardcoded.
How would you change "new A()" so that it's parameterized to use the class it's in when called, and not the hard-coded class A?
Static methods in java are not classmethods they are staticmethods. In general it is not possible to know which class reference the static method was called from.
Your class B does not have a main method and static methods are not inherited.
The only way I can see this happening is to find whatever is calling A.main( String[] arg ) and change it to call B.main instead.
B.main:
public static void main( String[] arg ) {
new B().run();
}
How is your program started? Is there a batch file, shortcut, etc? Something you can change? Where does A.main get called?
I think this isn't possible. Here's why:
In Java, the implementation of a method is determined by the instance's run-time type. So, to execute B.show(), you need to have an instance of B. The only way I could see to do this, if the method that constructs the instance is supposed to be inherited, is to use Class.newInstance() to construct an instance of a type that's not known at runtime.
The problem with that is that within a static method, you have no reference to the containing class, so you don't know whose newInstance method to call.
Why do you want to do this, though? There may be some better way to achieve whatever it is you want to achieve.
In your example I wouldn't put your main method inside of A. This is setup as the entry point into the system (you can't be in B if you are specifically entering into A).
In the example below I created class A, B, and C. Class C instantiates A and B and runs them. Notice that in C I created an A, a B, and another A that I instantiate as a B. My output is:
A
B
B
Hopefully this makes sense.
public class A {
public void show(){
System.out.println("A");
}
public void run(){
show();
}
}
public class B extends A {
#Override
public void show(){
System.out.println("B");
}
}
public class C {
public static void main(String[] args) {
A a = new A();
B b = new B();
A anothera = new B();
a.show();
b.show();
anothera.show();
}
}