0% found this document useful (0 votes)
3 views

C Sharp Inhertance

The document provides an overview of C# inheritance, aggregation, member overloading, method overriding, and polymorphism. It explains key concepts such as single-level and multi-level inheritance, the use of the base keyword, and examples of method overloading and overriding. Additionally, it discusses compile-time and runtime polymorphism, highlighting their significance in object-oriented programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C Sharp Inhertance

The document provides an overview of C# inheritance, aggregation, member overloading, method overriding, and polymorphism. It explains key concepts such as single-level and multi-level inheritance, the use of the base keyword, and examples of method overloading and overriding. Additionally, it discusses compile-time and runtime polymorphism, highlighting their significance in object-oriented programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

C# Inheritance

In C#, inheritance is a process in which one object acquires all the properties
and behaviours of its parent object automatically. In such way, you can reuse,
extend or modify the attributes and behaviours 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.
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:
Salary: 40000
Bonus: 10000
In the above example, Employee is the base class and Programmer is
the derived class.
C# Single Level Inheritance Example: Inheriting Methods
Let's see another example of inheritance in C# which inherits methods only.
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...
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.
Let's see the example of multi level inheritance in C#.
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...
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.
C# Aggregation Example
Let's see an example of aggregation where Employee class has the reference of
Address class as data member. In such way, it can reuse the members of Address
class.
1. using System;
2. public class Address
3. {
4. public string addressLine, city, state;
5. public Address(string addressLine, string city, string state)
6. {
7. this.addressLine = addressLine;
8. this.city = city;
9. this.state = state;
10. }
11.}
12. public class Employee
13. {
14. public int id;
15. public string name;
16. public Address address;//Employee HAS-A Address
17. public Employee(int id, string name, Address address)
18. {
19. this.id = id;
20. this.name = name;
21. this.address = address;
22. }
23. public void display()
24. {
25. Console.WriteLine(id + " " + name + " " +
26. address.addressLine + " " + address.city + " " + address.state);
27. }
28. }
29. public class TestAggregation
30. {
31. public static void Main(string[] args)
32. {
33. Address a1=new Address("G-13, Sec-3","Noida","UP");
34. Employee e1 = new Employee(1,"Sonoo",a1);
35. e1.display();
36. }
37. }
Output:
1 Sonoo G-13 Sec-3 Noida UP

C# Member Overloading
If we create two or more members having same name but different in number or
type of parameter, it is known as member overloading. In C#, we can overload:
o methods,
o constructors, and
o indexed properties
It is because these members have parameters only.
C# Method Overloading
Having two or more methods with same name but different in parameters, is
known as method overloading in C#.
The advantage of method overloading is that it increases the readability of the
program because you don't need to use different names for same action.
You can perform method overloading in C# by two ways:
1. By changing number of arguments
2. By changing data type of the arguments
C# Method Overloading Example: By changing no. of arguments
Let's see the simple example of method overloading where we are changing
number of arguments of add() method.
1. using System;
2. public class Cal{
3. public static int add(int a,int b){
4. return a + b;
5. }
6. public static int add(int a, int b, int c)
7. {
8. return a + b + c;
9. }
10.}
11.public class TestMemberOverloading
12.{
13. public static void Main()
14. {
15. Console.WriteLine(Cal.add(12, 23));
16. Console.WriteLine(Cal.add(12, 23, 25));
17. }
18.}
Output:
35
60
C# Member Overloading Example: By changing data type of arguments
Let's see the another example of method overloading where we are changing
data type of arguments.
1. using System;
2. public class Cal{
3. public static int add(int a, int b){
4. return a + b;
5. }
6. public static float add(float a, float b)
7. {
8. return a + b;
9. }
10.}
11.public class TestMemberOverloading
12.{
13. public static void Main()
14. {
15. Console.WriteLine(Cal.add(12, 23));
16. Console.WriteLine(Cal.add(12.4f,21.3f));
17. }
18.}
Output:
35
33.7

C# METHOD OVERRIDING
If derived class defines same method as defined in its base class, it is known as
method overriding in C#. It is used to achieve runtime polymorphism. It enables
you to provide specific implementation of the method which is already provided
by its base class.
To perform method overriding in C#, you need to use virtual keyword with
base class method and override keyword with derived class method.
C# Method Overriding Example
Let's see a simple example of method overriding in C#. In this example, we are
overriding the eat() method by the help of override keyword.
1. using System;
2. public class Animal{
3. public virtual void eat(){
4. Console.WriteLine("Eating...");
5. }
6. }
7. public class Dog: Animal
8. {
9. public override void eat()
10. {
11. Console.WriteLine("Eating bread...");
12. }
13.}
14.public class TestOverriding
15.{
16. public static void Main()
17. {
18. Dog d = new Dog();
19. d.eat();
20. }
21.}
Output:
Eating bread...

