Use method from other class like native methods? - java

(newbie in Java) I couldn't find exactly this question on SO. I have project, with two files (phseudo-code):
First Java File (class)
public class A {
public void xyz() { System.out.println("hello");}
}
Second Java File (class)
public class B Extends ZZZZZ {
public void callme() {
xyz(); // <----------------- I want to call in this way, but It cant be done like this.
}
}
How to make xyz() to call successfully (like as if was defined inside b() class natively !!).
p.s. again, I don't want to call it with classname in front, like this:
a.xyz();

The whole idea of instance methods, like xyz is in this, is that you are using the state of an instance of A in the method, without having to pass that instance as an argument like this:
... String xyz(A thisInstance, ...) {...}
Instead you use:
A thisInstance = ...;
thisInstance.xyz(...);
That's why you need an instance of A, because it is practically an argument to the function.
However, if you don't need an instance of A, you can make the method static:
static String xyz(...) {...}
Then you can call it without passing an instance of A:
A.xyz(...);
You can use a static import so that you don't have to write A:
import static A.xyz;
...
xyz(...);

Okay several possibilities:
Instantiate A:
A a=new A();
a.xyz();
(you do not want this)
Heredity:
public class B extends A {...}
and
public class A extends ZZZZZ{...}
so you can still extend ZZZZZ;
Interface:
public interface A{...}
public class B extends ZZZZZ implements A{...}
Static Method:
public class A{
public static void xyz()
{
System.out.println("hello");
}
}
public class B{
public void callme()
{
A.xyz());
}
}

This will help you.
class A {
public void xyz() {
System.out.println("hello");
}
}
class ZZZZZ extends A{
}
class B extends ZZZZZ {
public void callme() {
xyz();// <----------------- calling this shows error
}
}

Related

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)

Calling the Static Method of

This is a bit weird and might ring of iffy syntax but hold with me. I've been trying for three months and I'm convinced that I need a way to do this:
public abstract class Sup{
...
//This is implemented here because I cannot create an abstract static
//only implemented by the children but called statically by methods in
//the parent (more info later on in the post):
protected static Class<? extends Sup> getTypeClass(){ return Sup.class };
public static void init(){
...
alreadyDeclaredHashMap.put(getTypeClass(), hashMapOfOtherStuff);
}
}
public class A extends Sup{
static{
init();
}
protected static void getTypeClass(){ return A.class };
}
public class B extends Sup{
static{
init();
}
protected static void getTypeClass(){ return B.class };
}
... and so on.
So that if I were to print out alreadyDeclaredHashMap, it would look like:
class A -> hashMapOfOtherStuff
class B -> hashMapOfOtherStuff
class C -> hashMapOfOtherStuff
...
But instead it prints:
class Sup -> hashMapOfOtherStuff
class Sup -> hashMapOfOtherStuff
class Sup -> hashMapOfOtherStuff
...
Because the extending classes hide getTypeClass() but can't override it. This is just an example. In reality, I am making a Units system and I have a lot of methods depending on getTypeClass() and would really love to not have to rewrite them in every extending class (of which there are an indefinite number) with the only difference in implementation being the class name.
Many thanks!
P.S. These methods do have to be static because they are being called statically (and I would rather not have to create a dummy instance or reflection just to call them).
There is no way to get that to work. The static code in class sup has no knowledge of class A and class B, even when the init method is invoked from one of them.
Static methods are not "virtual", so calling getTypeClass() from the static code in Sup will call that implementation, not any of the subclass implementation.
Now, if you want to reuse the init method from A and B, you'll have to pass as parameters.
public abstract class Sup{
...
public static void init(Class<? extends Sup> typeClass) {
...
alreadyDeclaredHashMap.put(typeClass, hashMapOfOtherStuff);
}
}
public class A extends Sup {
static {
init(A.class);
}
}
public class B extends Sup {
static {
init(B.class);
}
}

Java - inheritance, class with methods

