/* The given C sharp example decalres abstract class Shape
* that implements polymorpism by overloading and
* overridding methods. The clas Shape2 consists
* of a virtual method ShapeType(). The method
* CalculateArea() is implemented twice by having
* different signatures and different return types.
* The class Square inherits the base class Shape2
* and overrides the base class method called ShapeType().
* The main() method creates an instance of the derieved
* class Square and the base class Shape2. the instance
* of the base class Shape2 invokes the method
* CalculateArea(). Depending on the parameter that
* the method CalcualteArea() takes, the apropiate
* method is called.
*/
using System;
namespace Inherit
{
class Shape2
{
public virtual void ShapeType()
{
Console.WriteLine("This message will be used to display type of the shape");
}
public int CalculateArea(int side)
{
return side * side;
}
public double CalculateArea(double baseBreadth, double height)
{
return 05 * baseBreadth * height;
}
}
class Square :Shape2
{
public override void ShapeType()
{
Console.WriteLine("A square is a Polygon");
}
public static void Main(string[] args)
{
Square sqr = new Square();
sqr.ShapeType();
Console.WriteLine("Area IS : "+sqr.CalculateArea(6));
}
}
}
//Output
//A square is a Polygon
//Area IS : 36
Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts
Sunday, June 15, 2008
Saturday, June 14, 2008
Multi level inheritance
/* Write a program in C sharp on parameteried
construtor in multilevel inheritance*
*/
using System;
namespace Inherit
{
class Metals
{
string _metalType;
public Metals(String type)
{
_metalType = type;
Console.WriteLine("Metal\t\t:" + _metalType);
}
}
class SteelCompany : Metals
{
string _grade;
public SteelCompany(string grade) : base("Steel")
{
_grade = grade;
Console.WriteLine("Grade\t\t:" + _grade);
}
}
class Automobiles : SteelCompany
{
string _part;
public Automobiles(string part): base("Cast Iron")
{
_part = part;
Console.WriteLine("Part\t\t:" + _part);
}
public static void Main()
{
Automobiles auto = new Automobiles("Mercedes");
}
}
}
//Output
//Metal :Steel
//Grade :Cast Iron
//Part :Mercedes
construtor in multilevel inheritance*
*/
using System;
namespace Inherit
{
class Metals
{
string _metalType;
public Metals(String type)
{
_metalType = type;
Console.WriteLine("Metal\t\t:" + _metalType);
}
}
class SteelCompany : Metals
{
string _grade;
public SteelCompany(string grade) : base("Steel")
{
_grade = grade;
Console.WriteLine("Grade\t\t:" + _grade);
}
}
class Automobiles : SteelCompany
{
string _part;
public Automobiles(string part): base("Cast Iron")
{
_part = part;
Console.WriteLine("Part\t\t:" + _part);
}
public static void Main()
{
Automobiles auto = new Automobiles("Mercedes");
}
}
}
//Output
//Metal :Steel
//Grade :Cast Iron
//Part :Mercedes
Multilevel Inhetitance 2
/* C# program to implemmet multi level inheritance.
This program demonstrates how base class constructors are inherited using multilevel inheritance. */
using System;
namespace Inherit
{
class Shape
{
public Shape()
{
Console.WriteLine("Constructor of base class Shape");
}
}
class Polygon : Shape
{
public Polygon()
{
Console.WriteLine("Constructor of Base class Polygon");
}
}
class Quad : Polygon
{
public Quad()
{
Console.WriteLine("Contructor of class Quad");
}
public static void Main(string[] args)
{
Quad obj= new Quad();
}
}
}
//Output
//Constructor of base class Shape
//Constructor of Base class Polygon
//Contructor of class Quad
This program demonstrates how base class constructors are inherited using multilevel inheritance. */
using System;
namespace Inherit
{
class Shape
{
public Shape()
{
Console.WriteLine("Constructor of base class Shape");
}
}
class Polygon : Shape
{
public Polygon()
{
Console.WriteLine("Constructor of Base class Polygon");
}
}
class Quad : Polygon
{
public Quad()
{
Console.WriteLine("Contructor of class Quad");
}
public static void Main(string[] args)
{
Quad obj= new Quad();
}
}
}
//Output
//Constructor of base class Shape
//Constructor of Base class Polygon
//Contructor of class Quad
Multilevel Inheritance
/* Program in C# for implementation of multiway inheritance */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inherit
{
class One:Mammal
{
public void Noise()
{
Console.WriteLine("Dog Barks");
}
static void Main(string[] args)
{
One obj = new One();
obj.Eat();
obj.feature();
obj.Noise();
}
}
class Animal
{
public void Eat()
{
Console.WriteLine("Every Animal Eats Something");
}
}
class Mammal : Animal
{
public void feature()
{
Console.WriteLine("Mammal Gives birth to young ones");
}
}
}
//Output
//Every Animal Eats Something
//Mammal Gives birth to young ones
//Dog Barks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inherit
{
class One:Mammal
{
public void Noise()
{
Console.WriteLine("Dog Barks");
}
static void Main(string[] args)
{
One obj = new One();
obj.Eat();
obj.feature();
obj.Noise();
}
}
class Animal
{
public void Eat()
{
Console.WriteLine("Every Animal Eats Something");
}
}
class Mammal : Animal
{
public void feature()
{
Console.WriteLine("Mammal Gives birth to young ones");
}
}
}
//Output
//Every Animal Eats Something
//Mammal Gives birth to young ones
//Dog Barks
Subscribe to:
Posts (Atom)