Unit4 Csharp
Unit4 Csharp
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.
When one class inherits another class, it is known as single level inheritance. Let's see
the example of single level inheritance which inherits the fields only.
1. using System;
2. public class Employee
3. {
4. public float salary = 40000;
5. }
6. public class Programmer: Employee
7. {
8. public float bonus = 10000;
9. }
10. class TestInheritance{
11. public static void Main(string[] args)
12. {
13. Programmer p1 = new Programmer();
14.
15. Console.WriteLine("Salary: " + p1.salary);
16. Console.WriteLine("Bonus: " + p1.bonus);
17.
18. }
19. }
Output:
Unit-4 Page 1
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
Salary: 40000
Bonus: 10000
1. using System;
2. public class Animal
3. {
4. public void eat() { Console.WriteLine("Eating..."); }
5. }
6. public class Dog: Animal
7. {
8. public void bark() { Console.WriteLine("Barking..."); }
9. }
10. class TestInheritance2{
11. public static void Main(string[] args)
12. {
13. Dog d1 = new Dog();
14. d1.eat();
15. d1.bark();
16. }
17. }
Output:
Eating...
Barking...
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.
Let's see the example of multi level inheritance in C#.
Unit-4 Page 2
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
1. using System;
2. public class Animal
3. {
4. public void eat() { Console.WriteLine("Eating..."); }
5. }
6. public class Dog: Animal
7. {
8. public void bark() { Console.WriteLine("Barking..."); }
9. }
10. public class BabyDog : Dog
11. {
12. public void weep() { Console.WriteLine("Weeping..."); }
13. }
14. class TestInheritance2{
15. public static void Main(string[] args)
16. {
17. BabyDog d1 = new BabyDog();
18. d1.eat();
19. d1.bark();
20. d1.weep();
21. }
22. }
Output:
Eating...
Barking...
Weeping...
C# Sealed
C# sealed keyword applies restrictions on the class and method. If you create a sealed
class, it cannot be derived. If you create a sealed method, it cannot be overridden.
Unit-4 Page 3
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
C# Sealed class
C# sealed class cannot be derived by any class. Let's see an example of sealed class in
C#.
1. using System;
2. sealed public class Animal{
3. public void eat() { Console.WriteLine("eating..."); }
4. }
5. public class Dog: Animal
6. {
7. public void bark() { Console.WriteLine("barking..."); }
8. }
9. public class TestSealed
10. {
11. public static void Main()
12. {
13. Dog d = new Dog();
14. d.eat();
15. d.bark();
16. }
17. }
Output:
Compile Time Error: 'Dog': cannot derive from sealed type 'Animal'
C# Sealed method
The sealed method in C# cannot be overridden further. It must be used with override
keyword in method.
1. using System;
2. public class Animal{
3. public virtual void eat() { Console.WriteLine("eating..."); }
4. public virtual void run() { Console.WriteLine("running..."); }
Unit-4 Page 4
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
5.
6. }
7. public class Dog: Animal
8. {
9. public override void eat() { Console.WriteLine("eating bread..."); }
10. public sealed override void run() {
11. Console.WriteLine("running very fast...");
12. }
13. }
14. public class BabyDog : Dog
15. {
16. public override void eat() { Console.WriteLine("eating biscuits..."); }
17. public override void run() { Console.WriteLine("running slowly..."); }
18. }
19. public class TestSealed
20. {
21. public static void Main()
22. {
23. BabyDog d = new BabyDog();
24. d.eat();
25. d.run();
26. }
27. }
Output:
1. using System;
2. public class TestSealed
3. {
4. public static void Main()
5. {
6. sealed int x = 10;
7. x++;
8. Console.WriteLine(x);
Unit-4 Page 5
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
9. }
10. }
Output:
C# Abstract
Abstract classes are the way to achieve abstraction in C#. Abstraction in C# is the
process to hide the internal details and showing functionality only. Abstraction can be
achieved by two ways:
1. Abstract class
2. Interface
Abstract class and interface both can have abstract methods which are necessary for
abstraction.
Abstract Method
A method which is declared abstract and has no body is called abstract method. It can
be declared inside the abstract class only. Its implementation must be provided by
derived classes. For example:
You can't use static and virtual modifiers in abstract method declaration.
C# Abstract class
In C#, abstract class is a class which is declared abstract. It can have abstract and non-
abstract methods. It cannot be instantiated. Its implementation must be provided by
derived classes. Here, derived class is forced to provide the implementation of all the
abstract methods.
Unit-4 Page 6
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
Let's see an example of abstract class in C# which has one abstract method draw(). Its
implementation is provided by derived classes: Rectangle and Circle. Both classes have
different implementation.
1. using System;
2. public abstract class Shape
3. {
4. public abstract void draw();
5. }
6. public class Rectangle : Shape
7. {
8. public override void draw()
9. {
10. Console.WriteLine("drawing rectangle...");
11. }
12. }
13. public class Circle : Shape
14. {
15. public override void draw()
16. {
17. Console.WriteLine("drawing circle...");
18. }
19. }
20. public class TestAbstract
21. {
22. public static void Main()
23. {
24. Shape s;
25. s = new Rectangle();
26. s.draw();
27. s = new Circle();
28. s.draw();
29. }
30. }
Output:
drawing ractangle...
drawing circle...
Unit-4 Page 7
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
C# Interface
Interface in C# is a blueprint of a class. It is like abstract class because all the methods
which are declared inside the interface are abstract methods. It cannot have method
body and cannot be instantiated.
It is used to achieve multiple inheritance which can't be achieved by class. It is used to
achieve fully abstraction because it cannot have method body.
Its implementation must be provided by class or struct. The class or struct which
implements the interface, must provide the implementation of all the methods declared
inside the interface.
C# interface example
Let's see the example of interface in C# which has draw() method. Its implementation is
provided by two classes: Rectangle and Circle.
1. using System;
2. public interface Drawable
3. {
4. void draw();
5. }
6. public class Rectangle : Drawable
7. {
8. public void draw()
9. {
Unit-4 Page 8
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
24. Drawable d;
25. d = new Rectangle();
26. d.draw();
27. d = new Circle();
28. d.draw();
29. }
30. }
Output:
drawing ractangle...
drawing circle...
Note: Interface methods are public and abstract by default. You cannot explicitly use
public and abstract keywords for an interface method.
1. using System;
2. public interface Drawable
3. {
4. public abstract void draw();//Compile Time Error
5. }
C# Namespaces
Namespaces in C# are used to organize too many classes so that it can be easy to
handle the application.
In a simple C# program, we use System.Console where System is the namespace and
Console is the class. To access the class of a namespace, we need to use
namespacename.classname. We can use using keyword so that we don't have to use
complete name all the time.
In C#, global namespace is the root namespace. The global::System will always refer to
the namespace "System" of .Net Framework.
C# namespace example
Let's see a simple example of namespace which contains one class "Program".
1. using System;
2. namespace ConsoleApplication1
3. {
4. class Program
5. {
6. static void Main(string[] args)
Unit-4 Page 9
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
7. {
8. Console.WriteLine("Hello Namespace!");
9. }
10. }
11. }
Output:
Hello Namespace!
Unit-4 Page 10
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
Unit-4 Page 11
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
Unit-4 Page 12
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
C# | Multilevel Inheritance
In the Multilevel inheritance, a derived class will inherit a base class and as well as
the derived class also act as the base class to other class. For example, three
classes called A, B, and C, as shown in the below image, where class C is derived
from class B and class B, is derived from class A. In this situation, each derived
class inherit all the characteristics of its base classes. So class C inherits all the
features of class A and B.
Unit-4 Page 13
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
Example: Here, the derived class Rectangle is used as a base class to create the
derived class called ColorRectangle. Due to inheritance the ColorRectangle inherit
all the characteristics of Rectangle and Shape and add an extra field called rcolor,
which contains the color of the rectangle.
This example also covers the concept of constructors in a derived class. As we know
that a subclass inherits all the members (fields, methods) from its superclass but
constructors are not members, so they are not inherited by subclasses, but the
constructor of the superclass can be invoked from the subclass. As shown in the
below example, base refers to a constructor in the closest base class. The base in
ColorRectangle calls the constructor in Rectangle and the base in Rectangle class
the constructor in Shape.
// C# program to illustrate the
// concept of multilevel inheritance
using System;
class Shape {
double a_width;
double a_length;
// Default constructor
public Shape()
{
Width = Length = 0.0;
}
set {
Unit-4 Page 14
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
set {
a_length = value < 0 ? -value : value;
}
}
public void DisplayDim()
{
Console.WriteLine("Width and Length are "
+ Width + " and " + Length);
}
}
string Style;
// A default constructor.
// This invokes the default
// constructor of Shape.
public Rectangle()
{
Style = "null";
}
// Constructor
public Rectangle(string s, double w, double l)
: base(w, l)
{
Style = s;
}
// Construct an square.
public Rectangle(double y)
: base(y)
{
Style = "square";
}
Unit-4 Page 15
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
}
}
string rcolor;
// Constructor
public ColorRectangle(string c, string s,
double w, double l)
: base(s, w, l)
{
rcolor = c;
}
// Driver Class
class GFG {
// Main Method
static void Main()
{
ColorRectangle r1 = new ColorRectangle("pink",
"Fibonacci rectangle", 2.0, 3.236);
Area is 6.472
Details of r2:
Rectangle is Square
Width and Length are 4 and 4
Color is black
Area is 16
Unit-4 Page 17
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
WriteLine( ). Many classes override this method. Doing so allows them to tailor a description
specifically for the types of objects that they create. For example: // Demonstrate ToString()
Unit-4 Page 18
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
Unit-4 Page 19
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
Here, the value of x is automatically boxed when it is passed to Sqr( ). Boxing and unboxing
allow C#’s type system to be fully unified. All types derive from object. A reference to any
type can be assigned to a variable of type object. Boxing and unboxing automatically handle
the details for the value types. Furthermore, because all types are derived from object, they
all have access to object’s methods. For example, consider the following rather surprising
program:
Unit-4 Page 20
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
Unit-4 Page 21
C # PROG.[ UCS15601] Unit-4 DrRajeev Sharma
Unit-4 Page 22