Access method of calling class - java

i want to access method of calling class when method in called class is triggered. Following is the example i want to do. The class that is making the instance of classes C is called randomly. so when the listerners method is triggered the calling class' interface method should be able to listen that tigger.Thats what i want to do
Interface myinterface
{
public void interfacemethod();
}
class A implements myinterface
{
C instance = new C ();
public void interfacemethod()
}
class B implements myinterface
{
C instance = new C ();
public void interfacemethod()
}
Class C extends someListerner
{
public void listenercalled()
{
//here i want to call the callers interfacemethod()
}
}

You need to pass a pointer to the caller object.
If you are trying to call C's creator's Myinterface object, pass a Myinterface pointer in the C constructor.
C instance = new C(this);
Store that pointer in your C class and use it for the callback. Java functions are "virtual" by default. That means, regardless of casting, the lowest subclass function will be called (which is what you want, I think).
Interface Myinterface{
public void interfacemethod();
}
class A implements Myinterface
{
C instance = new C ((Myinterface)this);
public void interfacemethod()
}
class B implements Myinterface
{
C instance = new C ((Myinterface)this);
public void interfacemethod()
}
Class C extends someListerner
{
Myinterface ml;
public C(Myinterface arg_ml){
ml = arg_ml;
}
public void listenercalled()
{
ml.interfacemethod();
//here i want to call the callers interfacemethod()
}
}

Related

Making abstract an overridden method

Here's something I quite understand:
abstract class A {
public void foo() {
System.out.println("a");
}
}
abstract class B extends A {
#Override
public abstract void foo();
public void bar() {
super.foo();
foo();
}
}
class C extends B {
#Override
public void foo() {
System.out.println("c");
}
}
public static void main(String[] args) {
new C().foo();
new C().bar();
}
new C().foo() prints c to the console, while new C().bar() prints a then c.
Calling super.foo() is illegal in the #foo() implementation of the C class.
I don't have a clear question, but if anyone could give a complete explanation of what is going on with the foo method, it may be interesting I think.
A is super class for B, so calling super.foo() inside B calls method defined in A, and calling foo() inside the same class will invoke its own implementation that should be delivered by any subclass.
You cannot use super.foo() within C class because it is defined as abstract in B and cannot be invoked directly.

Call base method on generic object of derived class

Trying to add a base interface with method so all derived classes have to implement the method or use default method. What's the best way to going about getting this method callable? See comment in code block below.
public interface IA{}
public interface IB{
public Integer doWork();
}
public interface IC extends IB{
}
class B implements IB{
Integer doWork(){
return 2;
}
}
class C extends B implements IC{
#Override
Integer doWork(){
return 7;
}
}
//What do I need to do to cast clazz to an object so I can call the derived class' doWork method?
private Integer newClient(Class<T> clazz){
((B) clazz).doWork();
}
Ended up finding a solution:
B.class.cast(clazz);
As for how to ensure you call the derived class' method that overrides the base, that is a native behavior of Java.
Example Program:
public class Foo {
static class A {
int get() { return 0; }
}
static class B extends A {
#Override
int get() { return 1; }
}
public static void main(final String[] args)
{
A a = new A();
B b1 = new B();
A b2 = new B();
printA(a);
printA(b1);
printA(b2);
}
public static <T extends A> void printA(T bObj) {
System.out.println(bObj.get());
}
}
Output:
0
1
1
Note that the output returned from b2::get()::int is the same as b1::get()::int, even though b2 is type A and b1 is type B. This is because even though we only have a reference to the A class in b2, the object implementation is still B.
It seems that you only want to know how to instantiate the Class. Assuming it has a default constructor you can do it this way:
private Integer newClient(Class<B> clazz){
try {
((B) (clazz.getConstructor().newInstance())).doWork();
} catch ...
}

override static method functionality

