Can I use implements and extends at the same time? - java

Can i use the implements and the extends at the same time? Because I need to inherit something on the other class while i used implements on the same class.
public class DetailActivity extends AppCompatActivity
implementsView.OnClickListener "extends ListActivity"
How can it be like that?

Yes, you can. But you need to declare extends before implements:
public class DetailActivity extends AppCompatActivity implements Interface1, Interface2 {
// ...
}
Any number of interfaces can be implemented, if more than one then each needs to be separated with a comma.

You can only extend one class but you implements multiple interfaces as your need.

class Human extends Monkey implements BasicAnimals, xyz
in this way you can extend a class as well as implement interfaces.

Related

How to add multiple implementations after AppCompatActivity?

How to add multiple implementations after extending the AppCompatActivity?
Why am I getting this error? Please correct this piece of code.
public class MainActivity extends AppCompatActivity
implements PopupMenu.OnMenuItemClickListener
implements NavigationView.OnNavigationItemSelectedListener{
Change it to
public class MainActivity extends AppCompatActivity
implements PopupMenu.OnMenuItemClickListener, NavigationView.OnNavigationItemSelectedListener{
You should read this too.
You shouldn't use implements twice to do so, just use it like this:
public class MainActivity extends AppCompatActivity
implements firstImplement, secondImplement {}

Interface hierarchy

I have a problem with interface hierarchy. My projects has four class and six interface in a rigid structure, that is:
public interface Set<T, N extends Settable<T,N>>
public interface Settable<T, N>
public interface Sequence<T, N extends Sequenceable<T,N>> extends Set<T,N>
public interface Sequenceable<T,N> extends Settable<T,N>
public interface DisjoinedSet<T, N extends DisjoinedSettable<T,N>> extends Set<T,N>
public interface DisjoinedSettable<T,N> extends Settable<T,N>
public class List<T> implements Sequence<T, NodeList<T>>
public class NodeList<T> implements Sequenceable<T,NodeList<T>>
public class ListDisjoined<T> extends List<T> implements DisjoinedSet<T,NodeListDisjoined<T>>, Sequence<T,NodeListDisjoined<T>>
public class NodeListDisjoined<T> extends List<T> implements DisjoinedSettable<T,NodeListDisjoined<T>>, Sequenceable<T,NodeListDisjoined<T>>
Beside the horrible naming, the idea is that by creating container-contained couples of classes/interfaces I gain an enormous amount of sinergy: I can simply write the body of those methods inside the comments of the interfaces and copy&past on a whole brunch of classes, without changing anyting.
However, I got two errors on the last two class:
The interface Sequence cannot be implemented more than once with different arguments: Sequence<T,NodeList<T>> and Sequence<T,NodeListDisjoined<T>>
The interface Sequenceable cannot be implemented more than once with different arguments: Sequenceable<T,NodeList<T>> and Sequenceable<T,NodeListDisjoined<T>>
NodeListDisjoined is a subclass of NodeList, so what is the problem?
Note: the edit written under this line works. I still do not understand why.
public interface DisjoinedSet<T, N extends DisjoinedSettable<T,N>> extends Sequence<T,N>
public interface DisjoinedSettable<T,N> extends Sequenceable<T,N>

Java: how to create a generic class that requires a data type which extends a class and implements an interface?

I want to create a composite in GWT that require a class which extends a class and implements an interface. Psudo code as below (it does not work apparently):
class GridRow<T extends Widget implements HasText> extends Composite{
//more codes here
}
That's written as:
class GridRow<T extends Widget & HasText> extends Composite {
// ...
}
You can have [class or interface]( & [interface])* (in pseudo regex language).

Java generics - is it possible to restrict T to be Serializable?

Is it possible to make something like this? I know that implements cannot be in the <>, but I want to restrict the T to be Serializable somehow.
public class Clazz<T implements Serializable> {
...
}
public class Clazz<T extends Serializable> {
...
}
Just use extends instead of implements.
Yes, just use extends instead of implements.

Extending class with generic parameter

I need to derive from the following class:
public abstract class MyTool<VIEW extends MyView>
implements LookupListener, MouseListener, MouseMotionListener, KeyListener {}
The following does not work:
public abstract class MySubTool<VIEW> extends MyTool<VIEW> {}
Thanks !
The compiler in MySubTool as no way of knowing that VIEW in MySubTool is a subclass of MyView, you have to specify it again:
public abstract class MySubTool<VIEW extends MyView> extends MyTool<VIEW> {}
This should:
public abstract class MySubTool<VIEW extends MyView> extends MyTool<VIEW> {}

Categories

Resources