Hi :)
i am new in C# and i am a little confused with the abstruct, virtual and override terms in C#... why they are good for?

i tried to google it but i got more confused...

for example - i am making a chess program and i made a class "piece" and other classes for pawn, knight, bishop etc. that inherit from piece
now, i wanna to made a "LegalSquares" function that show me all the legal squares on the board - ofcourse it will be different between the pieces
what is the right way to do it?

make an abstruct methode "LegalSquares" in piece and then regular "LegalSquares" methode in each piece class? or i confused and need to use virtual?
why not make the methode inside each class without the abstruct thing? why it is good for?

thanks in advance!!!

Hmmm... this is not an easy answer. It would be better to get your self a book about basics of programming, because question of yours is exactly this. It will take some time to understand what are you asking, and some more to make it work (in complete).

We cannot provide you a simple answer, especially not in one sentance. As you have figured it out by your self, there is plenty to learn (you stated: more you read, more you got confused). Sure, this is not an easy task to learn - as said, this are the fundations, or the basics or programming (in any language). Its called Polymorhism - or inheritance (roughtly said).

You can start with this the simplest examples here.

Take a look at this simple example of Animals:

// Assembly: Common Classes
namespace CommonClasses
{
    public interface IAnimal
    {
        string Name { get; }
        string Talk();
    }
}
 
// Assembly: Animals
using System;
using CommonClasses;
 
namespace Animals
{
    public abstract class AnimalBase
    {
        public string Name { get; private set; }
 
        protected AnimalBase(string name) {
            Name = name;
        }
    }
 
    public class Cat : AnimalBase, IAnimal
    {
        public Cat(string name) : base(name) {
        }
 
        public string Talk() {
            return "Meowww!";
        }
    }
 
    public class Dog : AnimalBase, IAnimal
    {
        public Dog(string name) : base(name) {            
        }
 
        public string Talk() {
            return "Arf! Arf!";
        }
    }
}
 
// Assembly: Program
// References and Uses Assemblies: Common Classes, Animals
using System;
using System.Collections.Generic;
using Animals;
using CommonClasses;
 
namespace Program
{
    public class TestAnimals
    {
        // prints the following:
        //   Missy: Meowww!
        //   Mr. Mistoffelees: Meowww!
        //   Lassie: Arf! Arf!
        //
        public static void Main(string[] args)
        {
            var animals = new List<IAnimal>() {
                new Cat("Missy"),
                new Cat("Mr. Mistoffelees"),
                new Dog("Lassie")
            };
 
            foreach (var animal in animals) {
                Console.WriteLine(animal.Name + ": " + animal.Talk());
            }
        }
    }
}

But as said before, to understand all about inheritance, I strongly suggest you to get your self some book (Polymorphism in Object Orientated Programming - OOP). Here, this is brand new great book:
http://www.amazon.com/Beginning-Object-Oriented-Programming-Dan-Clark/dp/1430235306

commented: thank u for the great answer! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.