Abstract Method
It is declared by the keyword abstract. It has no body part. Abstract method must be declared inside an Abstract class, but the vice verse is not correct. This means that it is not necessary that a abstract class always contain abstract methods. Abstract method's definition appears in the child.
It is just like the skeleton of the program and act as a reminder. That means it is defined once in the super class and again used in the derived class.In C# the abstract method must be declared as public and it is mandatory that the abstract method must be overridden in the child class. If it is not overridden by child compiler generates error.
Syntax
Examplepublic abstract return-type method-name(parameter list);
public abstract void Get(int a,int b);
Abstract Class
The
class declared by the keyword abstract is called as an Abstract Class.
Usually abstract classes contain abstract methods, but it is not
mandatory. If a class contains abstract method then it is mandatory for
that class to be abstract class.
Abstract
class can not be instantiated. If there is a abstract class there must
be a child to override the abstract methods of abstract class.
Child have to override all the abstract methods. If not or partially, then it should be abstract class again.
Abstract
class is just like the skeleton of the program. In an inheritance
hierarchy, usually top classes are declared as abstract to make a
program more reliable. In this case it will create a child and the same
process will continue.
Abstract Class
The
class declared by the keyword abstract is called as an Abstract Class.
Usually abstract classes contain abstract methods, but it is not
mandatory. If a class contains abstract method then it is mandatory for
that class to be abstract class.
Abstract
class can not be instantiated. If there is a abstract class there must
be a child to override the abstract methods of abstract class.
Child have to override all the abstract methods. If not or partially, then it should be abstract class again.
Abstract
class is just like the skeleton of the program. In an inheritance
hierarchy, usually top classes are declared as abstract to make a
program more reliable. In this case it will create a child and the same
process will continue.
Syntax
See the sample program to better understand abstract class and method. The following program calculates the net salary of an employee.abstract class class-name{Data Members;General methodsAbstract Methods;}
by Abhisek//Abstract classabstract class Demo{protected string name;protected int id;public void Get(string n , int i){name = n;id = i;}//Defining Abstract methodspublic abstract void Set(string des , double sal);public abstract void Salary();}class Salary:Demo{string designation;double netsalary , salary;//Overriding Abstract methodspublic override void Set(string des , double sal){designation = des;salary = s;}public override void Salary(){netsalary = salary + salary * 0.25;}public void Show(){Console.WriteLine("Name:-{0}",name);Console.WriteLine("ID:-{1}", id);Console.WriteLine("Designation:-{2}" , designation);Console.WriteLine("Salary={3}" , salary);Console.WriteLine("Net Salary={4}" , netsalary);}}class MainClass{static void Main(string args[]){Salary s = new Salary();s.Get("Abhisek Panda" , 108);s.Set("Analyst" , 50000);s.Salary();s.Show();}}
No comments:
Post a Comment