I have my subclass:
public class Actions extends Main{
public void getFireTarget() {
GameObject target = getGameObjects().closest("Target");
do{
log("Shooting at the target");
getMouse().click(target);
} while(target != null && !getDialogues().inDialogue() && getLocalPlayer().getTile().equals(rangeTile));
}
}
I want to write similar methods, so I can call them in my Main class, so I don't have to write over and over.
My main class looks like this (won't fully paste it as it's long):
public class Main extends AbstractScript{
...code here
Actions actions = new Actions();
}
So I am trying to implement the methods in Actions by doing actions.getFireTarget(), which seems to work. But when I compile, I am getting two compile errors:
1) In the Main class, in the line: Actions actions = new Actions();
2) In the Actions class, in the line where I am extending the superclass.
Am I missing something in the sub class in order to store methods and then call them in the main method? Please advise! Thanks
The syntax is wrong. () are not allowed here: public class SomeName(). Remove the brackets.
I am having trouble understanding your details.
Here is a short info about inheritance:
public class A{
protected int t;
public void methodA(){}
}
public class B extends A{
#Override
public void methodA(){}
public void methodB(){}
}
If you override the methodA in class B, any call from a instance of class B to the method will use the method defined in class B. (If you dont write the method in class B, it will use the method from class A)
Objects of class A cannot use methodB() defined in class B.
Also you can access the field t in class B, because of the protected modified.
I think its better for you your problem to instantiate other class to your main class if you have same function name or same variable name. try to compile and run this code
public class First{
Second sec = new Second();
String s = "This is first";
public First(){
System.out.println(this.s);
System.out.println(getSecondString());
System.out.println(sec.getSecondString());
}
public String getSecondString(){
return "This is first";
}
public static void main(String args[]){
new First();
}
}
public class Second {
String s = "This is second";
public String getSecondString(){
return s;
}
}

syntax to call un-overridden method

I have
public class D extends B
{
public void method() {}
}
public class B
{
public void method() {}
public void anotherMethod() { method(); }
}
In the above, if you hold an instance of D, say d, d.anotherMethod() results in calling D.method.
Is there a syntax in Java to call B.method() from inside anotherMethod()?
No, there isn't. The derived class would have to contain a call to super.method().
If B wants to prevent subclasses from overriding method(), it should declare method() as final.
You still can call the super method by explicitly using super like this this:
public class D extends B{
public void method() {}
public void anotherMethod() { super.method(); }
}
The only thing that is required is for you to override anotherMethod().
Another way is thinking like this. You want anotherMethod to call B method() so:
public class D extends B{
public void methodInternal() {}
}
public class B{
public final void method() {
//...
methodInternal();
}
public void methodInternal() {}
public void anotherMethod() { method(); }
}
Here the user can create his own version of method() by overriding methodInternal(), but still the behavior of the original method() is intact.
You can make method() static in B and then call it as B.method() from the instance methods where it is needed (you may need to rename the static method if you want to use the name for the instance method).
At least that's what I do in similar situations.
Maybe you should also reconsider your design.

Interface questions

