How to extends two utilities classes? - java

I extended two classes (TimerTask and Jframe) to my class but it shows me an error, is there any way to correct the error? I need them to set a timer in my GUI classes.

Java does not support multiple inheritance. You cannot extend two class in Java. However, you can implement multiple interfaces but not extend multiple class. Try to rethink your design to handle it in a different way.

Related

Workaround for a lack of multiple inheritance

I am currently working on a videogame (assignment), and the problem I have is the following:
The player can move vertically and horizontally, but some other objects can only move vertically OR horizontally. I would love to have an abstract class "HorizontalMovingObject" and "VerticalMovingObject", and have an abstract class "MovingObject" which extends both these classes. I was wondering if there happens to be a workaround for this?
I currently have these abstract classes, but find myself constantly copying code from the unidimensional moving classes to the regular moving class. I have tried a couple of things, but all felt wrong somehow. I worked through interfaces for the unidimensional moving parts, but there is a default underlying behavior that should extend to all objects moving in that direction, and thus it feels wrong to write these over and over again. I tried the reverse and have for example the horizontal class extend the MovingObject class, but with all its vertical components set to 0, which somehow felt even worse.
I know this might not seem like a very important detail, but it has been driving me crazy for some reason. Mostly because I come from C++.
Any help is appreciated!
I would love to have an abstract class "HorizontalMovingObject" and
"VerticalMovingObject", and have an abstract class "MovingObject"
which extends both these classes.
The workaround for that is interface.
MovingObject should implement both HorizontalMovingObject and VerticalMovingObject
I currently have these abstract classes, but find myself constantly
copying code from the unidimensional moving classes to the regular
moving class. I have tried a couple of things, but all felt wrong
somehow.
Java 8 introduces default methods that allow to define default implementations for interfaces.
Note that if a class implements both HorizontalMovingObject and VerticalMovingObject , which both have a same default method, you should override it to make the class compile : default interfaces allow to mix inherited behaviors from distinct interfaces while no ambiguity is present about which method is inherited. When it is not the case, a compilation error occurs.
An alternative to Java 8 interfaces default method to achieve this requirement is using the decorator pattern : define each part of behavior that you want to reuse in a specific decorator class, chain the decorators designed to work together and and add these to the decorated class that need these specific behavior.
This approach favors composition over inheritance and requires more boiler plate code but is more flexible.
One workaround that can be done for multiple inheritance is using default methods of interfaces.
I personally don't think it's a good idea, this is not what default methods are there for, but technically it's a loop-hole you can expliot
A better solution for me can be to have some MovingObjectAbs class that will hold the shared direction-agnostic logic that both horizontal and vertical classes will extend, or to have some utility classes that will have shared logic.

Android Java App: Extending two classes (walk around)

I have two classes, ImageMap, extending ImageView and PageView extending GLSurfaceView, I am using the ImageMap to mainly have hot spots on drawables but I also need to add a page flip/curl animation to it, in order to do that I need those two classes to act as one object, any idea how to do that?
It is totally clear to me that multiple inheritance is not allowed in java.
There is no way of really extend two classes. What you can do is:
You make a wrapper object, that holds one instance of each object. and simply do this.ImageMap.filed1 and so. This is more convenient while developing the class. This also allows you to proxy method invocations.
You define interfaces which should be implemented, and you make a new class which implements both. This is only for class that use this class to have the interface, without really caring about the implementation.
You may need both things, since the first is about "how to do it" and the second about "how it will be presented to objects that use it".
Your question is not about Android; it's about Java.
Java does not allow for multiple inheritance.
Your reasoning is inaccurate regarding the following:
in order to do that I need those two classes to act as one object
That's not the case. An 'Activity', for example, does not have to be an event handler; it's enough if your 'Activity' can have an event handler, e.g. as an inner class which can access the Activity's variables.

inherit from 2 already defined classes in java

Im trying to inherit from multiple classes, I have been told I should use interface, but the problem is that these are predefined classes in java, such as example activity class and Fragment class
Help would be appreciated!!
It seems like a contradiction in design, doesn't it ? An Activity may contain Fragments. If you make something both an Activity and Fragment, what are you trying to do ? =)
No, you can never inherit from two classes in Java.
You can only extend from one class in Java. In your case, FragmentActivity seems to be the right choice. Otherwise interfaces are the way to go. Fragments and Activities are different creatures in the android lifecycle and can't be treated equal.
Note that you can have instances of however many different classes in your own custom class though. But that class doesn't have to extend anything.
You could inherit from multiple classes in C++ and not Java. You need to rethink your strategy.

JAVA class implementation and extension issues

We have 2 JAVA classes related to a project, call them AGraph and CallGraph. We also have an interface named Graphism which is implemented by AGraph. There's one abstract class we have, named GraphSnippet, which basically creates a visualization of a ZEST graph.
Now, we want CallGraph to extend AGraph. My silly doubt here is, does CallGraph need to implement the interface Graphism as well?
Next, instead of making GraphSnippet an instance of the Graph class (Graph is a pre-defined class in the ZEST package), we want it to be an instance of CallGraph so that the functionalities of CallGraph and AGraph become a part of the implementation. And because we'll need the class AGraph during the later stages, we can't really knock this class out and get all its methods implemented in CallGraph.
Could someone help us with how to start with it? It's difficult for us, novice JAVA programmers, to figure out a way?
PS: We're working on Eclipse-JUNO.
Now, we want CallGraph to extend AGraph. My silly doubt here is, does
CallGraph need to implement the interface Graphism as well?
Yes, but you won't have to do anything. Because it extends from AGraph, it will already implement Graphism. You won't have to mark this anywhere in your code.

Is it possible in Java to have a class inherit from two different classes depending on some variable?

I have a parent class in Android that inherits from Activity and all my other activities inherit from that parent class. This parent does some life cycle stuff in onPause and onResume that all my activities need. The problem is I have a Map activity that must inherit from Android's MapActivity yet I still need this activity to have my parent classes life cycle methods. Is there a way to have the MapActivity inherit from two parents? Or maybe a partial class I'm not really sure here. Any Ideas would be great.
Thanks,
Bryan
The short answer is no. You cannot have a class that inherits from two classes in Java. The standard recommendation would be to use an interface, but I don't think that's right for you in this case.
Perhaps that you can achieve the code reuse you are looking for by using composition, for example, instead of inheritance. If you post a code example, I could give you a more specific answer.
Sorry but in Java you can only extend one class. However you can implement multiply interfaces. You could have a BaseMapActivity class extend a MapActivity and then have your MainMapActivity extend that BaseMapActivity. The easiest way would be to copy the code from the already existing base Activity and put it into the MainMapActivity.
In Java you can only extend from a single class, however you are able to implement multiple classes from a single class. Another thing to consider is chaining extended subclasses together (this simulates multiple inheritance).
A better description can be found here: http://www.javaworld.com/javaworld/jw-10-2005/jw-1024-multiple.html
It’s funny, I had exactly the same problem earlier this day (but with a PreferenceActivity).
Unfortunately, I don’t think it’s possible, I ended up making a copy of my parent class and changing the name and the extends Activity into extends PreferenceActivity.

Categories