Abstract Class-(Easy and Simple Way)
Abstract Class: An Abstract class will have the keyword “abstract”.
abstract class GuestVist
{
}
1.The Abstract class will be a super class for all our classes.
2. An Abstract class cannot be accessed by an object.
3. That means we cannot create an object for an abstract class.
4.An Abstract class can have both an Abstract Method and a normal method.
5.In an Abstract Class at least one Abstract Method should be declared.
6. To access the abstract method we should use the “override” keyword in our derived class.
What will happen when we create an object for an Abstract Class
Here we can see the error warning message “An instance of an abstract class cannot be created” when I try to create an object for my abstract class.
Here we can see the error warning message “An instance of an abstract class cannot be created” when I try to create an object for my abstract class.
Here we can see an example of an Abstract class and an Abstract method in detail.
In this example, we can see an Abstract class that has a normal method and Abstract method. Abstract methods do not have a body part in the Abstract class that means we can only declare an Abstract method in an Abstract class. There should be a minimum of one Abstract Method in an Abstract Class.
In this example, we can see an Abstract class that has a normal method and Abstract method. Abstract methods do not have a body part in the Abstract class that means we can only declare an Abstract method in an Abstract class. There should be a minimum of one Abstract Method in an Abstract Class.
- abstract class GuestVist
- {
- public void Guestwelcomemessage()
- {
- Console.WriteLine("Welcome to our AbstractHome");
- }
- public void GuestName()
- {
- Console.WriteLine("Guest name is: Abstract");
- }
- public abstract void purposeofVisit();
- }
- // derived class to inherit the abstract class
- public class Houseclass : GuestVist
- {
- static void Main(string[] args)
- {
- Houseclass objHouse = new Houseclass();
- objHouse.Guestwelcomemessage();
- }
- public override void purposeofVisit()
- {
- Console.WriteLine("Abstract just came for a Meetup and spend some time ");
- }
- }
No comments:
Post a Comment