Monday, August 11, 2014

Understanding abstract class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps
{

    abstract class Parent
    {
        public int i { getset; }
        public abstract void Print();
        public void nonabstract()
        {
            Console.WriteLine("Non Abstract Method Call");
        }
    } 
    class Child : Parent
    {
        public Child(int i)
        {
            this.i = i;
        }
        public override void Print()
        {
            Console.WriteLine(i);
        }
    }
    class Child2Parent
    {
        public Child2(int i)
        {
            this.i = i;
        }
        public override void Print()
        {
            Console.WriteLine(i);
        }
    }
    class UnderstandingAbstractClass
    {
        public static void Main()
        {
            Parent pj = new Child(10);
            pj.Print();
            Parent pj1 = new Child2(10);
            pj1.Print();
            Parent nonAbs = new Child(10);
            nonAbs.nonabstract();

        }
    }
}



O/P:

10
10
Non Abstract Method Call

1 comment:

  1. Abstract classes.

    1) Abstract class can have only non -abstract methods or both.
    2) Abstract class cannot be instantiated
    3) Abstract method is a signature that donot have any method body
    4) Abstract class constructor is can only be called by the derived class object.Since Abstract class constructor can only be called by derived class, constructor access modifier as Public does not make sense, it should be Protected.


    Difference between abstract and interface:

    Abstact class can consits of abstract methods and non-abstract methods(implementations). But in

    inerface we cannot have the implementations only the method signatures.

    Real time use of abstract class?


    Reference: http://www.codeproject.com/Tips/888455/When-to-Use-Abstract-Class-in-Csharp-Real-World-Ex

    ReplyDelete