We have class library as
class A {
public void callWorkflow() {
B b = new B();
}
}
class B {
public void callStatic() {
C.someMethod();
}
}
class C {
public static someMethod() {}
}
We are actually trying to change functionality of static method someMethod. Is there a way to solve this problem without changing call hierarchy?
You can't just Override a static method. In my opinion, remove static from the method someMethod(), then create an object of class C inside class B. Then call the method.
Class A{
public void callWorkflow() {
B b = new B();}
}
Class B{
public void callStatic(){
C c = new C();
c.someMethod();}
}
Class C{
public someMethod(){}
}
There is no way to override a static method.
That's why one of these approaches is preferred to calling static methods:
Inject another object (service) that will provide the functionality in a non-static method and call it through the injected object
Make the static method a thin wrapper that just delegates the work to some non-static object that can be configured (like in slf4j's logger)

Java static methods overriding and interfaces

I have an A interface with a method getT(), abstract B class with method getS() and class C that extends B class which implements A interface. I'm overriding methods from A interface and B superclass inside the C subclass. Then in the main method I instantiate the C class to be typeo if A like this: A obj = new C(); I'am able to call the getT() from obj, but can't call getS() method. How can I call getS() method from obj, but I can't change the type of obj, it has to be A. Here is the code:
Interface A :
public interface A {
public String getT();
}
Abstract class B :
public abstract class B implements A {
public abstract String getS();
}
subclass C :
public class C extends B {
#Override
public String getT() {
System.out.println("method getT() from C class");
return null;
}
#Override
public String getS() {
return null;
}
}
And the main method inside of the T class :
public class T {
public static void main(String[] args) {
A obj = new C();
obj.getT();
}
}
With a reference variable of type A, you cannot call getS(), because it could be any type that implements A, say, AImplementer, that doesn't extend from B. Any A object doesn't necessarily have a getS() method. It's only guaranteed to have a getT() method.
If obj has to be a type A and you need to call getS(), then include the getS() method in the definition of the A interface:
public interface A {
public String getT();
public String getS(); // Add this line.
}
Then B is still an A, and you can call getS() on an A reference variable.
By the way, I don't see any static methods in your code. Static methods cannot be overridden.
To gain access to the method you will need to downcast obj to B.
So you could have:
((B)obj).getS();
Downcasting: http://www.programmerinterview.com/index.php/java-questions/downcasting-in-java/
the type class of obj is A, but A hasn't method getS() so you can't call in the main method.
obj must be class B or class C.

Implement two interfaces in an anonymous class

I have two interfaces:
interface A {
void foo();
}
interface B {
void bar();
}
I am able to create anonymous instances of classes implementing either of these interfaces like so:
new A() {
void foo() {}
}
or:
new B() {
void bar() {}
}
I want to create an anonymous class that implements both interfaces. Something like (the fictitious):
new A implements B {
void foo() {}
void bar() {}
}
This obviously gives a compile error: "B cannot be resolved to a type".
The workaround is quite simple:
class Aggregate implements A, B {
void foo() {}
void bar() {}
}
I then use Aggregate where ever I would have used the anonymous class.
I was wondering if it is even legal for an anonymous class to implement two interfaces.
"An anonymous inner class can extend one subclass or implement one
interface. Unlike non-anonymous classes (inner or otherwise), an anonymous
inner class cannot do both. In other words, it cannot both extend a class and
implement an interface, nor can it implement more than one interface. " (http://scjp.wikidot.com/nested-classes)
If you are determined to do this, you could declare a third interface, C:
public interface C extends A, B {
}
In this way, you can declare a single anonymous inner class, which is an implementation of C.
A complete example might look like:
public class MyClass {
public interface A {
void foo();
}
public interface B {
void bar();
}
public interface C extends A, B {
void baz();
}
public void doIt(C c) {
c.foo();
c.bar();
c.baz();
}
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.doIt(new C() {
#Override
public void foo() {
System.out.println("foo()");
}
#Override
public void bar() {
System.out.println("bar()");
}
#Override
public void baz() {
System.out.println("baz()");
}
});
}
}
The output of this example is:
foo()
bar()
baz()
For save some keystrokes (for example if the interfaces have a lot of methods) you can do this:
abstract class Aggregate implements A, B {
}
new MyObject extends Aggregate {
void foo() {}
void bar() {}
}
Notice the key is to declare the Aggregate as abstract.
Note that you can make a named local class that implements the two interfaces:
void method() {
class Aggregate implements A, B {
void foo() {}
void bar() {}
}
A a = new Aggregate();
B b = new Aggregate();
}
This save you from doing a class-level or top-level class declaration.
The result is called a local class. Local classes declared in instance methods are also inner classes, which means that they can reference the containing object instance.

Categories