What is it?
Inheritance is Object oriented programming feature.
Its is used to inherit the properties or methods of parent class into the child class, so that it can access the common method and properties present in the parent class.
Types of Inheritance:
1. Multiple
2. Multi Level
3. Single Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Description:
Suppose we have a separate class for car, bike, bus and we defined accelerate,speed, color method inside all the class.
This would be very time consuming and it will also be difficult to manage when some changes has to be done in the methods.
Instead of rewriting this methods again and again i will take all the common method like accelerate,speed,color inside one separate class and will inherit this in another class like car,bike etc.
Thus if any changes has to be done in future, then just in one of class we will have to do the changes and also it will make our code more concise and clear.
Basic Syntax :
class a
{
//Common method
}
class b : a
{
//accessing common methods
}
class c : a
{
//accessing common methods
}
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Inheritance : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
result r1 = new result();
result2 r2 = new result2();
result r3 = new result2();
int i = r1.AreaREC();
Response.Write("<script language='javascript'>alert('Value from base class Method: "+ i + "');</script>");
int i1 = r2.AreaREC();
int i2 = r2.AreaSq();
Response.Write("<script language='javascript'>alert('Value from Base class method and Derived class method "+ i1 + " and " + i2 + "');</script>");
int i3 = r3.AreaREC();
Response.Write("<script language='javascript'>alert('value from base class Method: " + i3 + "');</script>");
}
}
public class result
{
public int AreaREC()
{
return 100;
}
}
public class result2 : result
{
public int AreaSq()
{
return 150;
}
}
Inheritance is Object oriented programming feature.
Its is used to inherit the properties or methods of parent class into the child class, so that it can access the common method and properties present in the parent class.
Types of Inheritance:
1. Multiple
2. Multi Level
3. Single Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Description:
Suppose we have a separate class for car, bike, bus and we defined accelerate,speed, color method inside all the class.
This would be very time consuming and it will also be difficult to manage when some changes has to be done in the methods.
Instead of rewriting this methods again and again i will take all the common method like accelerate,speed,color inside one separate class and will inherit this in another class like car,bike etc.
Thus if any changes has to be done in future, then just in one of class we will have to do the changes and also it will make our code more concise and clear.
Basic Syntax :
class a
{
//Common method
}
class b : a
{
//accessing common methods
}
class c : a
{
//accessing common methods
}
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Inheritance : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
result r1 = new result();
result2 r2 = new result2();
result r3 = new result2();
int i = r1.AreaREC();
Response.Write("<script language='javascript'>alert('Value from base class Method: "+ i + "');</script>");
int i1 = r2.AreaREC();
int i2 = r2.AreaSq();
Response.Write("<script language='javascript'>alert('Value from Base class method and Derived class method "+ i1 + " and " + i2 + "');</script>");
int i3 = r3.AreaREC();
Response.Write("<script language='javascript'>alert('value from base class Method: " + i3 + "');</script>");
}
}
public class result
{
public int AreaREC()
{
return 100;
}
}
public class result2 : result
{
public int AreaSq()
{
return 150;
}
}
No comments:
Post a Comment