Wednesday, May 7, 2008

Inheritance in vb and c# examples

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public interface IPerson
{

String MyToString();
}


public class Person:IPerson
{
private String personName = "";
public Person(String name)
{
personName = name;

}
public virtual String MyToString()
{
String output = "";
output += base.ToString()+ " " + personName;
return output;
}
}
public class Worker:Person, IPerson
{
private int workerPay = 0;
public Worker(int pay, String name)
: base(name)
{
workerPay = pay;
}
public override string MyToString()
{

return base.MyToString() + " The worker gets paid " + workerPay;
}
}

public abstract class DomesticHelp:Person
{
private String type = "";
public DomesticHelp(String name, String type ):base(name)
{
this.type = type;

}
public override string MyToString()
{

return base.MyToString() + " The domestic help is of type " + type;
}

}
public class Butler:DomesticHelp
{
public Butler(String name)
: base(name, "butler")
{

}

}
public class Horse
{
public string MyToString()
{

return "I'm a horse not a person";
}
}
public class Program
{
static void Main(string[] args)
{

Person p = new Person ("noel");
Console.WriteLine(p.MyToString());
Worker w = new Worker(2098, "John");
p = new Worker(2345, "nerk");
IPerson x = new Worker(2345, "Fred");
Console.WriteLine(p.MyToString());
Console.WriteLine(w.MyToString());
Console.WriteLine(x.MyToString());
Horse h = new Horse();
Console.WriteLine(h.MyToString());
//the next line will not compile
//h = new Worker(2345, "Fred");
Person p2 = new Person("noel2");
Console.WriteLine(p2.MyToString());
Worker w2 = new Worker(2098, "John2");
Console.WriteLine(w2.MyToString());
p2 = w2;
Console.WriteLine(p2.MyToString());

Console.WriteLine(w2.MyToString());
DomesticHelp d = new Butler("Jeeves");
Console.WriteLine(d.MyToString());
//Person p3 = new Person("noel3");
//Console.WriteLine(p3.MyToString());
//Worker w3 = new Worker(2098, "John3");
//Console.WriteLine(w3.MyToString());
//will not compile w3 = p3;
//will compile but a person cannot be cast to a worker as
//the person does not have space for a pay variable w3 = (Worker)p3;
//Console.WriteLine(p3.MyToString());

//Console.WriteLine(w3.MyToString());



//Console.ReadKey();
}
}
}



Output =

ConsoleApplication1.Person noel
ConsoleApplication1.Worker nerk The worker gets paid 2345
ConsoleApplication1.Worker John The worker gets paid 2098
ConsoleApplication1.Worker Fred The worker gets paid 2345
I'm a horse not a person
ConsoleApplication1.Person noel2
ConsoleApplication1.Worker John2 The worker gets paid 2098
ConsoleApplication1.Worker John2 The worker gets paid 2098
ConsoleApplication1.Worker John2 The worker gets paid 2098
ConsoleApplication1.Butler Jeeves The domestic help is of type butler





Example as vb




Imports System
Imports System.Collections.Generic
Imports System.Text

Namespace ConsoleApplication3

Public Interface IPerson

Function MyToString() As String

End Interface


Public Class Person
Implements IPerson

Private personName As String = ""
Public Sub New(ByVal name As String)

personName = name
End Sub


Public Overridable Function MyToString() As String Implements IPerson.MyToString

Dim output As String = ""
output &= MyBase.ToString() & " " & personName
Return output
End Function

End Class
Public Class Worker
Inherits Person
Implements IPerson

Private workerPay As Integer = 0
Public Sub New(ByVal pay As Integer, ByVal name As String)
MyBase.New(name)
workerPay = pay

End Sub

Public Overrides Function MyToString() As String


Return MyBase.MyToString() & " The worker gets paid " & workerPay
End Function

End Class



Public MustInherit Class DomesticHelp
Inherits Person

Private type As String = ""
Public Sub New(ByVal name As String, ByVal type As String)
MyBase.New(name)

Me.type = type

End Sub
Public Overrides Function MyToString() As String
Return MyBase.MyToString() & " The domestic help is of type " & type
End Function
End Class


Public Class Butler
Inherits DomesticHelp
Public Sub New(ByVal name As String)
MyBase.new(name, "butler")
End Sub
End Class
Public Class Horse

