What is Abstraction?
Abstraction is one of the principle of object oriented programming. It is used to display only necessary and essential features of an object to outside the world.Means displaying what is necessary and encapsulate the unnecessary things to outside the world.Hiding can be achieved by using "private" access modifiers.What is Abstract Class?
- An Abstract class cannot be instantiated; it means the object of that class cannot be created.
- Class having the abstract keyword with some of its methods (not all) is known as an Abstract Base Class.
- Class having the Abstract keyword with all of its methods is known as pure Abstract Base Class.
- The method of the abstract class that has no implementation is known as "operation". It can be defined as an abstract void method ();
- An abstract class holds the methods but the actual implementation of those methods is made in derived class.
Note: You cannot create an instance on abstract class but you can declare a variable of abstract type. Please see below example.
Example
class AbstractClass_Example
{
static void Main(string[] args)
{
//College c = new College();//it will thrown an error
student s = new student();
Console.WriteLine(s.StudentName());
Console.Write(s.CourseName());
Console.WriteLine(s.TeacherName());
//We can declare variable of abstract class and store value into that.
College c = s;
Console.WriteLine("Called Abstract :"+ "" + c.StudentName());
Console.ReadLine();
}
}
public abstract class College
{
public abstract string StudentName();
public abstract string CourseName();
public string TeacherName()//You may have a non abstract mehthod inside abstratc class.
{
return "Dr Sara";
}
}
public class student:College
{
public override string StudentName()
{
return "John";
}
public override string CourseName()
{
return "c#";
}
}
No comments:
Post a Comment
Please let me know if you have any feedback or doubts.