Should a java class x that extends class y, which implements interface z, also implement interface z
Date : March 29 2020, 07:55 AM
Hope this helps No. If the interface is implemented somewhere in the hierarchy of your current class, then that is enough. In the end, an interface just defines a few methods that a class should contain. To your subclass this is no different than if it were "normal" methods from the top class. public class Main {
public static void main(String[] args) {
new X().test();
new Y().test();
}
}
class X extends Y{
}
class Y implements Z{
@Override
public void test() {
System.out.println("in Y");
}
}
interface Z {
public void test();
}
class X extends Y implements Z
|
Cyclic inheritance and interfaces - class A can't implement class B interface while class B implements A interface
Date : March 29 2020, 07:55 AM
wish of those help I have: , It may help to draw it out. >A
is part of / \ inherits
V
AListener BListener
^
inherits \ / is part of
B<
|
How to pass an Object of a Class that implements an Interface with just knowing the Class of the Interface to a Method a
Date : March 29 2020, 07:55 AM
Hope this helps Lets say that I have an Interface and multiple Classes that implement this Interface: , As per generics for ? extends MyInterface only getters are allowed ArrayList<? extends MyInterface> objects = new ArrayList<>();
you can only do get of type MyInterface and Object
MyInterface myInterace = objects.get(0);
Object myInterace = objects.get(0);
Long longValue = 20L;
List<Long> longList = new ArrayList();
longList.add(longValue);
Integer i =10;
List<? extends Number> sNumber = longList ;// longList can be assigned to ? extends Number
sNumber.add(i); // if this was allowed
longValue = longList.get(1); // we will get an exception (class cast exception)
|
Does a class inherit an abstract inner class declared within an interface when the class implements that interface?
Date : March 29 2020, 07:55 AM
|
Adapting a Class to an Interface when it implements all of the interface but doesn't declare the interface
Tag : chash , By : chintown
Date : March 29 2020, 07:55 AM
hope this fix your issue Yes, the 3 methods you've done is all you need. A dictionary allegedly relies mostly on the hashcode. However your cast in Equals(object obj) will go wrong: it will cast a Booh to null. You want to test/cast both FooBar and just plain T. bool Equals(FooBar<T> other)
{
return EqualityComparer<T>.Default.Equals(Value, other.Value);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj is T) return Value.Equals(obj);
if (obj.GetType() != this.GetType()) return false;
return Equals((FooBar<T>) obj);
}
public override int GetHashCode()
{
return EqualityComparer<T>.Default.GetHashCode(Value);
}
[Fact]
public void CanUseInDict()
{
var foobar= new Booh();
IFooBar[] foobars= new IFooBar[]{ foobar.AsIFooBar() };
Dictionary<IFooBar,string> ifoobars= new Dictionary<IFooBar, string>()
{
{ foobar.AsIFooBar(), foobar.GetType().Name}
};
Assert.Equal( foobar.GetHashCode(), new FooBar<Booh>( foobar ).GetHashCode());
Assert.True( foobar.AsIFooBar().Equals( new FooBar<Booh>( foobar ) ) , "Equals FooBar<Booh>");
Assert.True( ifoobars.ContainsKey( new FooBar<Booh>(foobar) ), "ContainsKey");
ifoobars.Remove(foobar.AsIFooBar());
Assert.Empty(ifoobars);
}
static class BoohFooBarExt
{
public static IFooBar AsIFooBar<T>(this T value ) where T:IFoo, IBar => new FooBar<T>(value);
}
|