Abstract OOPS Concept
Abstract class contain both Abstract method and Non Abstract Method.Abstract class can not be instantiated. Abstract class identified using keyword "abstract" in the class prefix. Abstract class can also be defined with non abstract methods without containing Abstract methods .
code:abstract class
abstract class Operation
{
//Abstract Method
public abstract int add(int num1,int num2);
//Non Abstract Method
public int sub(int num1, int num2)
{
return num1-num2;
}
public abstract int add(int num1,int num2);
//Non Abstract Method
public int sub(int num1, int num2)
{
return num1-num2;
}
}
Abstract method inside abstract class does not have implementation.
Abstract method can be identified using Keyword "abstract" in the method inside the abstract class.
Derived class of the abstract class contain the implementation of abstract methods.
Non Abstract method have implementation inside the abstract class.
Sample Abstract class Code
using System;
using System.Collections.Generic;using System.Text;
namespace AbstractClass
{
abstract class Operation
{
//Abstract Method
public abstract int add(int num1,int num2);
//Non Abstract Method
public int sub(int num1, int num2)
{
return num1-num2;
}
};
class Program : Operation
{
//Abstract method Override Operation in Derived Class
public override int add(int num1, int num2)
{
return num1 + num2;
}
static void Main(string[] args)
{
//Object of Derived class Instance created to access Methods
Program prog = new Program();
Console.WriteLine("addition:{0} Subtraction:{1}",prog.add(10, 5),prog.sub(50, 25));
Console.ReadLine();
}
}
}
Result Screen
Properties of Abstract class & methods:
- Abstract class can not be instantiated.
- Abstract method can not be declared as 'Private'
- Abstract class cant use sealed keyword.
- Abstract method cant use Virtual keyword.
- If the abstract method declares as Protected inside the abstract class In the derived class also abstract method with protected modifier.
No comments :
Post a Comment