I have 3 classes A, B, and C. These extend another class D.
Class D has a method that is used in all classes A, B, and C.
Now the problem is that classes A, B, and C should extend different classes and use just the same method from class D.
I can't believe that I should copy and paste the method in all my classes. Is there something like an include for function in C?
By the way I'm working on an Android app. Class D extends Activity and has a method for managing a common menu of Android activities A, B, and C (this is the official approach as reported in Android's documentation). However I need that these activities extend different classes such as ActivityList and not just the Activity class.
Having two or more methods named the same in the same class is called overloading. It's not overloading if you have the same method name in two different classes.
The only way to pass the same object to two functions of different classes is if one class inherits from the other, and the Object is an instance of the inherited class. The other way would be to weakly type the object upon variable definition and function definition.
A class can implement multiple interfaces and many classes can implement the same interface. Final method can't be overridden. Thus, an abstract function can't be final.
Yes. Because main is a static method so it is not bound to an object like non-static method does. static methods are class level while non-static methods are object level. You can also create an instance in a non-static method for that matter.
If your method doesn't need to access private state, add a static method in class D, and call the static method from A, B, C.
If your method does need to access private state, see if you can factor out the need to use private state by adding a package-private getter to each class, and then use the method in A.
Otherwise, try factoring out some of the logic to a common interface rather than superclass.
Otherwise, try delegating to a helper class. (e.g. composition rather than inheritance as @Marcelo indicated)
Otherwise, repeat the methods in each class A, B, C.
as an example of the common interface approach, combined with a static method in D:
interface MyThing
{
public void doMyThing(String subject);
public List<String> getThingNames();
}
class D
{
static void doSomethingComplicatedWithMyThing(MyThing thing)
{
for (String name : thing.getThingNames())
{
boolean useThing = /* complicated logic */
if (useThing)
thing.doMyThing(name);
}
}
}
class A extends SomeClass implements MyThing
{
/* implement methods of MyThing */
void doSomethingComplicated()
{
D.doSomethingComplicatedWithMyThing(this);
}
}
class B extends SomeOtherClass implements MyThing
{
/* implement methods of MyThing */
void doSomethingComplicated()
{
D.doSomethingComplicatedWithMyThing(this);
}
}
class C extends YetAnotherClass implements MyThing
{
/* implement methods of MyThing */
void doSomethingComplicated()
{
D.doSomethingComplicatedWithMyThing(this);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With