I have multiple java files that I am working on which use many of the same methods. As of now when I update any of the methods I will have to find that method in the other files and update them by hand. I am wondering how I can do this through implementing the methods from a "master file" which will hold the bodies of the many different methods that I am using universally. In my research I thought that I could do this through interfaces, but when I create an interface, it does not allow me to add a body... So, obviously I am doing something wrong there or using interfaces incorrectly. In any case, what is the best was to accomplish this goal?
-
Consider using ab abstract class in place of an interface. Good reading on the topic: [link](http://stackoverflow.com/q/761194/335858). – Sergey Kalinichenko Jun 27 '13 at 11:18
3 Answers
If the classes are related and there is a parent child relationship, then you can have a method in your parent class(maybe abstract), which will hold this common code.
If classes are unrelated but still they have some common code, then moving the common code to a utitlity class can be a good idea.
More comments can be provided only seeing the code.

- 67,789
- 12
- 98
- 136
-
Well, yes, I am trying to move it to a "utility class" and successfully implement that class in all of the other files, so that the "utility class" methods can be called without the bodies having to be written in full in the different files. So how would I do this...? That is the actually question... – user2510325 Jun 27 '13 at 11:21
-
@user2510325 If you are implementing the utility class, then simply make all the those methods as public static. And in all other classes, where you need that method, call it in a static way something like Utilityclass.commonMethod(). – Juned Ahsan Jun 27 '13 at 11:37
It sounds like you need to use parent (possibly abstract) and child classes.
Parent class:
public class Parent {
public void method1() {
System.out.println("You can call this method from Child");
}
}
Child class:
public class Child extends Parent {
// Because you're extending Parent, it will allow you to use the method in your parent class.
public void method2() {
System.out.println("You can't call this method from Parent");
}
}
Now if you call your method, you can do it like this:
Parent parent = new Parent();
parent.method1();
Child child = new Child();
child.method1();
child.method2();
Output:
"You can call this method from Child"
"You can call this method from Child"
"You can't call this method from Parent"
If you edit method1 in your parent class now, it will automatically change the output that your child class will give.

- 3,572
- 3
- 27
- 45
-
-
@Ruchi I know, this is intended. In the case that OP is a beginner and tried to use the code, it might confuse him if it were abstract and he tries to instantiate it in the future. It doesn't HAVE to be abstract. That's why I put "possibly abstract" between brackets. – JREN Jun 27 '13 at 11:28
Because you have tagged the question "multiple inheritance" (of classes) I assume that you've found that to be impossible in Java, and that using an abstract class doesn't make sense in your hierarchy.
What you could do is take advantage of the delegation technique: make a class class A and put all common methods there. In class B and C you could make an instance of class A and use all the methods of that class. Example:
A delegate = new A();
delegate.someMethod();
Read more about the delegation pattern here.

- 694
- 4
- 8
- 21