Single inheritance :
The process of creating one new class by derived from a
single class is known as single Inheritance.
A simple example for single inheritance is father and son . a father property is can a chance to us by son like their relationship, inheritance forms a relationship called "IS A' for suppose
"Man is a Human ".
A simple example for single inheritance is father and son . a father property is can a chance to us by son like their relationship, inheritance forms a relationship called "IS A' for suppose
"Man is a Human ".
base class = Human
derived class = Man
After inheritance , A Derived class can access Base class properties , But The Base cannot have a permission to access Derived class properties
After inheritance , A Derived class can access Base class properties , But The Base cannot have a permission to access Derived class properties
Define a base class :
class <BaseClassName >
{
//data members of super class
//methods of super class
}
EX :
class A
{
String str = "base class" ;
public void Print()
{
Console.WriteLine("this is "+ str);
}
}
Here we write a normal class A with str as member variable and Print() member method
EX :
class A
{
String str = "base class" ;
public void Print()
{
Console.WriteLine("this is "+ str);
}
}
Here we write a normal class A with str as member variable and Print() member method
Define a derived class:
Class <
SubClassName> : < SuperClassName >
{
//data members of sub
class
//methods of sub class
}
Ex :
class B : A
{
string str2 = "child class";
public void print2()
{
print(); // accessing Class A method with out using object
Console.WriteLine(str2+" is derived from " + str);
}
}
Here By using colon ' : ' operator we can do inheritance from A to B , in Class B we can directly access Class A properties like print() method because they are inherited.
Accessing base class through derived class:
<subclassname> objectname = new <subclassname>();
objectname.SuperClassMethod();
or
variable = objectname.SuperClassVarible ;
Ex :
Class Program
{
B b = new B(); // creating a sub class object
b.print(); // accessing Class A method through object
b.print2() ;
Console.ReadKey();
}
Here we create a sub class object b and using sub class object only we can access directly the base class methods.
output :
this is base class
this is base class
child class is derived from base class
------------------------------------------------------------------------------------------------
Program 2 :
Write a program on single inheritance like entering details in Base class and print that details in Derived class ?
Ex :
class B : A
{
string str2 = "child class";
public void print2()
{
print(); // accessing Class A method with out using object
Console.WriteLine(str2+" is derived from " + str);
}
}
Here By using colon ' : ' operator we can do inheritance from A to B , in Class B we can directly access Class A properties like print() method because they are inherited.
Accessing base class through derived class:
<subclassname> objectname = new <subclassname>();
objectname.SuperClassMethod();
or
variable = objectname.SuperClassVarible ;
Ex :
Class Program
{
B b = new B(); // creating a sub class object
b.print(); // accessing Class A method through object
b.print2() ;
Console.ReadKey();
}
Here we create a sub class object b and using sub class object only we can access directly the base class methods.
output :
this is base class
this is base class
child class is derived from base class
------------------------------------------------------------------------------------------------
Program 2 :
Write a program on single inheritance like entering details in Base class and print that details in Derived class ?
/** A program on single inhertance **/ using System; namespace InheritanceDemo { class ClsParent // creating parent class or base class { public string source, destination; // base class variables public void Reservation() // base class method { Console.Write("Enter city name where your are in now : "); source = Console.ReadLine(); Console.Write("Enter city name where you need to go : "); destination = Console.ReadLine(); } } class ClsChid : ClsParent // creating child class using : colon operator { double amount; public void calAmount() // derived class method { amount=1000; } public void DisplayReservationDetails() // derived class method { Console.Write("\nfyour are travling from "+source); // source is accessible because it is inherited from parent class Console.WriteLine(" to "+destination); // destination is accessible because it is inherited from parent class Console.WriteLine("fare is :"+amount); // amount is child class variable only } } class Program { static void Main(string[] args) { ClsChid obj = new ClsChid(); // creating object for derived class obj.Reservation(); // using derived class object accessing base class method obj.calAmount(); // as usual accesing member methods obj.DisplayReservationDetails(); // as usual accessig member methods Console.ReadKey(); } } }
Output :
Enter city name where you are in now : hyderabad
Enter city name where yor need to go : vijayawada
your are travling from hyderabad to vijayawada
face is : 1000
------------------------------------------------------------------------------------------
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletec net training in electronic city