-5

I have made two objects, i don't know what is the difference between them also. Is anyone count as a good practice? examples are appreciated! also, mark the advantages of both. Is it that I making a very simple code that's why I'm not seeing the difference?

class Program    
{

    static void Main()
    {
        MYway addison = new Addison(4, 6);    //OBJ 1
        Addison addison2 = new Addison(4, 6); // OBJ 2
        Console.ReadKey();
    }

}

abstract class MYway
{
    abstract protected object Calculate(object value1, object value2);
}

class Addison: MYway
{
    public Addison(double v1,double v2)
    {
        Console.WriteLine(Calculate(v1,v2));
    }

    protected override object Calculate(object value1, object value2)
    {
        return (double)value1 + (double)value2;
    }
}

Output:

10

10

Hassaan Raza
  • 91
  • 1
  • 11
  • Objects are the same structurally. References are different. It depends on you to work with which abstraction level (base/derived class). Given your short example, MyWay is useless unless you put it in bigger picture. – qxg Oct 18 '17 at 05:17
  • You should probably look at the importance of Abstraction in modern programming languages, you will get your answer. It's not about the difference it's about the requirement. – Ipsit Gaur Oct 18 '17 at 05:17
  • qxg i know the code does not make much of sense but if you think of it as a beginner's ruff practice it would look ok :D Another thing i got your point so thanks – Hassaan Raza Oct 18 '17 at 05:25
  • Ipsit Gaur what do means by "modern programming languages"? is c# not modern enough? – Hassaan Raza Oct 18 '17 at 05:26
  • 1
    your code looks like a pretty bad example explaining inheritance in C#. You should look up some tutorials on this topic. – CSharpie Oct 18 '17 at 05:27
  • C# is a modern language, if you know the importance of abstraction then you will clearly know the difference in those two objects. – Ipsit Gaur Oct 18 '17 at 05:28
  • i don't know why people mark my questions as -ve , i means if the wording of question is wrong just suggest an edit i will do so, but no they have to discourage the asker! a read quote some time ago that "the only stupid question is the unasked one" why people don't get it :( – Hassaan Raza Oct 18 '17 at 05:35
  • CSharpie and Ipsit Gaur ok i will research more...thanks. :) – Hassaan Raza Oct 18 '17 at 05:37

2 Answers2

1

Starting from class definition. In my experience it is better to start with concrete implementation and then later (only if needed) extract some abstraction. I'm not sure whose words are these but: "Abstraction should be discovered". So you shouldn't start your design from abstract keyword (in your example I would delete abstract class)

Another thing is that classes should encapsulate state and behavior and in your example you don't have a state (numbers you are passing into constructor are not store anywhere). It means that you would be fine only with static method that calculate

public static class Addison
{
    public static object Calculate(object value1, object value2)
    {
        return (double)value1 + (double)value2;
    }
}

and you can use it:

class Program    
{
    static void Main()
    {
        object addison = Addison.Calculate(4, 6);    //OBJ 1
        object addison2 = Addison.Calculate(4, 6); // OBJ 2
        Console.ReadKey();
    }
}

If you would like to actually encapsulate state and behavior then

public class Addison
{
    private object _value1;
    private object _value2;
    public Addision(object value1, object value2)
    {
         _value1 = value1;
         _value2 = value2;
    }
    public object Calculate()
    {
        return (double)_value1 + (double)_value2;
    }
}

class Program    
{
    static void Main()
    {
        Addison addison = new Addison(4, 6);    //OBJ 1
        Addison addison2 = new Addison(4, 6); // OBJ 2
        Console.WriteLine(addison.Calculate());
        Console.WriteLine(addison2.Calculate());
        Console.ReadKey();
    }
}

In above example you values 4 and 6 are stored in private (not accessible from outside) fields. And Calculate method use them to produce result.

Now if you ask what is a difference between addison and addison2: - this are two different instances of the same class - they occupy two different places in memory (their references are not equal)

Piotr Pasieka
  • 2,063
  • 1
  • 12
  • 14
  • i did not get the difference in the object i created that all, i know its a foolish example (my code) , i was trying to explore abstraction , so i made what came in my mind first its nothing i want to create my mistake i did not specify that in the question – Hassaan Raza Oct 18 '17 at 05:41
  • try Console.WriteLine(addison == addison2); – Piotr Pasieka Oct 18 '17 at 05:44
  • so even if this was a complicated code the output would always be same or it can change ( in any case) – Hassaan Raza Oct 18 '17 at 05:45
  • output would be same if you use the same input parameters to create object. This is like buying 2 cars.. car1 = red BMW and car2 = red BMW... cars look identical but they are two separate things. If you crash one you still have second one – Piotr Pasieka Oct 18 '17 at 05:48
  • ok thanks dude. i will research more to strengthen the concept of abstraction....loved your edit. – Hassaan Raza Oct 18 '17 at 05:50
  • 1
    using `object` inside the abstract method is probably leading to some `if(a is typeof(...) do this else to other thing` with lots of casts that tightly couple your code - and will break on `Addison addison = new Addison("hello", null);` -> geared toward Hassan Raza, not Piotr – Patrick Artner Oct 18 '17 at 05:50
  • @Patrick Artner you are probably right. And Hassan Raza you probably should start with strengthening the concept of object creations, references and values and types in C# and be comfortable with that before your adventure with abstractions – Piotr Pasieka Oct 18 '17 at 05:55
0

I try explaining this giving another example:

public abstract class Animal
{
    public abstract void MakeNoise();
}

public class Dog : Animal
{
    public override void MakeNoise() { Console.WriteLine("Woof"); }
}

public class Cat : Animal
{
    public override void MakeNoise() { Console.WriteLine("Meow"); }
}


static void Main()
{
    Animal a = new Dog();
    Animal b = new Cat();

    a.MakeNoise();
    b.MakeNoise();
}

Both Dog and Cat are derived from Animal and therefore need to implement the abstract method "MakeNoise". When creating either a Dog or a Cat you know they are animals and therefor you can put them into a variable of the type Animal.

Since the method MakeNoise is defined in the Animal type, you have acces to it, even though the implementation of this method has to be in a subclass.

So Lets assume you have a nother method in Dog called Bark

    Animal x = new Dog();
    Dog y = new Dog();

This will work

y.Bark();

this wont

x.Bark();

x and y both contain dogs, however you only know for sure that y is a dog.

CSharpie
  • 9,195
  • 4
  • 44
  • 71