|
Abstract Classes : Net frame work: compare with PHP |
| Abstract class is an incomplete class, as expected to be changed down the road and must be inherited by a class and a method override method will do the job |
//csc interface_abstract.cs
using System;
namespace soccer
{
interface team
{
string method_1(string str);
string method_2(string str);
}
//abstract class A : team
//note the error when a method is abstract and class in not an abstract
class A : team
{
public abstract string method_1(string str);
public abstract string method_2(string str);
}
class B : A
{
public override string method_1(string str){ Console.WriteLine(" Hello : " + str); return str;}
public override string method_2(string str){ Console.WriteLine(" Hello : " + str); return str ;}
}
class test: B
{
public static void Main(string[] args)
{
Console.WriteLine("Player for team 1 : ");
string s1 = Console.ReadLine();
Console.WriteLine("Player for team 2 : ");
string s2 = Console.ReadLine();
B b = new B();
b.method_1(s1);
b.method_2(s2);
}
}
}
|
|
| You must override the method
|
//csc interface_abstract_1.cs
using System;
namespace soccer
{
interface team
{
string method_1(string str);
string method_2(string str);
}
//abstract class A : team
//note the error when a method is abstract and class in not an abstract
abstract class A : team
{
public abstract string method_1(string str);
public abstract string method_2(string str);
}
class B : A
{
//public override string method_1(string str){ Console.WriteLine(" Hello : " + str); return str;}
public string method_1(string str){ Console.WriteLine(" Hello : " + str); return str;}
public override string method_2(string str){ Console.WriteLine(" Hello : " + str); return str ;}
}
class test: B
{
public static void Main(string[] args)
{
Console.WriteLine("Player for team 1 : ");
string s1 = Console.ReadLine();
Console.WriteLine("Player for team 2 : ");
string s2 = Console.ReadLine();
B b = new B();
b.method_1(s1);
b.method_2(s2);
}
}
}
|
now let us correct all the code and compile
//csc interface_abstract.cs
using System;
namespace soccer
{
interface team
{
string method_1(string str);
string method_2(string str);
}
abstract class A : team
{
public abstract string method_1(string str);
public abstract string method_2(string str);
}
class B : A
{
public override string method_1(string str){ Console.WriteLine(" Hello : " + str); return str;}
public override string method_2(string str){ Console.WriteLine(" Hello : " + str); return str ;}
}
class test: B
{
public static void Main(string[] args)
{
Console.WriteLine("Player for team 1 : ");
string s1 = Console.ReadLine();
Console.WriteLine("Player for team 2 : ");
string s2 = Console.ReadLine();
B b = new B();
b.method_1(s1);
b.method_2(s2);
}
}
}
|
![]() |
| Now let us retrun the outp put from the method to the caller
//csc interface_abstract_return.cs
using System;
namespace soccer
{
interface team
{
string method_1(string str);
string method_2(string str);
}
abstract class A : team
{
public abstract string method_1(string str);
public abstract string method_2(string str);
}
class B : A
{
public override string method_1(string str){ return str;}
public override string method_2(string str){ return str ;}
}
class test: B
{
public static void Main(string[] args)
{
Console.WriteLine("Player for team 1 : ");
string s1 = Console.ReadLine();
Console.WriteLine("Player for team 2 : ");
string s2 = Console.ReadLine();
B b = new B();
Console.WriteLine(" Hello : " + b.method_1(s1));
Console.WriteLine(" Hello : " + b.method_2(s2));
}
}
}
|
![]() |