Advertisement
abstract public void add();
Advertisement
//C# Example of an abstract class
//With two abstract methods
using System;
abstract class A
{
//An abstract method Add()
//It ends with a semicolon;
public abstract int Add(int a, int b);
//An abstract Subtract() method
//It ends with a semicolon; as well
public abstract int Subtract(int a, int b);
}
//C# Example of an abstract class
//With a mix of abstract, ending with a semicolon;
//And, a regular(non-abstract) method, with its implementation provided between { }
using System;
abstract class A
{
//An abstract method Add()
//It ends with a semicolon;
public abstract int Add(int a, int b);
//A regular(non-abstract) Subtract() method of abstract class
int Subtract(int a, int b)
{
return (a-b);
}
}
//C# Example of an abstract class being inherited by a concrete class
//The concrete class will provide implementation of the abstract methods of the inherited abstract class.
using System;
//An abstract class
abstract class A
{
//An abstract method Add()
//It ends with a semicolon;
public abstract int Add(int a, int b);
//A non-abstract Subtract() method of abstract class
int Subtract(int a, int b)
{
return (a-b);
}
}
//A concrete class is inheriting abstract class A
class B:A
{
//Providing the implementation of the abstract method of abstract class A
//By using the override modifier
public override int Add(int a, int b)
{
return (a+b);
}
public static void Main(String[] ar)
{
B ob = new B();
Console.WriteLine(ob.Add(2,3));
}
}
5
//C# Example of creating an abstract class which only contains
//concrete regular methods and no abstract methods.
using System;
//An abstract class
//With only regular(non-abstract) method in it
abstract class A
{
//instance variable
public int i=10;
//A regular(non-abstract) method
public void Method()
{
Console.WriteLine("A");
}
}
//A concrete class B is inherting abstract class A
class B:A
{
public static void Main(String[] ar)
{
//Creating an object of concrete class B
B ob = new B();
//Calling the regular inherited method, Method()
//Using object of concrete class B
ob.Method();
//Accessing the inherited instance variable, i
//Using object of concrete class B
Console.WriteLine(ob.i);
}
}
//C# You must declare a class with the abstract keyword, //even when there is only a single abstract method in it //Abstract method class A { //An abstract method abstract public void M(); //A non-abstract regular method public void M2() { Console.WriteLine("Concrete method of abstract class"); } }
A.cs(11,22): error CS0513: 'A.m()' is abstract but it is contained in non-abstract class 'A'
//C# An abstract class cannot be instantiated
using System;
//Abstract class
abstract class A
{
//An abstract method
public abstract void Method1();
//A regular(non-abstract) method
public void Method2()
{
Console.WriteLine("Hello");
}
}
class Ex
{
public static void Main(String[] ar)
{
//Creating an object of abstract class
//This should issue a compile error
A ob = new A();
}
}
A.cs(26,9): error CS0144: Cannot create an instance of the abstract class or interface 'A'
//C# Example of an abstract class inheriting another abstract class
using System;
//An abstract class
abstract class A
{
abstract public void Message();
}
//An abstract class B inheriting another abstract class A
abstract class B:A
{
}
//A concrete class Ex inheriting abstract class B(which has inherited abstract class A)
class Ex:B
{
//Overriding the inherited abstract method
//By providing its implementation
public override void Message()
{
Console.WriteLine("Hello!");
}
public static void Main(String[]ar)
{
//Creating an object of concrete class Ex
Ex ob = new Ex();
//Calling the inherited and implemented method of abstract class
ob.Message();
}
}
Hello!
//C# Example of an abstract class inheriting another abstract class
using System;
//An interface
interface A
{
//The method of interface method is abstract and public by default
void Message();
}
//An abstract class B inheriting an interface A
abstract class B:A
{
//Providing the implementation of the inherited method of interface
//Which is a MUST in this case
public void Message()
{
Console.WriteLine("Hello!");
}
}
//A concrete class Ex inheriting abstract class B(which has inherited interface A)
class Ex:B
{
public static void Main(String[]ar)
{
//Creating an object of concrete class Ex
Ex ob = new Ex();
//Calling the inherited and implemented method of abstract class
ob.Message();
}
}
Hello!
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement