❔ 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
prg_mirae
prg_mirae15mo ago
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?
Richard Evanson
Richard Evanson15mo ago
and can base class be abstract?
prg_mirae
prg_mirae15mo ago
yes it can
Richard Evanson
Richard Evanson15mo ago
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
prg_mirae
prg_mirae15mo ago
well abstract class can have both abstract methods and non-abstract methods. Interface has only the signatures
Richard Evanson
Richard Evanson15mo ago
I thought point of abstract class is that it cant be made directly
prg_mirae
prg_mirae15mo ago
they have to be inherited
Richard Evanson
Richard Evanson15mo ago
so I will need interface and abstract class
prg_mirae
prg_mirae15mo ago
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?
Richard Evanson
Richard Evanson15mo ago
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
prg_mirae
prg_mirae15mo ago
lol! so then they decided to then throw all the specifics at you after you've completed it ! hhehhe
Richard Evanson
Richard Evanson15mo ago
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
prg_mirae
prg_mirae15mo ago
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
Richard Evanson
Richard Evanson15mo ago
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
prg_mirae
prg_mirae15mo ago
so as a start, find a method that does quite a bit ... see if that one can be defined in an Interface?
Richard Evanson
Richard Evanson15mo ago
I was thinking to find method that does basicly nothing but writes a line that would be easy to add interface for
prg_mirae
prg_mirae15mo ago
hehe yea that's a start right 🙂
Richard Evanson
Richard Evanson15mo ago
IPrintable or something
prg_mirae
prg_mirae15mo ago
yep, absolutely! do it do it!! 😄
Richard Evanson
Richard Evanson15mo ago
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
prg_mirae
prg_mirae15mo ago
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
Richard Evanson
Richard Evanson15mo ago
namespace WormsGame
{
public class Tile
{
int type = 0;
Worm currentWorm = null;

public int Type
{
set
{
if(value < 0 || value > 4)
{
Console.WriteLine("Tile value must be between 0 and 4. " +
"0 - empty tile, 1 - terrain tile, 2 and 3 - player is in tile, 4 - arrow");
}
else
{
type = value;
}
}
get { return type; }
}

public Worm CurrentWorm
{
set { currentWorm = value;}
get { return currentWorm; }
}
public Tile() { }

public Tile(int type, Worm currentWorm)
{
Type = type;
CurrentWorm = currentWorm;
}
}
}
namespace WormsGame
{
public class Tile
{
int type = 0;
Worm currentWorm = null;

public int Type
{
set
{
if(value < 0 || value > 4)
{
Console.WriteLine("Tile value must be between 0 and 4. " +
"0 - empty tile, 1 - terrain tile, 2 and 3 - player is in tile, 4 - arrow");
}
else
{
type = value;
}
}
get { return type; }
}

public Worm CurrentWorm
{
set { currentWorm = value;}
get { return currentWorm; }
}
public Tile() { }

public Tile(int type, Worm currentWorm)
{
Type = type;
CurrentWorm = currentWorm;
}
}
}
and then map classes uses this class but they arent in same file they just both have namespace
prg_mirae
prg_mirae15mo ago
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! 🙂
Richard Evanson
Richard Evanson15mo ago
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
prg_mirae
prg_mirae15mo ago
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.
Accord
Accord15mo ago
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.