❔ Adding Polymorphism to a program
I have project for school and I must use Polymorphism but I have already written everything and it works and to add Polymorphism where few classes use 1 base class would require to change a lot. What if I use 1 new "base" class which would get inhereted by my already made class and I would add Polymorphism functionality to it? Would this still count as having Polymorphism?
26 Replies
Yes, as long as the classes inherit at least a method of the base. So your base class method for eg void PrintMessage -> Console.Writeline("I'm base") then the inheriting classes have their own messages or even do something similar derivedClass' PrintMessage ->Console.Writeline("i'm not base, but i'm derived"); .... know what i'm saying?
and can base class be abstract?
yes it can
because I need to add 4 things and they are seem to be tied: polymorphism, abstract class, interface and namespace
maybe I can do all 4 with just 1 abstract class
well abstract class can have both abstract methods and non-abstract methods.
Interface has only the signatures
I thought point of abstract class is that it cant be made directly
they have to be inherited
so I will need interface and abstract class
well if that's what's they want you have no choice ? 🙂
thing is your Abstract class CAN inherit from an Interface if that's what you want to do, ya know, chain them all together... but that sorta defeats the purpose of OOP concepts you see
so you could have for eg something like IAnimal with its concrete class Animal implementing what's been defined in the interface. Something minimal will do since they haven't given you exactly how much to show, right?
yeah
it is what I did last time
I actually wrote whole program in beggining in course
it is pretty big maybe I should have waited
there already was 1 time I had to add stuff like static
lol! so then they decided to then throw all the specifics at you after you've completed it ! hhehhe
I just added static variable to show number of times object was created and it was okay so I think I just need to include concepts of things and not build program around concepts
and task that were given to us isnt very object orientied
it is just worms game so you cant even split worms into classes
thing is.. you might feel it's a lot of work to refactor it.. but then you just have to be patient and zoom in on all that code and see if you can try to pull bits out part by part
if it was some strategy or rpg game then I can make plenty of classes to make it more like concepts
I have map for this game and map has tiles so I think I will make some abstract class like space that tile class can inherit
so as a start, find a method that does quite a bit ... see if that one can be defined in an Interface?
I was thinking to find method that does basicly nothing but writes a line
that would be easy to add interface for
hehe yea that's a start right 🙂
IPrintable
or something
yep, absolutely!
do it do it!! 😄
and for namespaces I think I already use them
because I have each of my class in different file and they all have namespace of project
unless theres more to it
like this
yea as long as you have 2 different classes that are responsible for different things say, you could just give them different namespaces and that will make sure the reliance is there and that you'll be showing you know how to refer to it using that other namespace
and then map classes uses this class
but they arent in same file
they just both have namespace
so yes that's it, besides this one you'll have another namespace that houses presumably other things related to the worms or what they do etc and then you'll at some point have to refer to it so that cross referencing will require you to use that other namespace anyway
well, sounds like you know what you're doing to be honest! 🙂
but if I just use abstract classes
they act as blank class to inherent and then whichever class inherits it must use methods or fields that were in abstract class
isnt that already Polymorphism? I am overriding abstract class methods
yes that's right. That is Polymorphism But from your earlier message, i guess i got the wrong impression, had thought you're asked to show usage of Abstract, Interfaces, Namespaces , all of these. If you need to have only 4 things for any 1 or 2 of these Poly concepts then that's right, Just use what you've planned with abstract classes.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.