-3

I have abstract class and normal class. The normal class inherited from abstract class.

see below

public abstract class ABParent
{
    public void Add()
    {

    }
}

public class Derived : ABParent
{
    public void Show()
    {

    }
}

Here abstract class can't be instantiated. In other words, we can't create object for abstract class.

See below code

ABParent objABParent = new Derived();

From above code, objABParent is act like instance of abstract class and we can only access all elements in abstract class by using objABParent.

So my doubt is how it work as instance of abstract class?

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • 1
    Even though `objABParent` is declared as a `ABParent` it is still an instance of `Derived`. Just by casting like that it doesn't change the type of the instance. It just changes how you can interact with the object. – Enigmativity Nov 26 '19 at 08:01
  • 2
    First of all syntax is incorrect, second `AbstractParent parent = new Child();` will work. _"From above code, objABParent is act like instance of abstract class and we can only access all elements in abstract class by using objABParent."_ Is that a statement or question? If statement, then false, `objABParent` is instance of `Derived` type. – SᴇM Nov 26 '19 at 08:01
  • In addition to @SᴇM's comment, you need to learn what really _polymorphism_ is in regardless of any OOP language.. – Soner from The Ottoman Empire Nov 26 '19 at 08:03
  • OP, consider the same question but substituting "abstract [base] class" with "interface". – ProgrammingLlama Nov 26 '19 at 08:04
  • 2
    I think you should start here [abstract (C# Reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract) then this [Casting and type conversions (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions) then this [Polymorphism (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/polymorphism) – TheGeneral Nov 26 '19 at 08:09
  • Being able to use the derived class where only the base abstract class is needed is pretty much _the whole point_ of having an abstract class (likewise an interface). See marked duplicates. See also https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle for important related discussion. – Peter Duniho Nov 26 '19 at 09:01

2 Answers2

0

Suppose you want to have a collection of objects that are not all Derived, but you just want Add() stuff in them, the way it is done in ABParent. now what you do is add them to a list of abstracts and then you can just foreach all of them:

List<ABParent> classesThatAdd()
        {
            new DerivedA(),
            new Derivedb(), 
            new Derived(), 
            new Derived()
        };

foreach (var item in classesThatAdd)
{
    item.Add(new object());
}

Not all derived classes (DerivedA, DerivedB) need to have Show(), but they all have Add()

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Stucky
  • 156
  • 7
  • if you are into games an easier example might be a base abstract class- `Character`, that all characters derive from- a Warror, rogue, mage. they all have different stat values and different skills (maybe ) but they but they all `Fight(Character enemy)` the same way- they apply their `this.AtkSkill * this.Dmg` to `enemy.Health` – Stucky Nov 26 '19 at 08:12
0

Using the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes, not instantiated on its own. Members marked as abstract must be implemented by non-abstract classes that derive from the abstract class. The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.

https://learn.microsoft.com/ru-ru/dotnet/csharp/language-reference/keywords/abstract

https://learn.microsoft.com/ru-ru/dotnet/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members

If you want to create class member that must be implemented in a derived class, you can do something like this:

public abstract class ABParent
{
    public abstract void Add();
}

public class Derived : ABParent
{
    public void Add(){ }
    public void Show(){ }
}

In your case you can call method of base class, becuase this method was implemented in a base class. You can use virtual method. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method.

GDI89
  • 54
  • 3