Abstract Class :
An Abstract class can contain both Abstract and Non-Abstract methods. If any child class of the Abstract class wants to consume the Non-Abstract methods under the Abstract class. First it has to provide the implementation for all the abstract methods under the abstract class.
Note :
If a class contains any abstract methods in it object of that class can't be created.So object of an abstract class can't be created.
Examples : Absparent.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
abstract class Absparent
{
public void add(int x, int y)
{
Console.WriteLine(x + y);
}
public void sub(int x, int y)
{
Console.WriteLine(x - y);
}
public abstract void mul(int x, int y);
public abstract void div(int x, int y);
}
}
AbsChild.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Abschild :Absparent
{
public override void mul(int x, int y)
{
Console.WriteLine(x * y);
}
public override void div(int x, int y)
{
Console.WriteLine(x / y);
}
static void Main()
{
Abschild c = new Abschild();
c.add(100, 500);
c.sub(200, 100);
c.mul(100, 100);
c.div(200, 100);
Console.ReadLine();
}
}
}
Output :
600
100
10000
2
Another Example :
Using abstract class we can provide re-usability to multiple classes and also impose restriction on child class.
In the below example the class AbstractFigure.cs the class all the variables that are required under various child classes like Rectangle, Triangle, Circle , cone etc.... while can be consumed under the child classes the provide re-usability.
It also imposes a restriction on child classes to provide the implementation for the abstract method it has defined. Which should be done by each child according to it's requirement without change the signature.
------> Add a class AbstractFigure.CS and write the following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public abstract class AbstractFigure
{
public double width, height, radius;
public abstract double GetArea();
public abstract double GetPerimeter();
}
}
------> Add a class Rect.cs and Write the following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Rect : AbstractFigure
{
private double p;
public Rect(double width, double height)
{
// In this context this and base refers to variables of AbstractFigure calss only
this.width = width;
base.height = height;
}
public Rect(double p)
{
// TODO: Complete member initialization
this.p = p;
}
public override double GetArea()
{
return width * height;
}
public override double GetPerimeter()
{
return 2 * (width + height);
}
}
}
----> Add a new class TestFigure.cs to consume all the figures we have implemented
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class TestFigure
{
static void Main()
{
Rect r = new Rect(347.87,332.34);
Console.WriteLine(r.GetArea());
Console.WriteLine(r.GetPerimeter());
Console.ReadLine();
}
}
}
OutPut :
115611.1158
1368.42
An Abstract class can contain both Abstract and Non-Abstract methods. If any child class of the Abstract class wants to consume the Non-Abstract methods under the Abstract class. First it has to provide the implementation for all the abstract methods under the abstract class.
Note :
If a class contains any abstract methods in it object of that class can't be created.So object of an abstract class can't be created.
Examples : Absparent.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
abstract class Absparent
{
public void add(int x, int y)
{
Console.WriteLine(x + y);
}
public void sub(int x, int y)
{
Console.WriteLine(x - y);
}
public abstract void mul(int x, int y);
public abstract void div(int x, int y);
}
}
AbsChild.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Abschild :Absparent
{
public override void mul(int x, int y)
{
Console.WriteLine(x * y);
}
public override void div(int x, int y)
{
Console.WriteLine(x / y);
}
static void Main()
{
Abschild c = new Abschild();
c.add(100, 500);
c.sub(200, 100);
c.mul(100, 100);
c.div(200, 100);
Console.ReadLine();
}
}
}
600
100
10000
2
Another Example :
Using abstract class we can provide re-usability to multiple classes and also impose restriction on child class.
In the below example the class AbstractFigure.cs the class all the variables that are required under various child classes like Rectangle, Triangle, Circle , cone etc.... while can be consumed under the child classes the provide re-usability.
It also imposes a restriction on child classes to provide the implementation for the abstract method it has defined. Which should be done by each child according to it's requirement without change the signature.
------> Add a class AbstractFigure.CS and write the following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public abstract class AbstractFigure
{
public double width, height, radius;
public abstract double GetArea();
public abstract double GetPerimeter();
}
}
------> Add a class Rect.cs and Write the following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Rect : AbstractFigure
{
private double p;
public Rect(double width, double height)
{
// In this context this and base refers to variables of AbstractFigure calss only
this.width = width;
base.height = height;
}
public Rect(double p)
{
// TODO: Complete member initialization
this.p = p;
}
public override double GetArea()
{
return width * height;
}
public override double GetPerimeter()
{
return 2 * (width + height);
}
}
}
----> Add a new class TestFigure.cs to consume all the figures we have implemented
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class TestFigure
{
static void Main()
{
Rect r = new Rect(347.87,332.34);
Console.WriteLine(r.GetArea());
Console.WriteLine(r.GetPerimeter());
Console.ReadLine();
}
}
}
OutPut :
115611.1158
1368.42
No comments:
Post a Comment