Inheritance : When a class is defined , which retain property and member from previously define
class , this is called inheritance. Inheritance allow parent child relationship in object oriented
programming language . A parent class is a class whose member are inherited in the child class .
The previously define parent class have some member variable and functions and methods,
the newly declared child class have the same member variable, functions, method depending
upon the attribute what the parent class has allowed to inherit.The parent class member definition
determine that inherited members will be editable or not . The parent class is called here the
base class . A colon : , followed by the base class name. Below is a simple class,
which have no inheritance.
class , this is called inheritance. Inheritance allow parent child relationship in object oriented
programming language . A parent class is a class whose member are inherited in the child class .
The previously define parent class have some member variable and functions and methods,
the newly declared child class have the same member variable, functions, method depending
upon the attribute what the parent class has allowed to inherit.The parent class member definition
determine that inherited members will be editable or not . The parent class is called here the
base class . A colon : , followed by the base class name. Below is a simple class,
which have no inheritance.
public
class
Teacher
{
}
Now I am declaring another class which inherit of the previous defined class. here is the
example of inherited class.
public class Teacher
example of inherited class.
public class Teacher
{
}
public
class
Student
: Teacher
{
}
Sealed class : Sealed class is a type of class which cannot be inherited. Sealed keyword tells the compiler that this class cannot be inherit his further. Here is the example
of sealed class. The example shows that the this class cannot be inherited , if you try to inherit
the class compiler will with error.
public
sealed
class
Teacher
{
}
public
class
Student
: Teacher
{
//Error 1 'Student':
cannot derive from sealed type 'Teacher'
}Inherit member : Member of a class , may be variable, function or method. If we inherit a class,
the child class would get all the member of the parent class. Now we can add , additional member ,
variable , function to child class. Sometimes we need some additional of alteration of logic which
was inherited from parent class. The keyword overriding and virtual control the alteration of existing
member.
Override : Override is a keyword which is applied to a function and method of a class, which tells
the compiler that this member can be alter or redefined. The new implementation only allow
when signature and return type and access level same.
Here is the example of overriding method.
public
class
Teacher
{
public
virtual
int
GetHeight()
{
return
0;
}
}
public
class
Student
: Teacher
{
public
override
int
GetHeight()
{
return
0;
}
}Virtual method : Virtual method is a method which can modify property even declaration of a method.
It is overridable by default, it has same signature.We can't use the virtual modifier with the static,
abstract, private or override modifiers.By default, methods are non-virtual.
You cannot override a non-virtual method.
New : new keyword redefine the method again, new is higher the base class declaration .That
means if we declare a method with new keyword, the method will be redefined again.
Example 1
public
class Teacher
{
public
virtual
int
GetHeight()
{
return
0;
}
}
public
class
Student
: Teacher
{
internal
new
int
GetHeight()
{
return
0;
}
}How to access a base class from child class ? base keyword is used to access base class
member. Here is the example base class that can be access from child class. If we use
base keyword in the child class ,then class will get the same to access to its parent class.
public
class Teacher
{
public
virtual
int
GetHeight()
{
return
0;
}
}
public
class
Student
: Teacher
{
internal
new
int
GetHeight()
{
int
a = base.GetHeight();
return
a;
}
}Abstract class
An abstract class is a class whose instance cannot be created,it act like a interface, contain abstract
keyword in the definition .The abstract class may or may not content abstract method.When an
abstract class is inherited, instance can be created of this inherited class only. Abstract class is like
a template of creating another class. As like interface, abstract class has class member, which can
keyword in the definition .The abstract class may or may not content abstract method.When an
abstract class is inherited, instance can be created of this inherited class only. Abstract class is like
a template of creating another class. As like interface, abstract class has class member, which can
also be inherited. Here is an example of an abstract class.
Example 1
Example 1
public
abstract class
Teacher
{
}Example 2
public
abstract
class
Teacher
{
public
abstract
int
GetHeight();
public
abstract
string
Name
{
get;
set;
}
}
public
class
Student
: Teacher
{
public
override
int
GetHeight()
{
return
0;
}
public
override
string
Name
{
get
{
throw
new
NotImplementedException();
}
set
{
throw
new
NotImplementedException();
}
}
}
An abstract class can have access modifier like private, protected ,internal with class member,
but abstract member cannot have private access modifier. Another important thing related to
abstract class is , abstract class cannot be sealed, because The sealed modifier prevent
class to be inherited.
but abstract member cannot have private access modifier. Another important thing related to
abstract class is , abstract class cannot be sealed, because The sealed modifier prevent
class to be inherited.
public
abstract
class
Teacher
{
public
string
GetHeight()
{
return
"Height";
}
private
string
GetWeight()
{
return
"Weight";
}
private
string
GetAddress()
{
return
"Address";
}
}
public
class
Student
: Teacher
{
public
void
Show()
{
this.GetHeight();//Return
Height
this.GetWeight();//error
,in accessable due to protection level
this.GetAddress();//error
,in accessable due to protection level
}
}
also similar to the interface member definition. Only the access modifier , type, parameter are
defined during the declaration of abstract method. No details execution statement is required
for abstract method the detail education have to write in that method.
public class Teacher
defined during the declaration of abstract method. No details execution statement is required
for abstract method the detail education have to write in that method.
public class Teacher
{
public
void
GetHeight()
{
Console.WriteLine("5
feet");
}
}
class
Student
: Teacher
{
}
class
show
{
public
static
void
show()
{
Student
obj=new
Student();
obj.GetHeight();
}
}
//Output::5 feet
Here are some interview question asked by interviewer in different interview.
1)Can you declare an override method static if you original method is not static ?
No, the base class method and the child class method must have same signature.. Net
Framework will not allow different signature in inheritance of method.
Framework will not allow different signature in inheritance of method.
2)Can you declare a private class in a namespace?
In a namespace the class are internal by default, we can declare them public only. Private, protected
access modifiers are not allowed under a namespace.
access modifiers are not allowed under a namespace.
namespace
CShart
{
private
class
Student
{
//Elements
defined in a namespace cannot be explicitly declared as private,
protected,
//or
protected internal
}
}
3)When do you create an abstract class?
An abstract class is a template aur a blueprint of class which need to be followed by the derived class.
In a situation where multiple class is required with common functionality ant common behaviour,
an abstract class is created class, derived class inherit dose functionalities and behaviour.
In a situation where multiple class is required with common functionality ant common behaviour,
an abstract class is created class, derived class inherit dose functionalities and behaviour.
4)How can you prevent your class to be inherited further ?
The sealed keyword till the com pilot that that class cannot be inherited.
5)Is it possible for a class to inherit the constructor of base class?
No, actor of the base class cannot be inherited.
6) Does. Net support multiple inheritance?
No,. Net does not support multiple inheritance. A class cannot we made it from more than one class.
. Net support multiple inheritance to Interface.
. Net support multiple inheritance to Interface.
7)What is new modifier?
New modifier hides the member of the class.
8)Can we have sealed method in abstract class?
The answer is no. Abstract class is blueprint of class, instance cannot be created and not
usable until inherited by another class. Sealed class is a class which prevent class to be inherited.
If we use sealed keyword in the abstract class, abstract class will lose its inheritance
functionality. Compiler will show error during declaration.
abstract sealed class Teacher
usable until inherited by another class. Sealed class is a class which prevent class to be inherited.
If we use sealed keyword in the abstract class, abstract class will lose its inheritance
functionality. Compiler will show error during declaration.
abstract sealed class Teacher
{
//an
abstract class cannot be sealed or static
public
void
GetHeight()
{
}
}
class
Student
: Teacher
{
}
9)Can we have an abstract class without having any abstract method ?
The answer is yes. We can declare abstract class without having any abstract method.
Here is the example of the same.
Here is the example of the same.
abstract
class
Teacher
{
public
void
GetHeight()
{
Console.WriteLine("5
feet");
}
}
class
Student
: Teacher
{
}
10)Can we declare an abstract method in a non abstract class?
The answer is no
11) The difference between abstract class and interface
a)Class maybe inherit only one in abstract class, class may inherit multiple interface.
b)We can declare variable, constant, method to the abstract class. But we can declare
only member and the stub in the interface.
only member and the stub in the interface.
c)Abstract class member can be declared with access modifier, interface cannot have
the access modifier, everything declared in an interface are private by default.
the access modifier, everything declared in an interface are private by default.
No comments:
Post a Comment