Abstract in c#.
Abstract
Class :
: IT
contains method signatures and class which inherits abstract class should mandatorily implement them , But It
also can contain non abstract methods.
Abstract
method : a method which should be mandatorly defined in the child classes.
If a class
contains an abstract method in it, it should be named as an abstract class.
Note : WE cannot create objects for abstract class
using interfaces we can solve multiple inheritance issue , but using abstract class u cannot do so.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
square s = new square();// this is child class of shappe
s.D1 = 10;
//s.areaofsquare(s.D1);// comment
// Console.Read();
Rectangle R = new Rectangle();
R.D1 = 10;
R.D2 = 20;
R.area(R.D1, R.D2);
R.display();
R.ShowParams();
Console.Read();
s.ShowParams();
}
}
abstract class shape
{
public abstract void display();
public virtual void ShowParams()
{
Console.WriteLine("Hello");
}
public void area(int d1, int d2)
{
Console.WriteLine(d1 * d2);
}
}
class square : shape
{
private int d1;
public int D1
{
get
{
return d1;
}
set
{
d1 = value;
}
}
public override void display()
{
Console.WriteLine(d1);
}
public void areaofsquare(int s)
{
area(s, s);
}
}
class Rectangle : shape
{
private int d1, d2;
public int D1
{
get
{
return d1;
}
set
{
d1 = value;
}
}
public override void display()
{
Console.WriteLine(d1);
}
public int D2
{
get
{
return d2;
}
set
{
d2 = value;
}
}
public override void ShowParams()
{
Console.WriteLine("Rectangle");
}
}
}
Abstract in c#.
Reviewed by Share less to Learn More
on
6:42 PM
Rating:
No comments