I have made a class to include a custom title bar with my apps logo on it. This works well except that for the majority of my classes I need to be able to inherit that functionality as well as that of say a ListActivity. What to do?
Any help appreciated.
you should favor composition (and delegation) over inheritance :
public interface FirstClassInterface {
void method1();
}
public interface SecondClassInterface {
void method2();
}
public class FirstClass implements FirstClassInterface {
// ...
}
public class SecondClass implements SecondClassInterface {
// ...
}
public class FirstAndSecondClass implements FirstClassInterface , SecondClassInterface
{
private FirstClassInterface firstclass;
private SecondClassInterface secondclass;
public FirstAndSecondClass(FirstClassInterface firstclassinterface, SecondClassInterface secondclassinterface) {
this.firstclass= firstclassinterface;
this.secondclass= secondclassinterface;
}
public void method1() {
this.firstclass.method1();
}
public void method2() {
this.secondclass.method2();
}
public static void main(String[] args) {
FirstAndSecondClass t = new FirstAndSecondClass(new FirstClass(), new SecondClass());
t.method1();
t.method2();
}
}
In Java, you cannot have:
class MyClass extends ClassA, ClassB { ... }
Depending on what you are doing, it might be possible to use:
class ClassB extends ClassA { ... }
class MyClass extends ClassB { ... }
Related
I'm trying to do something along the lines:
abstract class Base {}
public interface One {...}
public interface Two {...}
public class A extends Base implements One {...}
public class B extends Base implements One, Two {...}
public class C extends Base implements Two {...}
public class Container
{
class Handler
{
public void doSomething(A obj){System.out.println("A");}
public void doSomething(B obj){System.out.printLn("B");}
public void doSomething(C obj){System.out.println("C");}
}
Base base;
Handler handler;
public Container(Base base)
{
this.base = base;
this.handler=new Handler();
}
public void set(Base base)
{
this.base=base;
}
public void go()
{
this.handler.doSomething(this.base);
}
}
Container con=new Container(new A());
con.go();
con.set(new B());
con.go();
Where the output would end up being "A" "B", but I'm running into problems dynamically casting Container.base to the appropriate class;
The closest solution I have found is in the Container.go function add in an if else chain checking the instanceOf the class and casting the parameter to the corresponding class then calling handler.doSomething in each if block.
public void go()
{
if(this.base instanceOf A)
{
this.handler.doSomething((A)this.base);
}
else if(this.base instanceOf B)
....
}
Is there a better way to go about this?
Which method is called is determined at compile time and not at run time so dynamic casting isn't going to work without some reflection or other tinkering about. I would suggest a better approach is to move the logic for
public void doSomething(A obj){System.out.println("A");}
public void doSomething(B obj){System.out.printLn("B");}
public void doSomething(C obj){System.out.println("C");}
Into the the specific classes. For example:
abstract class Base {
absract public void doSomething();
}
public class A extends Base implements One {
public void doSomething() {System.out.printLn("A");}
}
...
class Handler {
public void doSomething(Base obj){obj.doSomething();}
}
Now your handler doesn't need to care about the specific class of a Base object it is getting.
You could use the visitor pattern:
public interface Visitor {
public void doSomething(A obj);
public void doSomething(B obj);
public void doSomething(C obj);
}
Declare an abstract method in Base (or in a new interface):
abstract class Base {
public abstract void accept(Visitor v);
}
and implement it in A, B, C:
public class A extends Base implements One {
#Override
public void accept(Visitor v) {
v.doSomething(this);
}
...
}
public class B extends Base implements One, Two {
#Override
public void accept(Visitor v) {
v.doSomething(this);
}
...
}
public class C extends Base implements Two {
#Override
public void accept(Visitor v) {
v.doSomething(this);
}
...
}
Handler implements Visitor:
class Handler implements Visitor {
#Override
public void doSomething(A obj){
System.out.println("A");
}
#Override
public void doSomething(B obj){
System.out.printLn("B");
}
#Override
public void doSomething(C obj){
System.out.println("C");
}
}
And finally, go becomes:
public void go() {
this.base.visit(this.handler);
}
UPDATE
Note that #Evan Jones' solution is simpler and it could be what you need. The visitor pattern is used when you want to separate the implementations of the doSomething methods from the A, B, C classes and/or you want the ability to add new operations without changing these classes.
I would like to ask for your help regarding the following problem.
There is an interface which has more implementations:
public interface MyInterface {
void method();
}
public class MyInterfaceA implements MyInterface
public class MyInterfaceB implements MyInterface
and there is a class which uses these implementations of the interface in its different methods:
public class MyClass {
public void firstMethod() {
new MyInterfaceA().method();
}
public void secondMethod() {
new MyInterfaceB().method();
}
}
My problem is that I wouldn't like to create new specific instances in the methods of the class, so somehow I would like to hide which implementation is used.
Do you know a nice solution for this? Is there any design pattern what I can use? How can I hide the concrete implementations or I cannot?
Thanks for your answers in advance!
For example, you can receive the implementations in the constructor of MyClass and further use it. It helps you to hide the specific implementations.
public class MyClass {
private MyInterface one;
private MyInterface two;
private MyClass(MyInterface one, MyInterface two) {
this.one = one;
this.two = two;
}
public void firstMethod() {
one.method();
}
public void secondMethod() {
two.method();
}
}
You can apply the Factory pattern:
class Factory {
public MyInterface a() { return new MyInterfaceA(); }
public MyInterface b() { return new MyInterfaceB(); }
}
public class MyClass {
private Factory factory;
public MyClass(Factory factory) { this.factory = factory; }
public void firstMethod() {
factory.a().method();
}
public void secondMethod() {
factory.b().method();
}
}
Inside the Factory class you can cache the objects created if necessary.
Let me describe my intention with an example,
class Base {
public void sayHi() {
System.out.println("Hi");
}
}
class ChildOne extends Base {
public void sayBye() {
System.out.println("Bye-1");
}
}
class ChildTwo extends Base {
public void sayBye() {
System.out.println("Bye-2");
}
}
public class MainClass {
public static <T extends Base> void genericFunction(T child) {
child.sayBye();
}
public static void main(String[] args) {
ChildOne childOne = new ChildOne();
ChildTwo childTwo = new ChildTwo();
genericFunction(childOne);
genericFunction(childTwo);
}
}
This code is wrong. But my intention is the child class (some already existing classes in a project) which has similar properties between them are they extend the base class and have sayBye() member function.
Is there a way to pass all the child classes as parameter to a generic function and to call the member function sayBye() (as per this example)?
Sure, you can either create a common base class or common interface for all the classes that have a sayBye method.
Then you can change the type bound of your generic type parameter:
public static <T extends CommonInterface> void genericFunction(T child) {
child.sayBye();
}
or without generics:
public static void nonGenericFunction(CommonInterface child) {
child.sayBye();
}
You can bring common functionality together without adjusting the hierarchy by introducing an interface that defines it and creating empty classes extending the common objects.
class Base {
public void sayHi() {
System.out.println("Hi");
}
}
class ChildOne extends Base {
public void sayBye() {
System.out.println("Bye-1");
}
}
class ChildTwo extends Base {
public void sayBye() {
System.out.println("Bye-2");
}
}
// The common functionality I want to use.
interface Bye {
public void sayBye();
}
class ChildOneBye extends ChildOne implements Bye {
// Don't need anything here.
}
class ChildTwoBye extends ChildTwo implements Bye {
// Don't need anything here.
}
public static <T extends Bye> void genericFunction(T child) {
child.sayBye();
}
public void test(String[] args) {
Bye childOne = new ChildOneBye();
Bye childTwo = new ChildTwoBye();
genericFunction(childOne);
genericFunction(childTwo);
}
One way is to mark the base class as abstract and have an abstract method sayBye(). In this way you don't need to change anything else in your codebase.
abstract class Base {
public void sayHi() {
System.out.println("Hi");
}
public abstract void sayBye();
}
Another approach is to use interface ByeInterface and use it to call sayBye(). Here is the with the required changes.
interface ByeInterface {
void sayBye();
}
class Base {
public void sayHi() {
System.out.println("Hi");
}
}
class ChildOne extends Base implements ByeInterface {
public void sayBye() {
System.out.println("Bye-1");
}
}
class ChildTwo extends Base implements ByeInterface {
public void sayBye() {
System.out.println("Bye-2");
}
}
public class MainClass {
public static <T extends ByeInterface> void genericFunction(T child) {
child.sayBye();
}
public static void main(String[] args) {
ChildOne childOne = new ChildOne();
ChildTwo childTwo = new ChildTwo();
genericFunction(childOne);
genericFunction(childTwo);
}
}
This approach can be used if you can not mark your base class as abstract class. Using interface, you can even call it without using generic.
public static void nonGenericFunction(ByeInterface child) {
child.sayBye();
}
I have two abstract generic classes. They cooperate and hence depend on each other. Occasionally one needs to pass this to the other. I am trying to find a type safe way to do this:
public abstract class AbstractA<T extends AbstractB<? extends AbstractA<T>>> {
protected void foo() {
T aB = createB();
aB.setA(this);
}
/** factory method */
abstract public T createB();
}
public abstract class AbstractB<T extends AbstractA<? extends AbstractB<T>>> {
private T theA;
#SuppressWarnings("unchecked")
public void setA(AbstractA<? extends AbstractB<?>> theA) { // dreamed of parameter list (T theA)
// Unchecked cast from AbstractA<capture#1-of ? extends AbstractB<?>> to T
this.theA = (T) theA;
}
protected T getA() {
return theA;
}
}
My question is whether I can find a cleaner way so I avoid the unchecked cast in AbstractB.setA(). I had hoped to declare it setA(T theA), but then the call to it won’t compile: The method setA(capture#1-of ? extends AbstractA<T>) in the type AbstractB<capture#1-of ? extends AbstractA<T>> is not applicable for the arguments (AbstractA<T>). I am still struggling to understand whether the compiler should know enough to allow it or not.
I was thinking my problem may be related to the one discussed in Java generics compilation error - The method method(Class<capture#1-of ? extends Interface>) in the type <type> is not applicable for the arguments. My unchecked cast was inspired from there. I liked the reply by Tom Hawtin - tackling, but I have not found a way to apply it to my situation.
My user will declare concrete subclasses and instantiate one ConcreteA and any number of ConcreteBs:
public class ConcreteA extends AbstractA<ConcreteB> {
#Override
public ConcreteB createB() {
return new ConcreteB();
}
public void concreteAMethod() {
// ...
}
}
public class ConcreteB extends AbstractB<ConcreteA> {
public void bar() {
ConcreteA a = getA();
a.concreteAMethod();
}
}
(class AbstractA<T extends AbstractB<? extends AbstractA<T>>> looks a bit complicated; I thought I needed it for concrete subclasses to know each other’s exact types, but apparently it doesn’t give me that.)
If I've understood you correctly, this should create the binding you want.
class Demo {
public static void main(String[] args) {
ConcreteA a = new ConcreteA();
ConcreteB b = new ConcreteB();
a.foo(b);
b = (ConcreteB) a.getB();
}
}
abstract class AbstractA<T extends AbstractB<?>>{
private AbstractB<?> b;
public AbstractB<?> getB(){
return b;
}
void foo(AbstractB<?> aB) {
b = aB;
aB.bar(this);
}
}
abstract class AbstractB<T extends AbstractA<?>> {
private AbstractA<?> a;
public AbstractA<?> getA(){
return a;
}
public void bar(AbstractA<?> theA) {
a = theA;
theA.foo(this);
}
}
class ConcreteA extends AbstractA<ConcreteB>{
}
class ConcreteB extends AbstractB<ConcreteA>{
}
I think this is what you ended up at yourself. I am not able to remove the cast to ConcreteB, getB() simply cannot be sure of the type it is holding. I now see why you had multiple generic statements in your declaration. :)
If you're up for it, continue searching, and post your own answer if you find one, I'd love to see it.
I hope solving half your problem counts for anything. ;)
I think I got it now why I cannot declare public void setA(T theA) in AbstractB and then call it as aB.setA(this) in foo(). Suppose we had:
class IntermediateConcreteA extends AbstractA<ConcreteB> {
#Override
public ConcreteB createB() {
return new ConcreteB();
}
}
class SubConcreteA1 extends IntermediateConcreteA {}
class SubConcreteA2 extends IntermediateConcreteA {}
class ConcreteB extends AbstractB<SubConcreteA2> {}
Now if I have a SubConcreteA1 and call its foo(), then createB() will return an object that can pass as an AbstractB<SubConcreteA2> but cannot pass as an AbstractB<SubConcreteA1>. Therefore its setA() shouldn’t accept this as an argument. The compiler error message is logical after all.
Each abstract class would be parameterized with two type parameters, one for the actual concrete class of A, and one for the actual concrete class of B:
public abstract class AbstractA<A extends AbstractA<A,B>, B extends AbstractB<A,B>> {
protected void foo() {
B aB = createB();
aB.setA(getThis());
}
abstract public A getThis();
abstract public B createB();
}
public abstract class AbstractB<A extends AbstractA<A,B>, B extends AbstractB<A,B>> {
private A theA;
public void setA(A theA) {
this.theA = theA;
}
protected A getA() {
return theA;
}
}
public class ConcreteA extends AbstractA<ConcreteA, ConcreteB> {
#Override
public ConcreteA getThis() {
return this;
}
#Override
public ConcreteB createB() {
return new ConcreteB();
}
public void concreteAMethod() {
// ...
}
}
public class ConcreteB extends AbstractB<ConcreteA, ConcreteB> {
public void bar() {
ConcreteA a = getA();
a.concreteAMethod();
}
}
A factory can solve it:
public abstract class AbstractA {
public void abstractAMethod() {
// ...
}
}
public abstract class AbstractB<A> {
private A theA;
public void setA(A theA) {
this.theA = theA;
}
protected A getA() {
return theA;
}
}
public abstract class AbstractFactory<A extends AbstractA, B extends AbstractB<A>> {
private A theA = createA();
public A getA() {
return theA ;
}
public B getNextB() {
B newB = createB();
newB.setA(theA);
return newB;
}
protected abstract A createA();
protected abstract B createB();
}
Now the user can go:
public class ConcreteA extends AbstractA {
public void concreteAMethod() {
// ...
}
}
public class ConcreteB extends AbstractB<ConcreteA> {
public void bar() {
ConcreteA a = getA();
a.abstractAMethod();
a.concreteAMethod();
}
}
public class ConcreteFactory extends AbstractFactory<ConcreteA, ConcreteB> {
#Override
protected ConcreteA createA() {
return new ConcreteA();
}
#Override
protected ConcreteB createB() {
return new ConcreteB();
}
}
I don’t think it’s a typical application of the abstract factory pattern, though …
#Chris Wohlert, I did give up in my production code since I considered the factory overkill, but I could not let go of the theoretical question.
I have come to realize that my problem really came out of stuffing two concepts into the AbstractA/ConcreteA hierarchy that didn’t belong together. Though maybe not interesting to very many, I am posting this insight for two reasons: (1) I feel I owe Chris Wohlert the answer I have found myself (2) more importantly, I’d love to inspire anyone else facing a similar tricky generics issue to review your design from a higher level than just solving the generics and/or class cast issue. It certainly helped me. The cast/generics problem was a sign that something more fundamental was not quite right.
public abstract class AbstractA {
public void foo() {
AbstractB aB = createB();
aB.setA(this);
}
/** factory method */
abstract public AbstractB createB();
}
public abstract class AbstractB {
private AbstractA theA;
public void setA(AbstractA theA) {
this.theA = theA;
}
// methods that use theA
}
No generics and no class cast. Taking out the stuff that didn’t belong in the A class hierarchy into ConcreteC (with no AbstractC):
public class Client {
public void putTheActTogether() {
ConcreteC theC = new ConcreteC();
// the concrete A
AbstractA theA = new AbstractA() {
#Override
public AbstractB createB() {
return new ConcreteB(theC);
}
};
// call methods in theA
}
}
public class ConcreteB extends AbstractB {
private final ConcreteC c;
public ConcreteB(ConcreteC c) {
super();
this.c = c;
}
public void bar() {
c.concreteCMethod();
}
}
public class ConcreteC {
public void concreteCMethod() { // was concreteAMethod(); moved and renamed
// ...
}
}
The client needs a few more lines than before. In my real-world code I needed to duplicate one final field in AbstractA and ConcreteC, but it made sense to do. All in all I consider it a low price for a design that is otherwise pure and simple.
Suppose I have a class A:
public class A {
public A(){....}
public void method1() {...}
};
And an instance of that class:
A anA = new A();
Is there any way to override the method1() only for anA?
This question arises when I write a small painting program in which I have to extend the JPanel class several times just to make minor changes to the different panels that have slightly different characteristics.
You can do the following:
A anA = new A() {
public void method1() {
...
}
};
This is the same as:
private static class myA extends A {
public void method1() {
...
}
}
A anA = new myA();
Only with the exception that in this case myA can be reused. That's not possible with anonymous classes.
You can create a an new anonymous class on the fly, as long as you are using the no-arg constructor of your class A:
A anA = new A() {
#Override
public void method1() {
...
}
};
Note that what you want to do is very close to what is known as a lambda, which should come along the next release 8 of Java SE.
I like to do this kind of thing with a delegate, or "strategy pattern".
public interface ADelegate {
public void method1();
}
public class A {
public A(){....}
public ADelegate delegate;
public final void method1() { delegate.method1(); }
};
A anA = new A();
anA.delegate = new ADelegate() {
public void method1() { ... }
};