Well, i hope you understand me. I have two classes, A
and B
. B
is subclass of A
. They have the same public methods and means the same thing, but B does some things a little different, so it has additional methods and attributes that only uses itself. Let say, class A implements a method newFromWizard
that interactively creates an object. Can I implement logic for, depending on the user input, create an object A or and object B in the newFromWizard
method of A. I mean, can i create a B object from that method of A? Or i need to implement that elsewhere? How is the best way to do it? In practice, i can. But, it is correct for OOP?
By the way, if that matters, i'm using Smalltalk.
I would say that it's not intuitive way of doing things. Let's simplify it and say that you just redefine new
. Then in some point you do A new
and get an instance of B
. The thing that they are similar makes it not so bad. But imagine that someone else starts working with your code. And hew knows that message new
should result in creation of the instance of the receiver. And then something goes different. I'd say that conceptually it's wrong. Why now to implements some builder class? And have there something like
createInstanceOfAB
|className|
className := "do what you need".
^ className asClass new.
This is a more clear way.
Once again you can make new…
method to do whatever you want, even shoot fireworks, but most of the people will expect it to create instance of the same class