Better adjustment to make class inherit different superclass - java

I have a class A that inherit Middle, which inherit OriginBase
public abstract class Base<T>
public class OriginBase<T extends CustomObject> extends Base<T>
public class Middle<T extends CustomObject> extends OriginBase<T>
public class A extends Middle<ObjectA>
public class B extends Middle<ObjectB>
public class C extends Middle<ObjectC>
Due to new feature and refactor of class A, I got a new version of class OriginBase, NewBase, which is similar to OriginBase and only have a few more methods. The problem is that not only class A inherit Middle, so I cannot simply change Middle's superclass to NewBase.
For now, my solution is to create a new class NewMiddle extends NewBase, add corresponding new override methods, and then make class A extends NewMiddle. But this will cause hundreds of lines of code duplication.
To avoid such redundant duplication, I have thought about generics inherit.
The idea is to let class A, B, C specify the Base class they needed, such as:
public abstract class Base<T>
public class OriginBase<T extends CustomObject> extends Base<T>
public class NewBase<T extends CustomObject> extends Base<T>
public abstract class CommonBase<B extends Base>
// I'm not sure is this a good idea, even not sure if it's a right concept...
public class NewMiddle<B extends Base<T extends CustomObject>, T> extends CommonBase<B, T>
// modified class
public class A extends Middle<NewBase<ObjectA>>
// other classes
public class B extends Middle<OriginBase<ObjectB>>
public class C extends Middle<OriginBase<ObjectC>>
How to achieve this kind of inheritance? Is this a good idea to do so?

Related

java - Create a Comparator for a type that contains type parameters

I have a class defined as MyClass<T, S> for which I'd like to create a Comparator for MyClass where T and S extend Foo.
How can I go about doing this?
Unsuccessful attempts:
// Warning: The type parameter MyClass is hiding the type MyClass<T,S>
public class MyComparator<MyClass> implements Comparator<MyClass>
// Syntax errors and the warning from above
public class MyComparator<MyClass<T, S>> implements Comparator<MyClass<T, S>>
// Syntax errors
public class MyComparator<MyClass<T extends Foo, S extends Foo>> implements Comparator<MyClass<T extends Foo, S extends Foo>>
// Syntax errors and the warning from above
public class MyComparator<MyClass<? extends Foo, ? extends Foo>> implements Comparator<MyClass<? extends Foo, ? extends Foo>>
And various combinations of the above. What is the correct way? Thank you.
Your class is not generic. It always compares the same type of objects, ans this type is MyClass<? extends Foo, ? extends Foo>.
So it should be
public class MyComparator implements Comparator<MyClass<? extends Foo, ? extends Foo>>

Java generics: Bound mismatch.Looking for the valid substitute

I have a couple of generic classes:
public interface Data<E> {}
public interface Clonable<E extends Clonable<E>> {}
public interface NaturalNumberInterface extends Data<NaturalNumberInterface> {}
public class NaturalNumber implements NaturalNumberInterface {}
public interface SetInterface<E extends Data<E>> extends Clonable<SetInterface<E>> {}
public class Set<E extends Data<E>> implements SetInterface<E> {}
When I'm trying to create the new instance of Set Set<NaturalNumber> s=new Set<NaturalNumber>(); compiler says:
NaturalNumber is not valid substitute for the type parameter <E extends Data<E>> of the type Set<E>
Maybe you can help me to find the mistake, cause I spent a long time and didn't find the solution.
I assume that your SetInterface is defined in the same way as ListInterface and Data is just interface Data<T>.
The generic argument of SetInterface is F-bounded: E extends Data<E>. In your current code NaturalNumber type extends Data<NaturalNumberInterface>. So if E is NaturalNumber, then condition is violated as it should extend more specific type Data<NaturalNumber>.
You should use F-bounds for NaturalNumberInterface as well:
public interface NaturalNumberInterface<T extends NaturalNumberInterface<T>> extends Data<T>
public class NaturalNumber implements NaturalNumberInterface<NaturalNumber>
This way it will work.

Generics and inheritance with bounds

In following code:
class BaseT {}
class Base<T extends BaseT> {}
class CLass1T extends BaseT {}
class Class1<T extends Class1T> extends Base<T> {}
I would like:
a) class Class1 should inherit class Base and parameter in bounds should be passed to class Base (It is done in this code)
b) Class1 should have parameter Class1T in their code, not T. Something like this:
class Class1<T> extends Base<Class1T> {}
c) Class1 should be opened to inheritance. Example:
class CLass2T extends CLass1T {}
class Class2 extends CLass1<Class2T> {}
How to do this? Is it possible?
This is your solution. If I understand correctly your requirement "open for inheritance".
class Class1<T extends Class1T> extends Base<T> {}

implements and extends a class

I would like to know why in Java I can do this
interface Specification{
double Interest =12.5;
void deposit(double);
}
class Base{
private String Account;
public Base(String acct){Account=acct;}
public void Show(){System.out.print("Account # "+Account);}
}
class Child extends Base implements Specification{
public Child (String acct){super(acct);Show();}
public void deposit(double cash){System.out.print("Now you have "+cash+" Dollars");}
}
but I can't do this
class Child implements Specification extends Base{
public Child (String acct){super(acct);Show();}
public void deposit(double cash){System.out.print("Now you have "+cash+" Dollars");}
}
Is there any specific order in Java or rule when using extends and implements in the same class.
I would like somebody to explain me the reasons please.
Because the Java Language Specification says so. A normal class declaration follows this syntax
NormalClassDeclaration:
ClassModifiers(opt) class Identifier TypeParameters(opt)
Super(opt) Interfaces(opt) ClassBody
where Super is
Super:
extends ClassType
and Interfaces is
Interfaces:
implements InterfaceTypeList
In my opinion, this makes sense. You want to define what something is before defining what it can do.
Yes in java there is specific order first class in extended then you can implement many classes :
class Child extends Base implements Specification1,Specification2.. -> valid
&
class Child implements Specification1,Specification2.. extends Base -> not valid

Extend abstract generic class with abstract class in Java

I have a base abstract class public abstract class BaseModel <T extends BaseModel<T>>. Normally, I extend this class with something like public class OtherModel extends BaseModel<OtherModel>. For some of my classes, I want to have an intermediate abstract class, such that A extends B extends BaseModel.
I'd like to be able to declare public class EndModel extends MiddleModel<EndModel>. The only way I've managed to get Eclipse to be happy is if I declare MiddleModel as public abstract class MiddleModel<T extends BaseModel<T>> extends BaseModel<T>, but this seems ugly, and now I have to add a type anywhere I declare a variable as MiddleModel, even if I don't actually care what type it is. Is there a better way to do this?
If you want to add more functionality in MiddleModel, which depends on the generic type of it, declare it like this:
public abstract class BaseModel <T extends BaseModel<T>> {}
public abstract class MiddleModel <T extends MiddleModel<T>> extends BaseModel<T> {}
public class EndModel extends MiddleModel<EndModel> {}
Note that EndModel doesn't support that extensibility. It's a trade-off, because I don't want to write new EndModel<EndModel>() but rather new EndModel().

Categories