This question already has answers here:
Can I get the instance of the calling object in Java?
(3 answers)
Closed 9 months ago.
Following example
public class C{
A myA;
public C(){
myA = new A();
}
}
public class A{
C myOrigin;
public A(){
// How to set myOrigin to the instance
// which invoke this.
}
}
Is there any way, to get the instance of class C, which creates the instance of A (inside this instance). With other words: Does an instance of A know the object from where it was initialised.
I know, I can use this as an parameter,
public class C{
A myA;
public C(){
myA = new A(this);
}
}
public class A{
C myOrigin;
public A(Object pObject){
myOrigin = pObject;
}
}
but I'm searching for a way without any parameter.
I very much doubt it's possible, but would be interested to hear otherwise. The reason I doubt it is because it's not an instance that calls a method, but another method. The calling method can be found easily through the stack trace (e.g. throw & catch an exception and examine its stack trace to find the caller). However, the instance is just a hidden argument to the method, and we have no way of knowing what arguments the calling method was called with. (At least, not to my knowledge.)
So, I think passing this to the other method is the only way.
Related
This question already has answers here:
“overriding” private methods with upcasting call in java
(1 answer)
Overriding private methods in Java
(10 answers)
Closed 5 years ago.
Can anybody explain why output to below question is "A.test" ?
class A {
private void test(){
System.out.println("A.test");
}
public void mytest(){
this.test();
}
}
class B extends A{
protected void test(){
System.out.println("B.test");
}
}
public class Test{
public static void main(String[] args) {
A a = new B();
a.mytest();
}
}
The test() method of class A cannot be overridden by class B, since it is private. Therefore this.test(); invokes A's test() method even though it is executed on an instance of class B.
Super class can invoke the methods of the sub class without using typecasting, without using reflection and without using a reference other than this only if they are overridden.
In your case A a = new B(); , object of B is created which has both the behaviours private void test() as inherited by super class A as well as protected void test() as added by B. The reason for both the methods being there and not just one is that due to the acess modifier being private the method is not visible in subclass scope. Hence it can not overriden and adding the method with same name just simply adds another method.
As there is no run time polymorphism in this case hence compile time target method is resolved and method defined in A is invoked.
If in case you would have overriden the mytest in B and from the overriden mytest you would have made a call to test then method in B would have been invoked. This is easy to understand as any method of B can not see any private method of its super class.
This question already has answers here:
Is it possible to set to null an instance of a class within the class
(5 answers)
Closed 6 years ago.
can we make a instance reference null in the class itself?
like bellow:
public class ClassA{
public void clear(){
//make this class be null
}
}
ClassA a = new ClassA();
a.clear();
//now a == null
can we implement this methord in C# or Java
Short answer: No.
Longer answer:
Instances cannot be null at all. Variables that are typed as references to the class can be set to null, but not instances. There can be many variables referring to the same instance. The instance has no way of knowing about those variables, so there's no way for it to reach out and set them to null.
Instead, you could have a method that returns null with the return type of the class:
public ClassA clear() {
return null;
}
...which the caller could use
foo = foo.clear();
But it doesn't really serve any purpose, the caller could just as easily do
foo = null;
Side note:
In .Net (since you mention C#), you might look at the IDisposable interface.
In Java, you might look at AutoCloseable.
public class ClassA
{
private ClassA obj = new ClassA();
public void clear(){
obj = null;
}
}
Is this what you are expecting, References of an object can be set to null.
This question already has answers here:
Private methods in Inheritance
(9 answers)
Closed 7 years ago.
A friend of mine asked this question to me. Why the following code does not give error on invoking aa.x()?
I understand that aa is a reference to object of class B but is invoking private method of class A inside the method of class A where it is visible and hence accessible.
Is my understanding correct? Or is there any other reason behind this?
public class A {
public void xyz() {
System.out.println("A");
}
private void x() {
System.out.println("A:x");
}
public static void main(String[] args) {
B b = new B();
A aa = b;
aa.x();
aa.xyz();
B bb = (B) aa;
bb.xyz();
bb.xyz12();
}
}
class B extends A {
public void xyz() {
System.out.println("B");
}
public void xyz12() {
System.out.println("B-12");
}
}
I can't immediately find a duplicate using a subclass, but fundamentally it's the same answer as the answer to this question.
There are two things that govern access to x:
Where the code is that's doing the access. Since x is private to A, the code accessing it must be part of a method in A. It can't be in a subclass (B) or an unrelated class.
What kind of reference you're using. If you have an A reference, you can access x on it. If you have a B reference, you can't, even though your code is part of an A method. You could cast it to A and then access x, but you can't do it directly with a reference of type B.
It is only visible because the main method where it is invoked is contained in class A. Move it to class B and it will not work
As the private methods are not inherited, a superclass reference calls its own private method.
Your main method is the method of A, therefore it can can call x() private method.
private modifier—the field is accessible only within its own class.
This question already has answers here:
Calling super super class method
(12 answers)
Closed 8 years ago.
Let say I have three classes:
class A
{
public void method()
{ /* Code specific to A */ }
}
class B extends A
{
#Override
public void method()
{
/*Code specific to B*/
super.method();
}
}
class C extends B
{
#Override
public void method()
{ /* I want to use the code specific to A without using B */ }
}
The goal in this case is to use the code from A without using the code from B. I thought there was a way to do it by calling the super method, but this brings in the code from B as well.
Is there a way to do this?
The short answer is no. What you're seeing is that your design is flawed. It indicates that you need too move the code in class A out into a helper class. B and C would then use it via composition. You could try using partials to get the behavior you want. See this link for more details. Partial function application in Java 8
You could instantiate an object of class A in C and call the method method
class C extends B
{
#Override
public void method()
{ /* I want to use the code specific to A without using B */
A test = new A();
test.method();
}
}
Not sure if this is what you meant. Also asumed you forget the method name method in class A.
No there is no way to do it. You can simply create another method in class B that executes the code of A's method, and call that method in subclass C.
This question already has answers here:
Why is super.super.method(); not allowed in Java?
(22 answers)
Closed 9 years ago.
In the example below, how can I access, from C, the method method() of the class A?
class A {
public void method() { }
}
class B extends A{
public void method() { }
}
class C extends B{
public void method() { }
void test() {
method(); // C.method()
super.method(); // B.method()
C.super.method(); // B.method()
B.super.method(); // ERROR <- What I want to know
}
}
The error I am getting is
No enclosing instance of the type B is
accessible in scope
Answer: No, this is not possible. Java doesn't allow it. Similar question.
You can't - and very deliberately. It would violate encapsulation. You'd be skipping whatever B.method wants to do - possibly validating arguments (assuming there were any), enforcing invariants etc.
How could you expect B to keep a consistent view of its world if any derived class can just skip whatever behaviour it's defined?
If the behaviour B provides isn't appropriate for C, it shouldn't extend it. Don't try to abuse inheritance like this.
Following code could be a work-around (not nice, but should work):
class A {
public void method() { }
}
class B extends A {
public void method() { }
protected void superMethod() {
super.method();
}
}
class C extends B {
public void method() { }
void test() {
method(); // C.method()
super.method(); // B.method()
superMethod(); // A.method()
}
}
Well, there is no direct way of doing this but you can always try workarounds.
I am not sure of the purpose of accessing method in class A from class C but you can always get hold of that method.
You could either create an instance of class A in class C and if that looks too simple, try using reflection API...
[link text][1]
Extreme Java
You shouldn't.
If you want to access the methods in A, extend from A instead of B.
When B extends A, it assumes that the underlying A-object won't be manipulated in other ways than how it does it itself. Therefore, by directly accessing the methods of A, you could be breaking how B functions.
Imagine, for instance, you have a class that implements a list, MyList. Now, imagine we extend this list with another class called MyCountingList, which overrides the add() and remove() methods to count the elements being added/removed. If you bypass the add() method MyCountingList provides, using the one MyList has instead, you've now broken the counting feature of MyCountingList.
So, in short, just don't.