I have
public abstract class A
{
public abstract A get();
}
and
public abstract class B
{
public abstract void save(A a);
}
And bunch of different classes that extend these two classes.
And they are usually paired together. So if I have classes C, D
Then C would extend A and D would extend B and D's save function would expect C in order for it to work.
And I have controller E
public class E
{
List<B> list;
public void save(A a)
{
// how do I choose correct pair from list?
}
}
In that controller I have a list of classes that extend B.
There is a save function in E that receives extended class A as an argument.
How do I loop through all the elements in the list and figure out which class belongs to which in the pair?
Thanks to anyone for their help, I am trying to learn complex abstract classes and how to relate them.
EDIT: If I use instance of then I will have a huge if statements inside 100 classes
Let's say I have 100 classes that extend class A and 100 that extend class B.
They are paired together as I explained above.
So as a further clarification.
public class D extends B
{
public void save(A a)
{
C c = (C)a.get();
// proceed saving using c
}
}
How do I avoid doing a check for instance of inside all these classes, and know how to pair them? Is there no better way?
EDIT2: Pshemo answer is great, however I have one last question to solve.
In class E I have save function that receives class A
How do I know which class that A is linked to?
If class A is instance of C how would I know to search for instance of D in the list and call that classes' save function?
I am not sure if that is what you need, but it looks like you may want to change B class to something like
abstract class B<T extends A> {
public abstract void save(T a);
}
so now when you extend it you can specify which class should T represent, like
class D extends B<C> {
public void save(C a) {}
}
Related
interface A {
void someMethod();
}
class B implements A {
void someMethod() {
//do something
}
}
class C extends B implements A {
void someMethod() {
super.someMethod();
//do something
}
}
I'm using the above design in one of my codes. It is working fine. My whole purpose here is to use the default implementation of class B and do something extra in class C. Is this the correct way to use the implementation? Is there any better design patter to be looked at?
Because If I define my class C as below, still everything works fine. But this neglects the whole purpose of using implementation (to force class C to implement methods of interface A).
class C extends B implements A {}
Yes, it's perfectly fine to both extend and implement on the same classes.
in fact, if you'll look at HashMap (and many others), that's exactly what it does:
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
It still works fine because when you extend B that comes along with an implementation of someMethod, thus fulfilling the contract.
Please understand that
class C extends B implements A is exactly equal to class C extends B other than the quick documentation purpose.
Try removing the defination of someMethod() from class C and see if your code compiles or not. It will compile. The reason is, the moment you made C extend B, C by default gets all the methods defined in B which includes an impementation of someMethod() as well. Defying the whole purpose of your explicit contract.
If you really want to force C to give a defination of someMethod() then try the following code:
interface A {
void someMethod();
}
abstract class B implements A {
protected void someUtilMethod() {
//do common/default defination
}
}
class C extends B {
void someMethod() {
someUtilMethod();
//do extra something
}
}
I have two child classes B and C which inherit from one parent class A. Is it possible that I can make certain functions of class A accessible in B but not in C in java?
Well i don't know a way to forbid it in the Code. But you could just override and then don'f fill them.
If what you want is to forbid to call destroyEverything() from class C:
public class A {
public void travel() {
System.out.println("Travel from A");
}
public void makePee() {
System.out.println("Call from A");
}
}
public class B extends A {
public void travel() {
super.travel();
System.out.println("Travel from B");
}
public void makePee() {
super.makePee();
System.out.println("Call from B");
}
}
Then, on C:
public class C extends A {
public void travel() {
super.travel();
System.out.println("Travel from C");
}
public void makePee(){
throw new UnsupportedOperationException("Not supported from C");
}
}
BUT, if what you want is to not inherit stuff from A, it is probably a flaw at the design of your class hierarchy and class C should not inherit from A.
Example of design flaw: Class A is Animal, class B is Beaver, and you want your class C Cadillac to inherit stuff from Animal since Animal already has the method travel.
Since maybe you don't want to inherit the method makePee (every animal urinates, but Cadillacs don't), it is better to move Cadillacs (class C) to another class hierarchy or find another class design
As per my thinking it is not possible.
Let's see the one real time example->
A Parent have a two child then both are able to access parent property .it is there no restriction on that you can not use this or you can not use this.
And if you want to do like that then you can implicitly write logic in B class also
I have 6 classes as shown in figure below.
Now, class A has an object of class B as a private variable defined. Also class A methods calls many methods from class B, for example B.method1().
Now, class A_Base1 is which is derived from class A, needs to call methods from the derived class B_Base1; for example B1.method2(). And also methods of class A_Base2 needs to call methods from class B_Base2; for example B2.method3().
Now in class A I define the variable as -
private B bObject
Now in method of A_Base1, I cannot cannot call the methods like bObject.method2() since its a base class object.
I need suggestions on -
Is it possible to call derived class object methods using base class object?
Or do I need to re-design this scenario in some other good way?
Using inheritance like this imo only makes sense if the Bx.methodX() do something that means she same to the different Ax. And in that case, you should name them that way:
public class B {
public void doWhatAMeans() {
method1();
}
public class B1 extends B {
#Override
public void doWhatAMeans() {
method2();
}
public class B2 extends B {
#Override
public void doWhatAMeans() {
method3();
}
and then you only need A to call doWhatAMeans() and the A1 and A2 only need to be injected the appopriate instances of Bx.
On the other hand, if doWhatAMeans does not make sense because the methodX do different things that mean different things to Ax, then you need to rethink your object model, probably the parallel structures A,A1,A2 and B,B1,B2 are wrong then.
you could always cast. suppose your class A provides this method:
protected B getBInstance() {
return bObject;
}
then in A_Base1 you could do something like:
((B_Base1)getBInstance()).method2();
this, however, is a VERY bad design. if your A_Base1 class needs an instance of B_Base1 it should be handed such an instance directly at construction time:
public class A_Base1 extends A {
private B_Base1 b1Object;
public A_Base1(B_Base1 instance) {
super(B_Base1); //works as a B for parent
this.b1Ovject = instance;
}
}
and then you can use that
since A is a parent of A_Base1 (I'm assuming extended) you can make the function call that Accesses B public (or protected) and then A_Base1 or A_Base2 can use the same function A does to call into B.
I'm new to Java but used to OOP programming. Is there a way I can force implementing an abstract class nested into another abstract class like in the code below :
public abstract class A
{
public abstract class B extends C
{
#Override
public abstract void foo();
}
}
I would like B to be implemented in each subclass of A. Is it possible?
Thank you for your help,
If I read your question correctly, where you want each sub-class of A to CONTAIN a subclass of B, there is not a direct way to do this. However, you could do something like the below:
public abstract class A
{
public abstract class B extends C
{
#Override
public abstract void foo();
}
protected abstract B getBInstance();
}
Because this forces sub-class of A to return an instance of B, they must have access to some sub-class of B.
However, you should really ask yourself why A should care about its sub-classes implementing B unless A uses B in some way which would require the above.
Yes, make B an interface, as shown in When an Abstract Class Implements an Interface.
This is not possible. The A class should only provide what behaviour an instance of A has (either through the abstract or implemented methods). It should not state anything about how A is implemented.
It's not even clear how this would be useful either since you could not call the implemented class B so it would need to be named something else meaning it's existence could be ignored.
Given a few classes/interfaces.
public interface A {
public int doSomthing(int x);
}
public class B implements A {
int doSomthing(int x){
//actually do something
};
}
public class C extends B {
//does some specific implementations of what B does
// but does NOT override "int doSomething(int)"
}
How in a code using implementation C (or any subClass of C) may I determine (programatically) that B was the class implementing int doSomething(int).
Or if any of B's subclasses (lets say D which extends C) overrid "int doSomething(int)" how, when working with E (which extends D, yeah ... this is one large family of classes) may I define first parent that implemented "int doSomething(int)" ?
Thank you all in advance :)
You can do that using reflection, i.e. you start at the class the object has and check whether that class defines the method which is identified by the methodname and parameter types. If the class doesn't define that method you get its super class and check this, until you hit Object in which case the method isn't available at all.
For public methods, it's easier since Java has already a built-in method for this:
Class<?> mostSpecificImplementor =
yourObject.getClass().getMethod( "doSomthing", int.class ).getDeclaringClass();
Note that this only works for public methods, otherwise you'd have to search up the class hierarchy yourself (use getDeclaredMethod(...) in this case).