0

Microsoft states in regards to abstract classes "They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code."

If I add additional functionality to a base class, wouldn't this break the derived class, for example, if I added another abstract method, all the derived classes would now have to implement this method, or am I misreading the quote.

Here is a link to original article.

Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • I believe that by "functionality" they mean implementing some operation (method), not adding an abstract method – David Conde Apr 06 '11 at 12:02
  • @Nick, I added the link to the original article. – Xaisoft Apr 06 '11 at 12:02
  • @David Conde, that is what I was thinking, but not sure. – Xaisoft Apr 06 '11 at 12:03
  • The real question about the quote to me is, "version well as compared to what?" The only other case where the example would apply is a concrete class that has derivatives and the statement is true in that case as well. I suggest you ignore that particular sentence. Abstract classes are useful when the calling code needs some operations to work a particular way, but doesn't care about other operations. – Daniel T. Apr 06 '11 at 15:38

4 Answers4

0

In 2010 your code

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

public abstract class Human extends Animal{
    public  void eat(){
               //eat rice
    }
}

and your method

feedAnimal(Animal a){
  a.eat();//it will eat rice for code in 2010
}

Now in 2015, you have got new version

public abstract class Human extends Animal{
    public  void eat(){
        //eat rice + milk
    }
}

Now if you invoke feedAnimal(); it will eat rice + milk

I hope it clears up the things well ;)

jmj
  • 237,923
  • 42
  • 401
  • 438
  • I understand that this won't break the derived class, but what If I don't want an animal to eat rice + milk, I then have to implement another abstract method and this would break the derived classes. – Xaisoft Apr 06 '11 at 13:09
  • You can't add method , it will break it up whole things. when you design abstract class you need to make sure that this won't ever ever need any additional method – jmj Apr 06 '11 at 13:12
  • I think, you can add methods to base class which are `virtual` (non-abstract), as given in the example. The answer is specific to c# & not java. – shahkalpesh Apr 09 '11 at 17:45
  • Well , I answered it in terms of pure OOP – jmj Apr 11 '11 at 06:29
0

I'd argue that adding an abstract method to a superclass does not constitute adding "additional functionality" and that they were simply referring to being able to access new methods added to the superclass in new subclasses at a later date.

I agree though. It's pretty misleading.

Nick
  • 6,967
  • 2
  • 34
  • 56
0

There is an idea in OOP that you should only inherit from abstract classes and interfaces. The article seems to tacitly assume this. Thus, the comment about versioning relates to the relative advantages of using an abstract class vs. an interface.

C# and VB encourage excessive use of inheritance since classes are not sealed by default.

See:

Why aren't classes sealed by default?
Why aren't classes sealed by default?
Why does the 'sealed' keyword exist in .Net?
http://oredev.org/2010/sessions/c-s-greatest-mistakes

Community
  • 1
  • 1
Rodrick Chapman
  • 5,437
  • 2
  • 31
  • 32
0

I guess, it means the following

public abstract class myBase
{
    public abstract void sayHello();
}

public class child : myBase
{
    public override void sayHello()
    {
        Console.WriteLine("hello");
    }
}

Let's say, you want to add a new method in derived class(es) that should also be in base class, you could do

public abstract class myBase
{
    public abstract void sayHello();
    public virtual void saySomething()
    {
        Console.WriteLine("default something");
    }
}

public class child : myBase
{
    public override void sayHello()
    {
        Console.WriteLine("hello");
    }
}

Here, the code adds the default implementation in the base class & it won't break any other code. Also, derived classes can provide for overrideen behavior, if it differs from base's saySomething.

With this in mind, inherting class(es) can provide for its own implementation.

public abstract class myBase
{
    public abstract void sayHello();

    // here, if one can throw `NotImplementedException` 
    public virtual void saySomething()
    {
        Console.WriteLine("default something");
    }
}

public class child : myBase
{
    public override void sayHello()
    {
        Console.WriteLine("hello");
    }

    public override void saySomething()
    {
        Console.WriteLine("child said something");
    }
}
shahkalpesh
  • 33,172
  • 3
  • 63
  • 88