-1

I am working on a project on SoftKeyboard. I am editing the sample codes provided with the Eclipse ADT bundle. I realised it is already extends InputMethodService. However, I want to attach a ContextMenu in the soft key.

Therefore in this case, I need to extend Activity too. How do i solve this problem?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Hao Ting
  • 93
  • 1
  • 3
  • 7
  • 3
    No. Java doesn't support multiple inheritance of classes. If you need to extend multiple classes, then you almost certainly have a design problem, and don't *really* need to extend multiple classes. Composition over inheritance might help. – awksp Jul 08 '14 at 08:21
  • @user3580294 Sorry, but that's BS. While you can't do so in Java, it's frequently a useful feature to be able to extend multiple classes, and is definitely not a design flaw. Its widely considered a design flaw of Java to allow it only in the handicapped manner of interfaces. – Gabe Sechan Jul 08 '14 at 08:30
  • @GabeSechan Sorry, just repeating I've read all over the place. Stuff like how if you want multiple inheritance, time to consider composition. Or how it just really isn't appropriate in a lot of cases. [Here's](http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85562.aspx) one example of a bajillion, although to be honest I'm not sure if it's just people like me repeating what they've heard, although I'd think that author's words would hold at least some weight. Mind providing sources for your last sentence? That's the first time I've heard of it, as well as it being "frequently useful"... – awksp Jul 08 '14 at 08:38
  • @GabeSechan I suppose it's a surprise to me to see someone arguing the other way... Curious as to why, when what I've heard is that MI isn't necessarily flawed in and of itself, it just leads to more bad situations than it's worth, and there usually is a better solution. – awksp Jul 08 '14 at 08:43
  • @user3580294 There's situations where MI is the best and situations where multiple inheritance is best. But just because there's places where it isn't a good idea doesn't mean you throw it out altogether. Name a language feature or pattern that can't be abused. The main problem with multiple inheritance is the diamond- when you inherit from 2 classes with a common base class. If you avoid that situation there aren't too many technical problems with it. – Gabe Sechan Jul 08 '14 at 08:49
  • As for examples of where it would be useful- a lot of places in Java. I can't tell you how many times I've been stuck copy pasting code because Java forced us to use an interface instead of multiple inheritance and I needed to implement the same function the same way in 3 different classes. Which means 3 places to bug fix and maintain- if you remember. More likely it means bugs go unfixed. – Gabe Sechan Jul 08 '14 at 08:52
  • In fact then entire existance of codeless interfaces in Java is pretty much a workaround to the fact they don't have multiple inheritance. What's the real difference between implementing an interface and extending a class? Both of them are "is a" relationships. Code using them works the same way- you hold a reference to the base class/interface and can use any of the functions on that object. If MI is evil, then interface inheritance is evil too- there's little conceptual difference between them. – Gabe Sechan Jul 08 '14 at 08:56
  • @GabeSechan Can't say I can argue with you there. I admit, I overstated my original comment; will tone that down in the future. I agree that MI isn't flawed in and of itself; it's just I've heard that it shouldn't be used, although judging by your most recent comment I have a *lot* of missing experience to have not run into a lot of places in my Java programming where multiple inheritance would have been useful. Just curious -- is there anything in particular that you do so that you seem to run into places where MI would be useful a lot more than the answerers/commenters on SO's MI questions? – awksp Jul 08 '14 at 08:57
  • The real reason that MI is missing, other than making the compiler a bit more difficult to write (but a solvable problem) is to avoid the diamond shape pattern, which does cause major technical issues, and was a big problem in early C++ as coders didn't realize it. But they should have just banned diamond inheritance rather than everything. – Gabe Sechan Jul 08 '14 at 08:58
  • @GabeSechan Uh, I've read that the primary difference between multiple inheritance of interfaces vs. classes was multiple inheritance of type vs. state/implementation, although Java 8 blurs that... I honestly can't tell you why multiple inheritance of type is OK while multiple inheritance of the others isn't, although I'm fairly certain that I've read the rationale somewhere... – awksp Jul 08 '14 at 09:01
  • @GabeSechan : Do you really think it's appropriate to hijack the comment thread of an SO question in order to discuss the theoretical pros and cons of a design pattern of a particular programming language? – Squonk Jul 08 '14 at 09:01
  • @user3580294 : Do you really think it's appropriate to hijack the comment thread of an SO question in order to discuss the theoretical pros and cons of a design pattern of a particular programming language? – Squonk Jul 08 '14 at 09:02
  • @GabeSechan You make valid points though. I don't have the knowledge nor experience to say much else. – awksp Jul 08 '14 at 09:02
  • possible duplicate of [Java Multiple Inheritance](http://stackoverflow.com/questions/21824402/java-multiple-inheritance) – RobV Jul 08 '14 at 09:03
  • @user3580294 The real reason people made an argument for a difference between the two types of inheritance back in the day was basically to try and find an excuse for Java not having it while its closest rival, C++, did. But don't take my word for it- read up on the subject and come to your own opinion. I've been around long enough to see the "prevailing opinion" on most of these issues change back and forth. So don't go by what a blogger or a guy on a random Q&A site says, figure out your own opinion. Then figure it out again in 3 or 4 years with more experience. – Gabe Sechan Jul 08 '14 at 09:08
  • @GabeSechan I suppose I know what I'm doing in my downtime then... Thank you for enlightening me! – awksp Jul 08 '14 at 09:11

3 Answers3

0

No, java does not support Multiple inheritance, Why because the child can't know which parent it has to choose if extends more than one class. To over come this you have to go for Interface.

For more info refer this, this and this

Community
  • 1
  • 1
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0

You can use Strategy pattern: link link

You should create interface, then your classes should implemented this interface like in example in second link.

Forin
  • 1,549
  • 2
  • 20
  • 44
0

Java does not support multiple inheritance.

You can try this workarounds:

1)Aggregation: make a class that takes those two activities as fields.

2)Interfaces.

3)Rethink your design

Iffo
  • 353
  • 7
  • 18