Is it possible to extends 2 different classes in Java? - java

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?

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

You can use Strategy pattern:
link link
You should create interface, then your classes should implemented this interface like in example in second link.

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

Related

How to identify all classes implementing a specific interface that do NOT extend some base class?

I have some interface Action, and most of the classes implementing that interface are derived from some base AbstractAction. But I assume there are some classes that implement that interface, but that do not extend that base class.
Is there a way using IntelliJ to identify such classes? (ideally: using the community edition)
Edit: this is not a duplicate of How to show all parents and subclasses of a class in IntelliJ IDEA?, as I am looking to combine a condition like "implements X and NOT extends Y".
Works only in Intellij IDEA Ultimate Edition:
The only thing that comes into my mind to sort of solve your problem directly with Intellij IDEA is to generate the uml class diagramm of your Action interface.
This lets you search visually for hierarchy patterns.
Here is a diagramm for the JTextComponent as an example:
Another approach - Using the right tool for the job
jqassistant is a tool that analyses your java code and its relations and stores that into a neo4j database.
This enables you to describe your problem as graph query with cypher.
The easiest way to get started is to
download the binary distribution
jqassistant
run ./bin/jqassistant.sh scan -f your_application.jar, then
start the server via ./bin/jqassistant.sh server
and open http://localhost:7474/browser/
alternatively use the Intellij plugin for jqassistant
Example:
The query for finding all classes implementing aInteface would look like
MATCH (i:Interface {name:"aIntefaces"} )<-[:IMPLEMENTS]- (c) RETURN i,c
A query to your problem would look like:
MATCH
(i:Interface {name:'Action'} )<-[:IMPLEMENTS|EXTENDS*1..10]- (class),
(abstractAction:Class {name:'AbstractAction'})
where not (class)-->(abstractAction)
RETURN class

Android: extends from multiple classes

I have an application with the name MyApplication that extends from library application RoboInjectableApplication
public class MyApplication extends RoboInjectableApplication {
}
But now I want MyApplication to extends from another library application XWalkApplication so that my application have extension from RoboInjectableApplication and XWalkApplication how to achieve that
This is not possible Java only allows one super class. You may check the source codes of both classes and write a merged class which has all compatibilities.
If you would need a different super class in a different context you may try the flavors of Android Studio. There you can override classes which generates a different APK.
In java you can extend only one class.
You can check if you can use class field (variable) to have XWalkApplication functionality - this is often called composition.
Another way is as already suggested, you can merge the source code.
Another way is to play with interfaces if this is possible.
Multiple inheritence is not supported in java. It will cause ambiguity in few scenarios. One of the most common scenario is Diamond problem. You can read more about this from here.
In short it is not possible to extend from multiple classes.

more than one 'Extends'

public class MainActivity extends fragment
public class MainActivity extends Activity
The issue I have is that I need to extend Fragment to be able to use the 'pager_title_strip' but to use a list view that can read json, it also needs to be extended.
How can I extend both within the same class?
You can't, a class can only extend one other class.
It can however implement multiple interfaces, maybe that can help you in your design.
Abstractly spoken, multiple inheritance is not the only answer to such design problems, and in Java it was excluded by designers of Java (Gosling et.al.) because they saw it as inherently unsafe. Still in Java 8 multiple inheritance is only allowed for behaviour (see interface inheritance and new "default" methods), but not in state.
Alternatively, you can also think about composition or delegate patterns. From Joshua Bloch we know his recommendation "Favor composition for inheritance". I think this should be possible in your case.
There is FragmentActivity or ActionBarActivity class which may solve your problem!
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
The issue I have is that I need to extend Fragment to be able to use the 'pager_title_strip'
There is no requirement to use fragments at all with a ViewPager, let alone with a PagerTitleStrip or PagerTabStrip.
so saying that does this mean you aren't able to use a listview box that reads json and the pager title strip at the same time?
It is perfectly possible to "use a listview box that reads json and the pager title strip at the same time". However, you will need to create more than one Java class, in all likelihood, such as an Activity and a Fragment.

Extending a class--how do I avoid having to create multiple classes?

The question doesn't really make sense, but this should help explain it better.
I'm currently using the IOIO, and it works like this:
public class MainActivity extends CustomIOIOActivity {}
From there I can call classes I've made in CustomIOIOActivity, which extends Activity.
The problem I'm running into now is that I have to inherit classes different from just Activity. For example, FragmentActivity.
Is there a way for me to create a custom IOIO activity which I can use for when I extend Activity and FragmentActivity? I don't want to have to make two essentially identical classes, which is what I am doing right now--seems silly. I'm not very familiar with Java, perhaps this is easy to do.
In Java you can't inherit class from more than one superclass. In order to achive your purpose, I would advise you to move all common logic to another class(IOIOHolder, for example), which would take an Activity parameter in constructor. Later you can create two different classes extending Activity and FragmentActivity, each containing an exemplar of IOIOHolder and using it for it's own tasks. By using this approach you can write IOIO code once and use it in any activity you want.

Make a Java class have higher compile priority than one inside a library in IDEA

We have some portion of functionality packed in an external library and it is attached to our project. That library can't be changed in any way. Amongst others there are two classes lying inside it: com.myorg.Grandpa and com.myorg.Dad that extends com.myorg.Grandpa. Also there are com.myorg.Grandson extending com.myorg.Dad and a few other classes outside of the library extending com.myorg.Grandpa.
I decompile com.myorg.Grandpa class and add a new method new_method() to it.
Then I try to use new_method() in com.myorg.Grandson but IDEA won't let me do it cause Grandson extends Dad which extends library's Grandpa which doesn't contain new_method().
I tried to delete Grandpa from library and surprisingly IDEA didn't say a word and successfully compiled a project despite of the fact that in the boundaries of a library Dad extends non existing class.
The question is how to force Dad to extend a new Grandpa without deleting the one inside a library?
You could
Add an abstract class between Dad and GrandSon: Extend Dad, and add your method in the sub class. Then derive GrandSon from that sub class.
Put an instance of Dad in a new class, and let your IDE create delegate methods to the aggregated Dad instance. Again add your new method to the new class.
There is another possibility:
If you have to modify classes in place, use aspectj to weave in code: aspectj changes the byte-code (it does not need source code) at run-time. This way you can add methods or fields.
The fact is that you are duplicating classes with full package signature, so you will get the one that the classloader loads first. I know that in Websphere you can tweak classloader priorities, but couldn't say in your case.
Anyway, why not just do it without decompiling? You are causing yourself hard coupling to an external library and bad practices (maybe copyright issues) by decompiling/customizing. Besides, if the library gets updated, you will run into trouble having to reconstruct your customized classes.
Options:
Create your own implementation, for instance:
Create an Interface that replicates all methods in Grandpa plus the one you need.
Extend Grandpa class and implement the added method from your interface, all other methods will be left intact.
Extend all other extending classes from your own class hierarchie.
Instead of using the libraries common class, use your Interface as naming
This way you are kind of creating your own interface to the library, if it changes, you know where to make changes.
You could even do it without the interface, it's kind of wrapping the functionality, it would depend on what you need to achieve.
Anyway, I would try to solve it by own code and not by messing up with the library, it is just not worth it to do such tricks, if a new Programmer takes the project, they will need a lot of time to find out why and how it behaves.
Now, there might be variations in how to structure the class hierarchie, but it would depend on the specific implementation you need, so you would have to post more detailed data on what the library is and what you're trying to add to it if you expect some more specific answer...
Regards
It has to appear first to the class loader.
IDEA should load your class first if is in your project. You may also try to create a separate library for your class and include it in your project.
See also: http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html

Categories

Resources