C#.NET Inheritance - Single, Multi level, multiple, hierarchical, Hybrid

Acquiring or getting the properties from one class to another class is known as Inheritance
In Object Oriented approach if we can apply parent/child relation between classes.
Members of the parent class can be consumed by the child classes.

Syntax for C#.NET Inheritance

class parent
{
body
}
class child : parent
{
body
}

Example program for C#.NET Single Inheritance with 2 classes a & b with 2 methods display & show inside each them

Program

using System;

namespace Inheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            b o1 = new b();//creating object for the derived class
            o1.show();//calling method from child class
            o1.display();//calling method from parent class
            Console.ReadKey();
        }
        class a//parent or base class
        {
            public void display()
            {
                Console.WriteLine("display");
            }
        }
        class b : a//child or derived class inherited with base class
        {
            public void show()
            {
                Console.WriteLine("show");
            }
        }
    }
}

Output
show
display

C#.NET Single Inheritance

When a single derived class is derived from single base class, then that inheritance is called as single inheritance
In single inheritance we have single base class that is inherited by the derived class.
Here the derived class has all the features of the base class and can add some new features.
The inheritance also depends on the access specifier that is used at the time of base class inheritance.

Syntax for C#.NET single Inheritance

class Parent
{
body
}
class child : parent
{
body
}

Example for C#.NET single inheritance

Program

using System;

namespace SingleInheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            dad d = new dad();
            d.car();
            son s = new son();
            s.bike();
            s.car();
            Console.ReadKey();
        }
        class dad
        {
            public void car()
            {
                Console.WriteLine("car");
            }
        }
        class son : dad
        {
            public void bike()
            {
                Console.WriteLine("bike");
            }
        }
    }
}


Output for C#.NET single inheritance

car
bike
car

C#.NET Multi level Inheritance

Acquiring properties to a sub child class from the child class and also from the parent class.
When a derived class is created from another derived class, then that inheritance is called as Multi level Inheritance

Syntax for C#.NET multi level inheritance

class parent
{
body
}
class child : parent
{
body
}
class subchild : child
{
body
}

Example for C#.NET multi level inheritance
Program

using System;

namespace MultiLevelInheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            grandfather g = new grandfather();
            g.helicopter();
            dad d = new dad();
            d.car();
            d.helicopter();
            son s = new son();
            s.bike();
            s.car();
            s.helicopter();
            Console.ReadKey();
        }
        class grandfather
        {
            public void helicopter()
            {
                Console.WriteLine("helicopter");
            }
        }
        class dad : grandfather
        {
            public void car()
            {
                Console.WriteLine("car");
            }
        }
        class son : dad
        {
            public void bike()
            {
                Console.WriteLine("bike");
            }
        }
    }
}


Output for C#.NET Multi level inheritance example

helicopter
car
helicopter
bike
car
helicopter

C#.NET Hierarchical Inheritance

Single parent class with multiple child classes are called as Hierarchical Inheritance.
In other words when more than one derived class are created from single base class is called Hierarchical Inheritance.

Syntax for C#.NET hierarchical inheritance


class parent
{
body
}
class child1 : parent
{
body
}
class child2 : parent
{
body
}

Example for C#.NET hierarchical inheritance
Program

using System;

namespace HierarchicalInheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            grandfather g = new grandfather();
            g.helicopter();
            dad d = new dad();
            d.helicopter();
            d.car();
            son s = new son();
            s.helicopter();
            s.bike();
            Console.ReadKey();
        }
        class grandfather
        {
            public void helicopter()
            {
                Console.WriteLine("helicopter");
            }
        }
        class dad : grandfather
        {
            public void car()
            {
                Console.WriteLine("car");
            }
        }
        class son : grandfather
        {
            public void bike()
            {
                Console.WriteLine("bike");
            }
        }
    }
}


Output for C# hierarchical inheritance

helicopter
helicopter
car
helicopter
bike

C#.NET Multiple Inheritance

One child with two parents is known as Multiple Inheritance.
Multiple Inheritance can contain only one derived class, two or more base classes.
When a derived class is created from more than one base class then that inheritance is called as Multiple Inheritance
Multiple Inheritance is not supported in C#.NET because of ambiguity or confusion problem.

Syntax for C#.NET Multiple inheritance

class Parent
{
body
}
class parent2
{
body
}
class child : parent,parent2

Example for C#.NET Mulitple inheritance with error

Program

using System;