Public Function MyToString() As String
Return "I'm a horse not a person"
End Function
End Class


Public Class Program
Shared Sub Main(ByVal args() As String)


Dim p As Person = New Person("noel")
Console.WriteLine(p.MyToString())
Dim w As Worker = New Worker(2098, "John")
p = New Worker(2345, "nerk")
Dim x As IPerson = New Worker(2345, "Fred")
Console.WriteLine(p.MyToString())
Console.WriteLine(w.MyToString())
Console.WriteLine(x.MyToString())
Dim h As Horse = New Horse()
Console.WriteLine(h.MyToString())
'the next line will not compile
'h = new Worker(2345, "Fred");
Dim p2 As Person = New Person("noel2")
Console.WriteLine(p2.MyToString())
Dim w2 As Worker = New Worker(2098, "John2")
Console.WriteLine(w2.MyToString())
p2 = w2
Console.WriteLine(p2.MyToString())

Console.WriteLine(w2.MyToString())
Dim d As DomesticHelp = New Butler("Jeeves")
Console.WriteLine(d.MyToString())



Console.ReadKey()


End Sub

End Class

End Namespace

1 comment:

Noel said...

Updated to show hiding..
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public interface IPerson
{

String MyToString();
}


public class Person:IPerson
{
private String personName = "";
public Person(String name)
{
personName = name;

}
public virtual String MyToString()
{
String output = "";
output += base.ToString()+ " " + personName;
return output;
}
public String MyBaseToString()
{
String output = "";
output += base.ToString() + " " + personName + "I'm a base method";
return output;
}
}
public class Worker:Person, IPerson
{
private int workerPay = 0;
public Worker(int pay, String name)
: base(name)
{
workerPay = pay;
}
public override string MyToString()
{

return base.MyToString() + " The worker gets paid " + workerPay;
}
public new String MyBaseToString()
{
String output = "";
output += base.ToString() + "I'm a derived method";
return output;
}
}

public abstract class DomesticHelp:Person
{
private String type = "";
public DomesticHelp(String name, String type ):base(name)
{
this.type = type;

}
public override string MyToString()
{

return base.MyToString() + " The domestic help is of type " + type;
}

}
public class Butler:DomesticHelp
{
public Butler(String name)
: base(name, "butler")
{

}

}
public class Horse
{
public string MyToString()
{

return "I'm a horse not a person";
}
}
public class Program
{
static void Main(string[] args)
{

Person p = new Person ("noel");
Console.WriteLine(p.MyToString());
Worker w = new Worker(2098, "John");
p = new Worker(2345, "nerk");
IPerson x = new Worker(2345, "Fred");
Console.WriteLine(p.MyToString());
Console.WriteLine(w.MyToString());
Console.WriteLine(x.MyToString());
Horse h = new Horse();
Console.WriteLine(h.MyToString());
//the next line will not compile
//h = new Worker(2345, "Fred");
Person p2 = new Person("noel2");
Console.WriteLine(p2.MyToString());
Worker w2 = new Worker(2098, "John2");
Console.WriteLine(w2.MyToString());
p2 = w2;
Console.WriteLine(p2.MyToString());

Console.WriteLine(w2.MyToString());
DomesticHelp d = new Butler("Jeeves");
Console.WriteLine(d.MyToString());

Person p4 = new Person("Kim Base");
Worker p5 = new Worker(12345, "Mary Worker");
Person p6 = p5;

Console.WriteLine(p4.MyBaseToString());
Console.WriteLine(p5.MyBaseToString());
Console.WriteLine(p6.MyBaseToString());
//Person p3 = new Person("noel3");
//Console.WriteLine(p3.MyToString());
//Worker w3 = new Worker(2098, "John3");
//Console.WriteLine(w3.MyToString());
//will not compile w3 = p3;
//will compile but a person cannot be cast to a worker as
//the person does not have a pay variable w3 = (Worker)p3;
//Console.WriteLine(p3.MyToString());

//Console.WriteLine(w3.MyToString());



//Console.ReadKey();
}
}
}
ConsoleApplication1.Person noel
ConsoleApplication1.Worker nerk The worker gets paid 2345
ConsoleApplication1.Worker John The worker gets paid 2098
ConsoleApplication1.Worker Fred The worker gets paid 2345
I'm a horse not a person
ConsoleApplication1.Person noel2
ConsoleApplication1.Worker John2 The worker gets paid 2098
ConsoleApplication1.Worker John2 The worker gets paid 2098
ConsoleApplication1.Worker John2 The worker gets paid 2098
ConsoleApplication1.Butler Jeeves The domestic help is of type butler
ConsoleApplication1.Person noel
ConsoleApplication1.Worker nerk The worker gets paid 2345
ConsoleApplication1.Worker John The worker gets paid 2098
ConsoleApplication1.Worker Fred The worker gets paid 2345
I'm a horse not a person
ConsoleApplication1.Person noel2
ConsoleApplication1.Worker John2 The worker gets paid 2098
ConsoleApplication1.Worker John2 The worker gets paid 2098
ConsoleApplication1.Worker John2 The worker gets paid 2098
ConsoleApplication1.Butler Jeeves The domestic help is of type butler
ConsoleApplication1.Person Kim BaseI'm a base method
ConsoleApplication1.WorkerI'm a derived method
ConsoleApplication1.Worker Mary WorkerI'm a base method