C# Base
In C#, base keyword is used to access fields, constructors and methods of base
class.
You can use base keyword within instance method, constructor or instance
property accessor only. You can't use it inside the static method.
C# base keyword: accessing base class field
We can use the base keyword to access the fields of the base class within
derived class. It is useful if base and derived classes have the same fields. If
derived class doesn't define same field, there is no need to use base keyword.
Base class field can be directly accessed by the derived class.
Let's see the simple example of base keyword in C# which accesses the field of
base class.
1. using System;
2. public class Animal{
3. public string color = "white";
4. }
5. public class Dog: Animal
6. {
7. string color = "black";
8. public void showColor()
9. {
10. Console.WriteLine(base.color);
11. Console.WriteLine(color);
12. }
13.
14.}
15.public class TestBase
16.{
17. public static void Main()
18. {
19. Dog d = new Dog();
20. d.showColor();
21. }
22.}
Output:
white
black
C# base keyword example: calling base class method
By the help of base keyword, we can call the base class method also. It is useful
if base and derived classes defines same method. In other words, if method is
overridden. If derived class doesn't define same method, there is no need to use
base keyword. Base class method can be directly called by the derived class
method.
Let's see the simple example of base keyword which calls the method of base
class.
1. using System;
2. public class Animal{
3. public virtual void eat(){
4. Console.WriteLine("eating...");
5. }
6. }
7. public class Dog: Animal
8. {
9. public override void eat()
10. {
11. base.eat();
12. Console.WriteLine("eating bread...");
13. }
14.
15.}
16.public class TestBase
17.{
18. public static void Main()
19. {
20. Dog d = new Dog();
21. d.eat();
22. }
23.}
Output:
eating...
eating bread...
C# inheritance: calling base class constructor internally
Whenever you inherit the base class, base class constructor is internally
invoked. Let's see the example of calling base constructor.
1. using System;
2. public class Animal{
3. public Animal(){
4. Console.WriteLine("animal...");
5. }
6. }
7. public class Dog: Animal
8. {
9. public Dog()
10. {
11. Console.WriteLine("dog...");
12. }
13.
14.}
15.public class TestOverriding
16.{
17. public static void Main()
18. {
19. Dog d = new Dog();
20.
21. }
22.}
Output:
animal...
dog...

C# Polymorphism
The term "Polymorphism" is the combination of "poly" + "morphs" which
means many forms. It is a greek word. In object-oriented programming, we use
3 main concepts: inheritance, encapsulation and polymorphism.
There are two types of polymorphism in C#: compile time polymorphism and
runtime polymorphism. Compile time polymorphism is achieved by method
overloading and operator overloading in C#. It is also known as static binding or
early binding. Runtime polymorphism in achieved by method overriding which
is also known as dynamic binding or late binding.
C# Runtime Polymorphism Example
Let's see a simple example of runtime polymorphism in C#.
1. using System;
2. public class Animal{
3. public virtual void eat(){
4. Console.WriteLine("eating...");
5. }
6. }
7. public class Dog: Animal
8. {
9. public override void eat()
10. {
11. Console.WriteLine("eating bread...");
12. }
13.
14.}
15.public class TestPolymorphism
16.{
17. public static void Main()
18. {
19. Animal a= new Dog();
20. a.eat();
21. }
22.}
Output:
eating bread...
C# Runtime Polymorphism Example 2
Let's see a another example of runtime polymorphism in C# where we are
having two derived classes.
1. using System;
2. public class Shape{
3. public virtual void draw(){
4. Console.WriteLine("drawing...");
5. }
6. }
7. public class Rectangle: Shape
8. {
9. public override void draw()
10. {
11. Console.WriteLine("drawing rectangle...");
12. }
13.
14.}
15.public class Circle : Shape
16.{
17. public override void draw()
18. {
19. Console.WriteLine("drawing circle...");
20. }
21.
22.}
23.public class TestPolymorphism
24.{
25. public static void Main()
26. {
27. Shape s;
28. s = new Shape();
29. s.draw();
30. s = new Rectangle();
31. s.draw();
32. s = new Circle();
33. s.draw();
34.
35. }
36.}
Output:
drawing...
drawing rectangle...
drawing circle...
Runtime Polymorphism with Data Members
Runtime Polymorphism can't be achieved by data members in C#. Let's see an
example where we are accessing the field by reference variable which refers to
the instance of derived class.
1. using System;
2. public class Animal{
3. public string color = "white";
4.
5. }
6. public class Dog: Animal
7. {
8. public string color = "black";
9. }
10.public class TestSealed
11.{
12. public static void Main()
13. {
14. Animal d = new Dog();
15. Console.WriteLine(d.color);
16.
17. }
18.}
Output:
white

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.
Note: Structs are implicitly sealed therefore they can't be inherited.
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.
18. }
19.}
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.
Let's see an example of sealed method in C#.
1. using System;
2. public class Animal{
3. public virtual void eat() { Console.WriteLine("eating..."); }
4. public virtual void run() { Console.WriteLine("running..."); }
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:
Compile Time Error: 'BabyDog.run()': cannot override inherited member
'Dog.run()' because it is sealed
Note: Local variables can't be sealed.
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);
9. }
10.}
Output:
Compile Time Error: Invalid expression term 'sealed'

You might also like