Suppose that I have interface MyInterface and 2 classes A, B which implement MyInterface.
I declared 2 objects: MyInterface a = new A() , and MyInterface b = new B().
When I try to pass to a function - function doSomething(A a){} I am getting an error.
This is my code:
public interface MyInterface {}
public class A implements MyInterface{}
public class B implements MyInterface{}
public class Tester {
public static void main(String[] args){
MyInterface a = new A();
MyInterface b = new B();
test(b);
}
public static void test(A a){
System.out.println("A");
}
public static void test(B b){
System.out.println("B");
}
}
My problem is that I am getting from some component interface which can be all sorts of classes and I need to write function for each class.
So one way is to get interface and to check which type is it. (instance of A)
I would like to know how others deal with this problem??
Thx
Can you not just have a method on the interface which each class implements? Or do you not have control of the interface?
This would provide both polymorphism and avoid the need to define any external methods. I believe this is the intention of an interface, it allows a client to treat all classes implementing it in a non type specific manner.
If you cannot add to the interface then you would be best introducing a second interface with the appropriate method. If you cannot edit either the interface or the classes then you need a method which has the interface as a parameter and then check for the concrete class. However this should be a last resort and rather subverts the use of the interface and ties the method to all the implementations.
It sounds like you are after something like this:
public static void test(MyInterface obj){
if(obj instanceof A) {
A tmp = (A)obj;
} else if(obj instanceof B) {
B tmp = (B)obj;
} else {
//handle error condition
}
}
But please note this is very bad form and indicates something has gone seriously wrong in your design. If you don't have control of the interface then, as suggested by marcj, adding a second interface might be the way to go. Note you can do this whilst preserving binary compatibility.
I'm unclear on what you're actually asking, but the problem is that you don't have a method that takes a parameter of type MyInterface. I don't know what the exact syntax is in Java, but you could do something like if (b is B) { test(b as B) } but I wouldn't. If you need it to be generic, then use the MyInterface type as the variable type, otherwise use B as the variable type. You're defeating the purpose of using the interface.
I'm not sure if I fully understand the issue, but it seems like one way might be to move the test() methods into the child classes:
public interface MyInterface {
public void test();
}
public class A implements MyInterface{
public void test() {
System.out.println("A");
}
}
public class B implements MyInterface{
public void test() {
System.out.println("B");
}
}
public class Tester {
public static void main(String[] args){
MyInterface a = new A();
MyInterface b = new B();
b.test();
}
}
You could similarly use a toString() method and print the result of that. I can't quite tell from the question, though, if your requirements make this impossible.
I think visitor design pattern will help you out here. The basic idea is to have your classes (A and B) call the appropriate method themselves instead of you trying to decide which method to call. Being a C# guy I hope my Java works:
public interface Visitable {
void accept(Tester tester)
}
public interface MyInterface implements Visitable {
}
public class A implements MyInterface{
public void accept(Tester tester){
tester.test(this);
}
}
public class B implements MyInterface{
public void accept(Tester tester){
tester.test(this);
}
}
public class Tester {
public static void main(String[] args){
MyInterface a = new A();
MyInterface b = new B();
a.accept(this);
b.accept(this);
}
public void test(A a){
System.out.println("A");
}
public void test(B b){
System.out.println("B");
}
}
Use only one public class/interface in one .java file, otherwise it'll throw error. And call the object with the object name.. You declared two methos in Teater class only, then what the purpose of declaring class A,B.
I usually use an abstract class to get around this problem, like so:
public abstract class Parent {}
public class A extends Parent {...}
public class B extends Parent {...}
That allows you to pass Parent objects to functions that take A or B.
You have 3 options:
Visitor pattern; you'll need to be able to change the MyInterface type to include a method visit(Visitor) where the Visitor class contains lots of methods for visiting each subclass.
Use if-else inside your method test(MyInterface) to check between them
Use chaining. That is, declare handlers ATester, BTester etc, all of which implement the interface ITester which has the method test(MyInterface). Then in the ATester, check that the type is equal to A before doing stuff. Then your main Tester class can have a chain of these testers and pass each MyInterface instance down the chain, until it reaches an ITester which can handle it. This is basically turning the if-else block from 2 into separate classes.
Personally I would go for 2 in most situations. Java lacks true object-orientation. Deal with it! Coming up with various ways around it usually just makes for difficult-to-follow code.
Sounds like you need either a) to leverage polymorphism by putting method on MyInterface and implementing in A and B or b) some combination of Composite and Visitor design pattern. I'd start with a) and head towards b) when things get unwieldy.
My extensive thoughts on Visitor:
http://tech.puredanger.com/2007/07/16/visitor/
public interface MyInterface {}
public class A implements MyInterface{}
public class B implements MyInterface{}
public class Tester {
public static void main(String[] args){
MyInterface a = new A();
MyInterface b = new B();
test(b); // this is wrong
}
public static void test(A a){
System.out.println("A");
}
public static void test(B b){
System.out.println("B");
}
}
You are trying to pass an object referenced by MyInterface reference variable to a method defined with an argument with its sub type like test(B b). Compiler complains here because the MyInterface reference variable can reference any object which is a sub type of MyInterface, but not necessarily an object of B.There can be runtime errors if this is allowed in Java. Take an example which will make the concept clearer for you. I have modified your code for class B and added a method.
public class B implements MyInterface {
public void onlyBCanInvokeThis() {}
}
Now just alter the test(B b) method like below :
public static void test(B b){
b.onlyBCanInvokeThis();
System.out.println("B");
}
This code will blow up at runtime if allowed by compiler:
MyInterface a = new A();
// since a is of type A. invoking onlyBCanInvokeThis()
// inside test() method on a will throw exception.
test(a);
To prevent this, compiler disallows such method invocation techniques with super class reference.
I'm not sure what are you trying to achieve but it seems like you want to achieve runtime polymorphism. To achieve that you need to declare a method in your MyInterface and implement it in each of the subclass. This way the call to the method will be resolved at run time based on the object type and not on the reference type.
public interface MyInterface {
public void test();
}
public class A implements MyInterface{
public void test() {
System.out.println("A");
}
}
public class B implements MyInterface{
public void test() {
System.out.println("B");
}
}
public class Tester {
public static void main(String[] args){
MyInterface a = new A();
MyInterface b = new B();
b.test(); // calls B's implementation of test()
}
}

Categories