How to use abstract class in C#

6 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    abstract class MyClass
    {
        public abstract void Print(string s);
    }

    /* Error CS0534	'MySubClass' does not implement inherited abstract member 
       'MyClass.Print(string)'	*/
    class MySubClass : MyClass 
    {
        /*public override void Print(string s)
        {
            Console.WriteLine("public override void Print(string s): {0}", s);
        }*/
    }

    class Program
    {
        static void Main(string[] args)
        {
            MySubClass mysubclass = new MySubClass();
        }
    }
}



/*
run:
    
ERROR!
test.cs(12,11): error CS0534: 'MySubClass' does not implement inherited abstract member 'MyClass.Print(string)'

*/

 



answered Apr 25, 2017 by avibootz
edited May 4, 2024 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    abstract class MyClass
    {
        public abstract void Print(string s);
    }

    class MySubClass : MyClass
    {
        public override void Print(string s)
        {
            Console.WriteLine("public override void Print(string s): {0}", s);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            /* Error CS0144 Cannot create an instance of the abstract class 
               or interface 'MyClass' */

            // MyClass a = new MyClass(); 

            MySubClass mysubclass = new MySubClass();

            mysubclass.Print("C#");
        }
    }
}


/*
run:
     
public override void Print(string s): C#
 
*/

 



answered Apr 25, 2017 by avibootz
edited Jul 4, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    abstract class MyClass
    {
        public abstract void Print(string s);
    }


    class MySubClass : MyClass 
    {
        public override void Print(string s)
        {
            Console.WriteLine("public override void Print(string s): {0}", s);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MySubClass mysubclass = new MySubClass();

            mysubclass.Print("C#");
        }
    }
}


/*
run:
    
public override void Print(string s): C#

*/

 



answered Jul 4, 2017 by avibootz
0 votes
using System;
 
abstract class Area {
    public abstract int GetArea();
}
 
class Square : Area {
    int a;
 
    public Square(int n) => a = n;
 
    public override int GetArea() => a * a;
 
    static void Main()
    {
        var o = new Square(8);
         
        Console.WriteLine(o.GetArea());
    }
}
 
 
 
 
/*
run:
 
64
 
*/

 



answered May 4, 2024 by avibootz
0 votes
using System;
 
abstract class Base  {
    protected int _x = 5;
 
    public abstract void AbstractMethod();   
     
    public abstract int X { get; }
}
 
class Derived : Base
{
    public override void AbstractMethod() {
        _x++;
    }
 
    public override int X {
        get {
            return _x + 3;
        }
    }
 
 
    static void Main()
    {
        var o = new Derived();
         
        o.AbstractMethod();
         
        Console.WriteLine(o.X);
    }
}
 
 
 
 
/*
run:
 
9
 
*/

 



answered May 4, 2024 by avibootz
0 votes
using System;
 
abstract class Base  {
    protected int _x = 5;
    protected int _y = 8;
 
    public abstract void AbstractMethod();   
     
    public abstract int X { get; }
    public abstract int Y { get; }
}
 
class Derived : Base
{
    public override void AbstractMethod() {
        _x++;
    }
 
    public override int X {
        get {
            return _x + 3;
        }
    }
 
 
    static void Main()
    {
        var o = new Derived();
         
        o.AbstractMethod();
         
        Console.WriteLine(o.X);
    }
}
 
 
 
 
/*
run:
 
error CS0534: `Derived' does not implement inherited abstract member `Base.Y.get' 
 
*/

 



answered May 4, 2024 by avibootz

Related questions

1 answer 202 views
1 answer 175 views
1 answer 205 views
1 answer 170 views
1 answer 163 views
163 views asked Apr 1, 2018 by avibootz
2 answers 382 views
382 views asked Jul 4, 2017 by avibootz
1 answer 195 views
195 views asked May 30, 2016 by avibootz
...