C# Tutorial
Part 28:Polymorphism
www.siri-kt.blogspot.com
• Inheritance:(Reusability)
• One of the most important concepts in object-oriented
programming is inheritance. Inheritance allows us to define a
class in terms of another class, which makes it easier to
create and maintain an application.
• This also provides an opportunity to reuse the code
functionality and speeds up implementation time.
• When creating a class, instead of writing completely new
data members and member functions, the programmer can
designate that the new class should inherit the members of
an existing class.
• This existing class is called the baseclass, and the new class
is referred to as the derived class.
• The idea of inheritance implements the IS-A relationship.
For example, mammal IS A animal, dog IS-A mammal hence
dog IS-A animal as well, and so on.
• Base and Derived Classes
• A class can be derived from more than one class or interface, which
means that it can inherit data and functions from multiple base
classes or interfaces.
• The syntax used in C# for creating derived classes is as follows:
• <acess-specifier> class <base_class> { ... } class <derived_class> :
<base_class> { ... }one base class variable features we access
different child class (derived class); base class--child class
• class base
• { }
• class child:base
• { }
• note:":"(colan) is nothing but inheritance. which base class not gives
the full functionality that base class features we can access into
diff child class. here we can create the object for child class. ex on
inheritance:
class emp//base class
• { protected int sal;//variables
• public void esal()//method
• { sal = 15000;
• Console.WriteLine(sal.ToString()); } }
• class dept:emp//base class inherits into child classes
• { int bonus,ta,ts;
• public void tsal()
• { bonus = 5000;
• ta = 600;
• ts = sal + bonus + ta;
• Console.WriteLine("the total salary is:" + ts.ToString());
• Console.ReadLine(); }
• static void Main(string[] args)
• { dept obj = new dept();//object
• obj.esal();
• obj.tsal(); }}
• Inheritance is classified into 5 types
• 1.single inheritance
• 2.multiple inheritance
• 3.hierachial inheritance
• 4.multi level inheritance
• 5.Hybrid inheritance
• example on single inheritance:
• using System;
• namespace singleinheritanceexample
• class BaseClass
• { //Method to find sum of give 2 numbers
• public int FindSum(int x, int y)
• { return (x + y); }
• //method to print given 2 numbers
• //When declared protected , can be accessed only from inside
the derived class
• //cannot access with the instance of derived class
• protected void Print(int x, int y)
• { Console.WriteLine("First Number: " + x);
• Console.WriteLine("Second Number: " + y);
• } }
• class Derivedclass : BaseClass
• { public void Print3numbers(int x, int y, int z)
• { Print(x, y); //We can directly call baseclass members
• Console.WriteLine("Third Number: " + z);
• }
• static void Main(string[] args)
• { //Create instance for derived class, so that base class members
• // can also be accessed
• //This is possible because derivedclass is inheriting base class
• Derivedclass instance = new Derivedclass();
• instance.Print3numbers(30, 40, 50); //Derived class internally calls base
class method.
• int sum = instance.FindSum(30, 40); //calling base class method with derived
class instance
• Console.WriteLine("Sum : " + sum);
• Console.Read();
• } } }
• Multi level inheritance:
• one base class and more child classes.
• class father//base class
• {}
• class son:father//child class 1
• {}
• class son:son//child class 2
• { }
• When one class is derived from another derived
class then this type of inheritance is called
multilevel inheritance");
• Example on multi level inheritance:
• public class Person//base class
• {
• public void persondet()
• { Console.WriteLine("this is the person
class"); }
• }
• public class Bird : Person//child class1
• {
• public void birddet()
• { persondet();
• Console.WriteLine( "this is the birddet Class
• } }
• public class Animal : Bird//child class 2
• { public void animaldet()
• { persondet();
• birddet();
• Console.WriteLine("this is the Animal Class");
• }
• static void Main(string[] args)
• { Animal obj = new Animal();
• obj.persondet();
• obj.birddet();
• obj.animaldet();
• Console.ReadLine();
• } }
• Hierachial inheritance:
one base clase and this base class relation between different child
clasess.
ex:
• class father
• { }
• class son:father
• { }
• class daughter:father
• { }
• This is the type of inheritance in which there are multiple classes
derived from one base class. This type of inheritance is used when
there is a requirement of one class feature that is needed in
multiple classes. here we can create multiple objects for the child
class.
• Example on hierachial inheritance:
• class A //base class
• { public void msg()
• { Console.WriteLine("this is A class Method");
• } }
• class B : A//child class 1
• { public void info()
• { msg();
• Console.WriteLine("this is B class Method");
• }
• class C : A//child class2
• { public void getinfo()
• { msg();
• Console.WriteLine( "this is c class Method");
} }
• static void Main(string[] args)
• { B obj1 = new B();//creat child class 1 obj1
• obj1.info();
• C obj2 = new C();//create child class 2 obj2
• obj2.getinfo();
• Console.ReadLine(); } } }
• Multiple inheritance:
• more than base class and only one child class.
• class father
• { }
• class mother
• { }
• class daughter:father,mother
• { }
• note:
• c#.net does not supports multiple inheritance. C# does not support multiple
inheritances of classes.
• To overcome this problem we can use interfaces.
• Hybrid inheritance:
• this is the combination single, multilevel, hierachial, multiple inheritance
• note :c#.net does not supports hybrid inheritance.
• we can overcome this problem virtual function in functionoverriding
• C# Inheritance
• In C#, inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. In such way, you can reuse, extend or
modify the attributes and behaviors which is defined in other class.
• In C#, the class which inherits the members of another class is called derived
class and the class whose members are inherited is called base class. The derived
class is the specialized class for the base class.
• Advantage of C# Inheritance
• Code reusability: Now you can reuse the members of your parent class. So, there is
no need to define the member again. So less code is required in the class.
• C# Single Level Inheritance Example: Inheriting Fields
• When one class inherits another class, it is known as single level inheritance.
• C# Multi Level Inheritance Example
• When one class inherits another class which is further inherited by another class, it
is known as multi level inheritance in C#. Inheritance is transitive so the last
derived class acquires all the members of all its base classes.
• Note: Multiple inheritance is not supported in C# through class.
• C# Aggregation (HAS-A Relationship)
• In C#, aggregation is a process in which one class defines another class as any
entity reference. It is another way to reuse the class. It is a form of association
that represents HAS-A relationship.
For more visit our website www.siri-kt.blogspot.com
Thanks for
Watching
More Angular JS TutorialsMore C sharp (c#) tutorials

29csharp

  • 1.
  • 2.
    • Inheritance:(Reusability) • Oneof the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. • This also provides an opportunity to reuse the code functionality and speeds up implementation time. • When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. • This existing class is called the baseclass, and the new class is referred to as the derived class. • The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.
  • 3.
    • Base andDerived Classes • A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces. • The syntax used in C# for creating derived classes is as follows: • <acess-specifier> class <base_class> { ... } class <derived_class> : <base_class> { ... }one base class variable features we access different child class (derived class); base class--child class • class base • { } • class child:base • { } • note:":"(colan) is nothing but inheritance. which base class not gives the full functionality that base class features we can access into diff child class. here we can create the object for child class. ex on inheritance:
  • 4.
    class emp//base class •{ protected int sal;//variables • public void esal()//method • { sal = 15000; • Console.WriteLine(sal.ToString()); } } • class dept:emp//base class inherits into child classes • { int bonus,ta,ts; • public void tsal() • { bonus = 5000; • ta = 600; • ts = sal + bonus + ta; • Console.WriteLine("the total salary is:" + ts.ToString()); • Console.ReadLine(); } • static void Main(string[] args) • { dept obj = new dept();//object • obj.esal(); • obj.tsal(); }}
  • 5.
    • Inheritance isclassified into 5 types • 1.single inheritance • 2.multiple inheritance • 3.hierachial inheritance • 4.multi level inheritance • 5.Hybrid inheritance
  • 6.
    • example onsingle inheritance: • using System; • namespace singleinheritanceexample • class BaseClass • { //Method to find sum of give 2 numbers • public int FindSum(int x, int y) • { return (x + y); } • //method to print given 2 numbers • //When declared protected , can be accessed only from inside the derived class • //cannot access with the instance of derived class • protected void Print(int x, int y) • { Console.WriteLine("First Number: " + x); • Console.WriteLine("Second Number: " + y); • } }
  • 7.
    • class Derivedclass: BaseClass • { public void Print3numbers(int x, int y, int z) • { Print(x, y); //We can directly call baseclass members • Console.WriteLine("Third Number: " + z); • } • static void Main(string[] args) • { //Create instance for derived class, so that base class members • // can also be accessed • //This is possible because derivedclass is inheriting base class • Derivedclass instance = new Derivedclass(); • instance.Print3numbers(30, 40, 50); //Derived class internally calls base class method. • int sum = instance.FindSum(30, 40); //calling base class method with derived class instance • Console.WriteLine("Sum : " + sum); • Console.Read(); • } } }
  • 8.
    • Multi levelinheritance: • one base class and more child classes. • class father//base class • {} • class son:father//child class 1 • {} • class son:son//child class 2 • { } • When one class is derived from another derived class then this type of inheritance is called multilevel inheritance");
  • 9.
    • Example onmulti level inheritance: • public class Person//base class • { • public void persondet() • { Console.WriteLine("this is the person class"); } • } • public class Bird : Person//child class1 • { • public void birddet() • { persondet(); • Console.WriteLine( "this is the birddet Class
  • 10.
    • } } •public class Animal : Bird//child class 2 • { public void animaldet() • { persondet(); • birddet(); • Console.WriteLine("this is the Animal Class"); • } • static void Main(string[] args) • { Animal obj = new Animal(); • obj.persondet(); • obj.birddet(); • obj.animaldet(); • Console.ReadLine(); • } }
  • 11.
    • Hierachial inheritance: onebase clase and this base class relation between different child clasess. ex: • class father • { } • class son:father • { } • class daughter:father • { } • This is the type of inheritance in which there are multiple classes derived from one base class. This type of inheritance is used when there is a requirement of one class feature that is needed in multiple classes. here we can create multiple objects for the child class.
  • 12.
    • Example onhierachial inheritance: • class A //base class • { public void msg() • { Console.WriteLine("this is A class Method"); • } } • class B : A//child class 1 • { public void info() • { msg(); • Console.WriteLine("this is B class Method"); • }
  • 13.
    • class C: A//child class2 • { public void getinfo() • { msg(); • Console.WriteLine( "this is c class Method"); } } • static void Main(string[] args) • { B obj1 = new B();//creat child class 1 obj1 • obj1.info(); • C obj2 = new C();//create child class 2 obj2 • obj2.getinfo(); • Console.ReadLine(); } } }
  • 14.
    • Multiple inheritance: •more than base class and only one child class. • class father • { } • class mother • { } • class daughter:father,mother • { } • note: • c#.net does not supports multiple inheritance. C# does not support multiple inheritances of classes. • To overcome this problem we can use interfaces. • Hybrid inheritance: • this is the combination single, multilevel, hierachial, multiple inheritance • note :c#.net does not supports hybrid inheritance. • we can overcome this problem virtual function in functionoverriding
  • 15.
    • C# Inheritance •In C#, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which is defined in other class. • In C#, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class. • Advantage of C# Inheritance • Code reusability: Now you can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class. • C# Single Level Inheritance Example: Inheriting Fields • When one class inherits another class, it is known as single level inheritance. • C# Multi Level Inheritance Example • When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C#. Inheritance is transitive so the last derived class acquires all the members of all its base classes. • Note: Multiple inheritance is not supported in C# through class. • C# Aggregation (HAS-A Relationship) • In C#, aggregation is a process in which one class defines another class as any entity reference. It is another way to reuse the class. It is a form of association that represents HAS-A relationship.
  • 16.
    For more visitour website www.siri-kt.blogspot.com Thanks for Watching More Angular JS TutorialsMore C sharp (c#) tutorials