namespace MultipleInheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            grandfather g = new grandfather();
            g.helicopter();
            dad d = new dad();
            d.car();
            Console.ReadKey();
        }
        class grandfather
        {
            public void helicopter()
            {
                Console.WriteLine("helicopter");
            }
        }
        class dad
        {
            public void car()
            {
                Console.WriteLine("car");
            }
        }
        class son : dad, grandfather
        {
            public void bike()
            {
                Console.WriteLine("bike");
            }
        }
    }
}


Output

An error will occur because a class cannot have multiple base classes

C#.NET Hybrid Inheritance

Any combination of single, hierarchical and multi level inheritance is called as Hybrid Inheritance.
Tuesday, 5 January 2016
Posted by Sudhir Chekuri
Tag :

C#.NET Encapsulation

  • Encapsulation is a procedure of binding data under class name.
  • Encapsulation is a procedure of covering up of the data & functions into a single unit called as Class.
  • An encapsulated object is often called an abstract data type.
  • Rather than defining the data in the form of public, we can declare those fields as private.
  • In other words wrapping up of data and methods into a single unit is called as "Encapsulation"(ex:Class,method).
Posted by Sudhir Chekuri
Tag :

C#.NET Partial class

Partial class is an unfinished class.

Generally we cannot create two classes with the same name in a namespace. So, Every class you create is a finished class which cannot open again to add some code.
If you create a class as partial class then you can again create a class with the same name and the code inside both the classes will belong to the same class.

Example program to create partial classes and calling methods inside two partial classes with the same object

Program
using System;

namespace PartialClass
{
    class Program
    {
        static void Main(string[] args)
        {
            a o = new a();
            o.add();
            o.sub();
            Console.ReadKey();
        }
    }

    partial class a
    {
        public void add()
        {
            Console.WriteLine("add");
        }
    }
    partial class a
    {
        public void sub()
        {
            Console.WriteLine("sub");
        }
    }

}


Output

add

sub
Posted by Sudhir Chekuri
Tag :

C#.NET Destructor

  • · Destructor is a special method which has the same name of the class.
  • · It is invoked automatically when an object of that class is cleared.
Example program to declare a destructor for a class

Program

using System;
namespace destructor
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.ReadKey();
        }
        ~Program()
        {
            Console.WriteLine("destroyed");
            Console.ReadKey();
        }
    }
}


Output
destroyed

In the above example destructor with the same name of the class i.e., Program is created using tilt symbol before the method name as below.

~ Program()
{
}

This destructor is invoked automatically when the memory of that class Program is cleared.
So, the message in destructor method is displayed before the output screen gets closed.
Posted by Sudhir Chekuri
Tag :

C#.NET Constructor - with parameter, overloading

  • Constructor is a special method which has the same name of the class.
  • It is invoked automatically when an object for that class is created.
  • It is used for initializing the components of a class.
Example program to declare a constructor for a class
Program

using System;
namespace constructor
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.ReadKey();
        }
        public Program()
        {
            Console.WriteLine("program");
        }
    }
}

Output
program

In the above example a constructor with name Program is declared i.e., with the same name of the class in which it is declared.
This constructed is invoked automatically when an object is created for the Program class.
So, When an object p is created for the class Program constructor is invoked and the code inside that gets executed to print program on the output screen

Constructor with parameter

Declaring a user defined constructor with and input parameter is known as Constructor with parameter or parameterized constructor.

Example program to declare a parameterized constructor for a class

Program


using System;
namespace constructor
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program(5);
            Console.ReadKey();
        }
        public Program(int i)
        {
            Console.WriteLine("Parameter value: "+i);
        }
    }
}

Output

Parameter value: 5

Constructor Overloading

Multiple constructors for the same class with the same names but different parameters is known as constructor overloading.

Example program for constructor overloading

using System;
namespace constructor
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program(5);
            Program p1 = new Program(5, 10);
            Console.ReadKey();
        }
        public Program(int a)
        {
            Console.WriteLine("Parameter value: " + a);
        }
        public Program(int a, int b)
        {
            Console.WriteLine("Sum: " + (a + b));
        }
    }
}


Output

Parameter value: 5

Sum: 15
Posted by Sudhir Chekuri
Tag :

C#.NET Object

  • Object is an instance of class.
  • Object will have memory.
  • Object is used to call methods of a class.
  • Object is a reference type.
  • To create this object "new" keyword is used.
Syntax

classname objectname= new constructor();

Example

maths m=new maths();

Example program using object for a class

Program

using System;

namespace Class_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            maths m = new maths();
            m.add();
            Console.ReadKey();
        }
    }
    class maths
    {
        public void add()
        {
            int a = 10;
            int b = 20;
            Console.WriteLine("Sum is: " + (a + b));
        }
    }
}

Output

sum is : 30
Posted by Sudhir Chekuri
Tag :

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -