using System;
public class Creature
{
protected string Name { get; private set; }
public Creature(string name)
{
this.Name = name;
}
private void Talk()
{
Console.WriteLine("I am creature ...");
}
protected void Walk()
{
Console.WriteLine("Walking, walking, walking ....");
}
}
using System;
public class Mammal : Creature
{
public int Age { get; set; }
public Mammal(string name, int age) : base(name)
{
this.Age = age;
}
public void Sleep()
{
Console.WriteLine("Shhh! {0} is sleeping!", this.Name);
}
}
using System;
public class Dog : Mammal
{
public string Breed { get; private set; }
public Dog(string name, int age, string breed) : base(name, age)
{
this.Breed = breed;
}
public void WagTail()
{
Console.WriteLine("{0} is {1} wagging his {2}-aged tail ...",
this.Name, this.Breed, this.Age);
//this.Walk(); // This will successfully compile - Walk() is protected
//this.Talk(); // This will not compile - Talk() is private
//this.Name = "Doggy"; // This will not compile - Name.set is private
}
}
using System;
class InheritanceAndAccessibility
{
static void Main()
{
Dog joe = new Dog("Sharo", 6, "labrador");
joe.Sleep();
Console.WriteLine("Breed: " + joe.Breed);
Console.ReadLine();
joe.WagTail();
}
}
OUTPUT
Shhh! Sharo is sleeping!
Breed: labrador
public class Creature
{
protected string Name { get; private set; }
public Creature(string name)
{
this.Name = name;
}
private void Talk()
{
Console.WriteLine("I am creature ...");
}
protected void Walk()
{
Console.WriteLine("Walking, walking, walking ....");
}
}
using System;
public class Mammal : Creature
{
public int Age { get; set; }
public Mammal(string name, int age) : base(name)
{
this.Age = age;
}
public void Sleep()
{
Console.WriteLine("Shhh! {0} is sleeping!", this.Name);
}
}
using System;
public class Dog : Mammal
{
public string Breed { get; private set; }
public Dog(string name, int age, string breed) : base(name, age)
{
this.Breed = breed;
}
public void WagTail()
{
Console.WriteLine("{0} is {1} wagging his {2}-aged tail ...",
this.Name, this.Breed, this.Age);
//this.Walk(); // This will successfully compile - Walk() is protected
//this.Talk(); // This will not compile - Talk() is private
//this.Name = "Doggy"; // This will not compile - Name.set is private
}
}
using System;
class InheritanceAndAccessibility
{
static void Main()
{
Dog joe = new Dog("Sharo", 6, "labrador");
joe.Sleep();
Console.WriteLine("Breed: " + joe.Breed);
Console.ReadLine();
joe.WagTail();
}
}
OUTPUT
Shhh! Sharo is sleeping!
Breed: labrador
No comments:
Post a Comment