I have a model:
class A {
int a;
int b;
int c;
}
I want to create a new class like:
class B {
int a;
int b;
}
I don't want to rewrite all attributes in class B, these attributes are already written in A. How to use A to create B.
We can add extra features to class with extends but how do we create less specific class? Are there any patterns for this? How should class A or class B be designed to be more polymorphic?
(I want to use class B for inserting into a database, the model class and database table should be same)
There should be an "IS A" relation between parent and derived classes.
I.e. the derived class (B) should also be an instance of it's parent class (A).
If you remove something from B it can't be A anymore.
Therefore it should not be possible.
Not sure whether you are able to change the classes/implementation but there seem to be multiple solutions. First one would be to have class A extend class B
public class B {
int a;
int b;
}
public class A extends B {
int c;
}
but that is not answering your core question (creating less specif class).
Creating a less specific class (or at least something that mimics it) could be done via interfaces and getter/setters, so for example:
public class Implementation implements A,B {
private int a;
private int b;
private int c;
public int getA(){return a;}
public int getB(){return b;}
public int getC(){return c;}
...
}
public interface A {
int getA();
int getB();
int getC();
}
public interface B {
int getA();
int getB();
}
I guess you need a design technique like composition.
Explanation : If you want use class B in class A, you can use B b = new B(); in your class A ! this is composition.
also you can implement interfaces instead of class which provides you multiple inheritance.
here in syntax :
class A implements interface1, interface2
Related
I have class A , class B and class C.
In class C,i am declaring the variable like:
private A a;
private B b;
private ? thirdVariable;
I want to declare the third variable whose data type can be of A or B.
So that i can assign this variable either the value of a or b as per my wish.
You can't do that if there is no relation between A & B. If you want to do that then you should Interface/Class that will be extended by A & B.
class X{}
class A extends X{
}
class B extends X{
}
private X thirdVariable;
If you really want to use Generic then Make your use a generic constructor in class C and then pass the object of A/B according to the generic like below.
class C<T> {
private T thirdVariable;
C(T t) {
thirdVariable = t;
}
}
When you create the object of C then you can specify and pass the type of object like below.
A a = new A();
C<A> c1 = new C<A>(a);
B b = new B();
C<B> c2 = new C<B>(b);
You may use Polymorphism with an (abstract) class or an Interface
Interface :
public Interface Parent{ }
public class A implements Parent { }
public class B implements Parent { }
public class C {
private A a;
private B b;
private Parent thirdVariable;
}
(abstract) class : the abstract is optionnal and depend of the design and the functionnalities that you have to make
public (abstract) class Parent{ }
public class A extends Parent { }
public class B extends Parent { }
public class C {
private A a;
private B b;
private Parent thirdVariable;
}
You could create a (possibly abstract) superclass (let's call it class X) and make both A and B extend from it. Then, your third variable can be declared as:
private X thirdVariable;
Similarly, you could create an interface (let's call it interface Y) and make both A and B implement it. Then, your third variable can be declared as:
private Y thirdVariable;
So basically you want to put polymorphism to good use, because generics are not flexible enough to express a type relationship like the one you want.
Time-appropriate greetings :)
Working in Java, I have an Interface A. All implementors of this Interface also extend class B, but B does not implement A. In a class where we use an instance of A (referenced as A), it is cast to a B Reference so that we can use a Method defined in class B. It makes sense conceptually that the Method should belong in Interface A too.
Can you think of a reason not to introduce the Method to Interface A, so that we don't have to cast it to B? Should I maybe override the Method in the subclasses and just call the super version, so that it's easier to navigate in the IDE etc?
In a class where we use an instance of A (referenced as A), it is cast to a B Reference so that we can use a Method defined in class B.
So I'm assuming you have this scenario
public void doStuff(A aType){
...
B bType = (B) aType;
...
}
If this is true, can this work?
private <T extends B & A> void example(T type){
type.aStuff();
type.doBStuff();
}
I created the following to test this.
public class Foo{
private static interface A{
void aStuff();
}
private static class B{
public void doBStuff(){
System.out.println("B stuff");
}
}
private static class AB extends B implements A{
public void aStuff(){
System.out.println("A stuff");
}
}
public static void main(String[] args) {
Foo foo = new Foo();
foo.example(new AB());
}
// method "example" already given
}
Gave me
A stuff
B stuff
Why not creating an abstract class which extends B and implements A? Assuming this class would be called C, your other classes would extend C and implement the method required by A, but will provide you with the methods available in B without casting.
I think that moving methods now would not be a good idea, maybe, at most, have B implement A (assuming you have no other classes which you haven't talked about which are dependent on the classes and interfaces you mentioned).
I have a number of classes, please allow me to introduce them and then ask my question at the end:
I have a container class which contains two objects in a composite relationship:
public class Container{
A a;
B b;
public someMethod(){
a.getC().myMethod(b);
}
}
A and B are superclasses (or Interfaces), with subtypes that can also be the type held in the composite relationship.
A contains a member of (interface) type C:
public class A{
C c;
}
public interface C{
public void myMethod(B b);
}
public class D implements C{
public void myMethod(B b){
//This code will modify the state of object b, in class Container.
b.changeState();
}
}
public class E implements C{
public void myMethod(B b){
//This code will modify the state of object b, in class Container.
b.changeState();
}
}
My problem is that I wish to modify the state of object b from a method starting in the container class, which eventually calls code down the hierarchy, to classes D and E- calling myMethod() via dynamic binding. I want to do this because I am going to use polymorphism to run the correct myMethod() (depending on whether the type of object is D or E) and I wish to do this, rather than write IF statements.
So my problem is that it seems very bad continually passing the instance of object b down the class hierarchy to myMethod, so that I can run b-specific code to modify the state of b. Is there anything else I can do to modify b from d and e (collectively known as c)?
I can get this to work using just interfaces but without using generics- but when I added generics i had problems with types and that made me start to think if my whole design was flawed?
EDIT: I could probably do this easily just by using IF statements- but I wanted an elegant solution using polymorphism of classes D and E.
First of all, if I understood your question correctly, no instance of B is being "passed down" in your code. Dynamic dispatch will simply cause the myMethod() implementation in the actual type of a to be called with an instance of B as argument.
While it may be tedious to have to write the argument explicitly every time you implement myMethod(), there's nothing wrong with it.
The alternative is to give each subclass/implementation of A an attribute of type B. In this case, however, you would have to pass your B instance down the chain of constructors to the class that actually has your B attribute.
Your code would become:
public class A{
C c;
public A(C c) {
this.c = c;
}
public interface C{
public void myMethod(B b);
}
public abstract class CC {
protected B b;
public CC(B b) {
this.b = b;
public class D extends CC implements C {
public D(B b) {
super(b);
}
public void myMethod(){
b.changeState();
}
}
public class E extends CC implements C {
public E(B b) {
super(b);
}
public void myMethod(){
b.changeState();
}
}
And then somewhere, e.g. in Container's constructor:
b = new B();
a = new A(new E(b));
You could pass the instance of B to the constructor of E. (or use a setter). That poses issues in itself, but at least it avoids having to pass B down every time you call myMethod(), which now needs no arguments.
e.g.
somewhere inside B
E myE = new E(this);
and, inside E
final B myB;
public E(B myHigherLevelThing) {
this.myB = myHigherLevelThing;
}
public void myMethod() {
myB.changeState();
}
Use the most general interface for the declarations, I'm a little confused about your full hierarchy so there may be room for improvement there...
Let's say I have 2 classes A.java and B.java and there is private int a in A class like that :
public class A {
private int a;
}
and I use this class in class B that I want to know or attach an handler to the field int a;
that to know it's value change in every asynchronous call. Let me more explain :
public class B {
private A aClass;
public static void main (String ... args) {
aClass = new A(); // now the int a; is changed how do I know this
// user may call many asynchronous method in class A and I want to know
// the changing value of int a; from A class in B class
}
}
Which design pattern should I use? What solution do you offer?
Thanks in advance,
hilal
Observer pattern or here
B registers itself as the observer of A. A is the subject and B is the observer .
Whenever the "a" changes, A notify()'s all the registered Observer's.
public class A {
private int a;
private B observer;
void setA(int i) {
a = i;
observer.notify();
}
void registerObserver(B b) {
observer = b;
}
}
Add a B object in A, and recall B's method.
You could turn class A into a JavaBean and add support for PropertyListeners.
However, you first have to register one with your A() instance.
So I have the following classes:
class A{
public A(int n1){
n=n1;
}
int n;
}
class B extends A{
public B(int n2){
super(n2);
cnt=1;
}
int cnt;
}
class C extends B{
public C(int n3){
super(n3);
clr="red";
}
String clr;
}
public class Driver {
public static void main(String[] args){
A a,b,c,d,e;
a=new B(200); d=a.copy();
b=new C(100); e=b.copy();
}
}
I am asked to define the method copy() in classes A,B,C. The copy method essentially makes a copy of all nested objects.
I have 2 questions:
I don't see any nested objects being constructed, why does he ask me to make a copy of all nested objects? Is it because when I construct a subclass object, a base class object is constructed and nests inside the subclass object?
Is it correct to write the method as follows (take class B for example):
class B extends A{
public B(int n2){
super(n2);
cnt=1;
}
int cnt;
public A copy(){
A copy_ref=new B(1);
((B)copy_ref).cnt=this.cnt;
copy_ref.n=super.n;
return copy_ref;
}
}
I think you are confusing to different concepts.
You are confusing the has-a relationship with the is-a relationship.
In your code C is a B and also an A: C has an is-a relationship with B and A.
C does not contain an instance of B or A (that would be an has-a relationship).
Since C is an B and A, it contains all the members of B and A. Calling a copy of C will copy all of its members variables. You do not need to create any particular method, you can just use the already defined Object.clone method.
If you want to define your own clone/copy method I suggest you look at the following article on the subject.
Enjoy !