In this articles i'm going to explain about Inheritance and type of Inheritance. Article submitted by Praveen Reddy.
Inheritance :
- It is mechanism of creating new class by already existing class.
- Inheritace is used to establish the relationship between two (or) more classes.
- It is mechanism of obtaining the Variables and Methods from one class to another class.
- The class which is giving Variable & Methods is called as "Base Class (or) Super Class (or) Parent Class".
- The class which is taking variable and methods is called as "Derived Class (or) Sub Class (or) Child Class".
- The Operator : is inheritance operator
- Inheritance is always is possible between "Base Class" to "Derived Class"
- The Advantages of Inheritance is
- Reusability
- Re-Implementaion
- Extensability
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication28 { //Base class class Country { public void ShowCountry() { Console.WriteLine("This is India.."); } } //Derived class class State : Country { public void ShowState() { Console.WriteLine("This is Andhra Pradesh.."); } } class Program { static void Main(string[] args) { //create instance for Derived class State st = new State(); //access the Method from Base and Derived Class st.ShowCountry(); st.ShowState(); Console.Read(); } } }
Types of Inheritance :
- Single Inheritance
- Multi-Level Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
- Multiple Inheritance
Single Inheritance :
It is a process of creating a "Derived Class" by using "Base Class"
below program is also called as Inheritance and Single Inheritance.using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication28 { //Base class class Country { public void ShowCountry() { Console.WriteLine("This is India.."); } } //Derived class class State : Country { public void ShowState() { Console.WriteLine("This is Andhra Pradesh.."); } } class Program { static void Main(string[] args) { //create instance for Derived class State st = new State(); //access the Method from Base and Derived Class st.ShowCountry(); st.ShowState(); Console.Read(); } } }
Multi-Level Inheritance :
It is a process of creating a "Derived Class" by using another "Base Class"
example of multi - level inheritanceusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { //Base class class Country { public void ShowCountry() { Console.WriteLine("This is India.."); } } //Derived class class State : Country { public void ShowState() { Console.WriteLine("This is Andhra Pradesh.."); } } //Derived class class City : State { public void ShowCity() { Console.WriteLine("This is Vijayawada.."); } } class Program { static void Main(string[] args) { //create instance for Derived class City ct = new City(); //access the Method from Base and Derived Class ct.ShowCountry(); ct.ShowState(); ct.ShowCity(); Console.Read(); } } }
Hierarchical Inheritance :
It is a process of creating multiple "Derived Class" by using single "Base Class"
example of Hierarchical inheritanceusing System; namespace ConsoleApplication1 { class Person { int id; string firstname; string lastname; //Constructor public Person(int id1, string fname, string lname) { this.id=id1; this.firstname=fname; this.lastname=lname; } public void DisplayData() { Console.WriteLine("Id is : "+id); Console.WriteLine("Name is : "+firstname+lastname); } } //Derived Class class Student : Person { char grade; //Constructor public Student(int id1, string fname, string lname, char grade1):base(id1, fname, lname) { this.grade=grade1; } public void DisplayStudentData() { DisplayData(); Console.WriteLine("Grade is : "+grade); } } //Derived Class class Employee : Person { string designation; public Employee(int id1, string fname, string lname, string desig):base(id1, fname, lname) { this.designation=desig; } public void DisplayEmployeeData() { DisplayData(); Console.WriteLine("Designation is : "+desig); } } //Main Class class Program { static void Main() { Student s1=new Student(101, "praveen", "reddy", 'A'); s1.DisplayStudentData(); Employee e1=new Employee(102, "praveen", "reddy", 'Software Engg.'); e1.DisplayEmployeeData(); } } }
Hybrid Inheritance :
It is the combination of two (or) more Inheritance
Multiple Inheritance :
- It is the process of creating a Derived Class from more than one Base Class.
- c#.net (or) java doesn't support multiple inheritance directly through classes.
- in c#.net we can achieve multiple inheritance by using Interface.
1 Komentar untuk "INHERITANCES"
Inheritance : Interview Question and Answer
http://allittechnologies.blogspot.in/2016/01/inheritace-interview-question-and-answer-dotnet-programmer.html