C#.NET Abstract Class
Abstract class is a class with both abstract methods and normal methods.
Abstract methods will have only definition and they will not have implementation.
A class that inherits the abstract class should implement the abstract methods in the abstract class by overriding it.
Example program for abstract class
Program
Output
Area of the square = 144
Abstract methods will have only definition and they will not have implementation.
A class that inherits the abstract class should implement the abstract methods in the abstract class by overriding it.
Example program for abstract class
Program
using System;
abstract class ShapesClass
{
abstract public int Area();
}
class Square : ShapesClass
{
int side = 0;
public Square(int n)
{
side =
n;
}
// Area method is required to
avoid
// a compile-time error.
public override int Area()
{
return side * side;
}
static void Main()
{
Square sq = new Square(12);
Console.WriteLine("Area of the square =
{0}", sq.Area());
Console.ReadKey();
}
}
Output
Area of the square = 144
C#.NET Sealed Class
- A class which cannot be inherited by any other class is known as Sealed class.
- It should be declared using "sealed" keyword.
- Sealed classes are used to restrict the inheritance feature of object oriented programming.
- Once a base class is defined as Sealed class,this class cannot be inherited.
- If a class is derived from a Sealed class, compiler throws an error.
sealed class classname
{
//methods
}
class classname2:classname//Invalid because derived class will not inherit sealed class.
Example for sealed class
Program
using System;
namespace SealedClass
{
class Program
{
static void Main(string[] args)
{
SealedClass sealedCls = new SealedClass();
int total = sealedCls.Add(4, 5);
Console.WriteLine("Total = " + total.ToString());
Console.ReadKey();
}
}
// Sealed class
sealed class SealedClass
{
public int Add(int x, int y)
{
return x + y;
}
}
}
Output
Total = 9
C#.NET static class
C#.NET Static class should contain only static methods.
Static methods cannot be called using reference.
We cannot create object for Static class
Static methods cannot be called using reference.
We cannot create object for Static class
Example program for C#.NET Static class
Program
Program
using System;
namespace StaticClass
{
class Program
{
static void Main(string[] args)
{
a.add();
}
}
static class a
{
static public void add()
{
Console.WriteLine("add");
}
}
}
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.
Output
using System;
Output for C#.NET single inheritance
car
bike
car
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
C#.NET Hierarchical Inheritance
Output for C# hierarchical inheritance
helicopter
helicopter
car
helicopter
bike
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
Output
An error will occur because a class cannot have multiple base classes
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
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");
}
}
}
}
show
display
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
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
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
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