Is it based up polymorphy or just inheritance?


Joined
Jan 2, 2014
Messages
13
Reaction score
0
Hi there,
I have an oop happenig but no idea if it based up polymorphy or just inheritance.

There are two classes: ClsAuto and ClsJeep
ClsAuto is the baseclass in this case and ClsJeep derivedclass.
ClsJeep has no more than an construktor, to decide how much gears it shal has.

Within static void Main I chose those methode "Gearup" and finally it shows me ClsJeep has gotten it first gear.
Thats fine, but what is it? Polymorphy? Or just inheritance?

Polymorphy is described as the posibility to give other capabilitys to those derivedclass, (using override)
But obviously this ClsJeep has'nt override any method or property

Inheritance is described as: every property and method are usable within derivedclass as well.
Thats true in this case, but does not explain that effect.

good night albert


Code:
class Program
    {
        [COLOR=Blue]static void Main(string[] args)[/COLOR]
        {
            ClsAuto myCar = new ClsAuto(4);
            ClsJeep myJeep = new ClsJeep(6);

            myCar.GearUp();
            myCar.GearUp();
            myCar.GearUp();
            Console.WriteLine("" + myCar.GearActual);
            Console.ReadLine();

            myJeep.GearUp();
            Console.WriteLine("" + myJeep.GearActual);
            Console.ReadLine(); 
        }
    }

[COLOR=Blue]public class ClsAuto[/COLOR]
    {
        private int mIntGearMax;
        private int mIntGearActual;

        #region "Construktor"
        public ClsAuto() { } //Construktor
        public ClsAuto(int IntGearMax)
        {
            GearMax = IntGearMax;
        }
        #endregion
        

        public void GearUp() //Method
        {
           GearActual++;
        }


        #region "Propertys"  
        public int GearMax //Propertys
        {
            get { return mIntGearMax; }

            set
            {
                if (value > 0)
                {
                    this.mIntGearMax = value;
                }

                else
                {
                    Console.WriteLine ("Gear needs to be higher than 0 ");

                }               
            }
        }

        public int GearActual
        {
            get { return mIntGearActual; }
            set
            {
                if (value < this.mIntGearMax)
                    mIntGearActual = value;
                else
                { Console.WriteLine("No more than" + mIntGearActual); }
            }

        }
        #endregion

    }


[COLOR=Blue]class ClsJeep : ClsAuto[/COLOR]
    {
        public ClsJeep(){ }

        public ClsJeep(int IntGearMax)
            : base(IntGearMax) { }
    }
 
Ad

Advertisements

Joined
Jan 2, 2014
Messages
13
Reaction score
0
perhaps thats helpful After passing static void main string, myCar has get the third gear and myJeep the first gear. Those variable 'mIntGearActual' has start from 0, even myJeep has no more than his construktor. No override, no overload, nor add any new methode. So far I would say it has nothing to do with polymorphi. But I've never read anything about inheritance which might give any explication about this capability. Albert
 
Ad

Advertisements

Joined
Jan 2, 2014
Messages
13
Reaction score
0
Well, thats the answer: If I declare those two objects, each one will have his owen memory area, ClsJeep will have all methodes(guess explication is not need here) .

So, if "Gearup" is called, than of cource there will be a change only in those area(the class wich was used).

I gues what makes people really often confused about polymorphy, . the description really often is no more like: There are derivad-Clases, they are using same methods, but overwritten, and so it offer diffrent reaction.
But polymorphy means:

ClsAuto[] arr = new ClasAuto[3]
arr[0] = new ClasJeep();
arr[1] = new ClsBMW();
arr[2] = new ClsPorsche();
for each (ClsAuto Auto in arr)
{ Auto.SpeedUp(); }

SpeedUp is overwritten in each class, but every Auto will get his owen method, within foreach, even its just called from a reference of the baseclase.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top