Imports System
Imports System.Collections.Generic
Imports System.Text

Namespace ConsoleApplication3

Public Interface IPerson

Function MyToString() As String

End Interface


Public Class Person
Implements IPerson

Private personName As String = ""
Public Sub New(ByVal name As String)

personName = name
End Sub


Public Overridable Function MyToString() As String Implements IPerson.MyToString

Dim output As String = ""
output &= MyBase.ToString() & " " & personName
Return output
End Function

Public Function MyBaseToString() As String

Dim output As String = ""
output &= MyBase.ToString() & " " + personName & "I'm a base method"
Return output
End Function

End Class
Public Class Worker
Inherits Person
Implements IPerson

Private workerPay As Integer = 0
Public Sub New(ByVal pay As Integer, ByVal name As String)
MyBase.New(name)
workerPay = pay

End Sub
Public Shadows Function MyBaseToString() As String

Dim output As String = ""
output += MyBase.ToString() & "I'm a derived method"
Return output
End Function

Public Overrides Function MyToString() As String


Return MyBase.MyToString() & " The worker gets paid " & workerPay
End Function

End Class



Public MustInherit Class DomesticHelp
Inherits Person

Private type As String = ""
Public Sub New(ByVal name As String, ByVal type As String)
MyBase.New(name)

Me.type = type

End Sub
Public Overrides Function MyToString() As String
Return MyBase.MyToString() & " The domestic help is of type " & type
End Function
End Class


Public Class Butler
Inherits DomesticHelp
Public Sub New(ByVal name As String)
MyBase.new(name, "butler")
End Sub
End Class
Public Class Horse

Public Function MyToString() As String
Return "I'm a horse not a person"
End Function
End Class


Public Class Program
Shared Sub Main(ByVal args() As String)


Dim p As Person = New Person("noel")
Console.WriteLine(p.MyToString())
Dim w As Worker = New Worker(2098, "John")
p = New Worker(2345, "nerk")
Dim x As IPerson = New Worker(2345, "Fred")
Console.WriteLine(p.MyToString())
Console.WriteLine(w.MyToString())
Console.WriteLine(x.MyToString())
Dim h As Horse = New Horse()
Console.WriteLine(h.MyToString())
'the next line will not compile
'h = new Worker(2345, "Fred");
Dim p2 As Person = New Person("noel2")
Console.WriteLine(p2.MyToString())
Dim w2 As Worker = New Worker(2098, "John2")
Console.WriteLine(w2.MyToString())
p2 = w2
Console.WriteLine(p2.MyToString())

Console.WriteLine(w2.MyToString())
Dim d As DomesticHelp = New Butler("Jeeves")
Console.WriteLine(d.MyToString())
Dim p4 As Person = New Person("Kim Base")
Dim p5 As Worker = New Worker(12345, "Mary Worker")
Dim p6 As Person = p5

Console.WriteLine(p4.MyBaseToString())
Console.WriteLine(p5.MyBaseToString())
Console.WriteLine(p6.MyBaseToString())



Console.ReadKey()


End Sub

End Class

End Namespace