A class is declared as Abstract when it is incomplete which means that the definition of incomplete method has to be provided by class which is inheriting from this class.
Abstract classes have abstract methods. Abstract methods are the methods which do not have there implementation. Its implementation has to be provided by classes inheriting them.
Abstract Classes are the class that can not be instantiated.
The child class either has to provide definition of abstract function or again declare the method as abstract.
Sealed and abstract can not be used to together as both have opposite meaning
Sealed --> can not act as Parent class. Only have its own objects
Abstract --> Has to act only as Parent class. Can not have it own objects
/*----------------------------------------------------------*/
abstract class Person
{
int age;
string name;
public int Age
{
get { return age; }
set { age = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
/*-------------------------------------------------------*/
Here in the above example we can not create the object of person class.
An abstract class need not necessarily have abstract method.
But if the class contains an abstract method then the class should be made abstract.
/*------------------------------------------------------*/
abstract class Person
{
public abstract void checkAge();
}
/*----------------------------------------------------*/
It provides the functionality for its child classes.
Abstract methods in parent can not be made virtual as they are implicitly virtual. Child class has to override the abstract method or make the method as abstract in itself also.
/*-------------------------------------------------*/
abstract class Person
{
public abstract void checkAge();
}
class Employee:Person
{
public override void checkAge()
{
//Logic to check the age.
}
}
/*-------------------------------------------------*/
or
/*-------------------------------------------------*/
abstract class Person
{
public abstract void checkAge();
}
class Employee:Person
{
public abstract void checkAge()
{
//Logic to check the age.
}
}
/*-------------------------------------------------*/
No comments:
Post a Comment