C OOPS Concepts in Depth With Sample Codes 1690341231
C OOPS Concepts in Depth With Sample Codes 1690341231
❑ Inheritance in C#
❑ Single Inheritance
❑ Multi level Inheritance
❑ Hierarchical Inheritance
❑ Multiple Inheritance
❑ Compile Time Polymorphism in C#
❑ Method/Operator Overloading in C#
❑ Run-time Polymorphism in C#
❑ Importance of base keyword in C#
❑ Abstraction in C#
❑ Abstract Class
❑ Abstract Method
❑ Non Abstract Method
❑ Encapsulation in C#
Learn C# or C-Sharp from Scratch
3) Hierarchical Inheritance:
One class serves as a superclass (base class) for more than one subclass.
Example:
Public class Animal{
//Overridden method
public void eat()
{
Console.WriteLine(“Animal is eating");
}}
Public class Tiger : Animal{
//Overriding method
public void eat(){
Console.WriteLine("Tiger is eating");
}
static void Main() {
Tiger obj = new Tiger();
//This will call the child class version of eat()
obj.eat();
}
} //Output: Tiger is eating
Importance of base keyword in C#
❑ The base keyword in C# is a reference variable which is used to refer immediate parent class
object.
❑ Whenever user create the instance of subclass, an instance of parent class is created implicitly
which is referred by base reference variable.
❑ base keyword can be used at variable, method and constructor level.
Example:
public class Animal{
public string name=“Elephant";
}
public class Dog : Animal{
public string name=“Bullet";
public void printName(){
Console.WriteLine(Name);//prints Name of Dog class
Console.WriteLine(base.Name);//prints Name of Animal class
}
}
static void Main() {
Dog d=new Dog();
d.printName();
}
Abstraction in C#
❑ Abstraction is a process of hiding implementation details and showing only
functionality to the user.
❑ In another way it shows important things to the user and hides internal details, for
example, sending SMS where user type the text and send the message. One don't
know the internal processing about the message delivery.
❑ Abstraction focuses on what the Object does instead of how it does. Abstraction can
be achieved with either abstract classes or interfaces.
❑ A class which is declared with the abstract keyword is known as an abstract class in
C#. It can have abstract and non-abstract methods (method with the body).
❑ It is not possible to create an object of the abstract class.
❑ The abstract keyword enables user to create classes and class members that are
incomplete and must be implemented in a derived class.
❑ The purpose of an abstract class is to provide a common definition of a base class
that multiple derived classes can share.
❑ Abstract methods have no implementation, so the method definition is followed by a
semicolon instead of a normal method block. Derived classes of the abstract class
must implement all abstract methods.
❑ User can not declare the abstract methods outside the abstract class.
Abstraction Example in C#
Example:
abstract class Animal
{
public abstract void eat();
public void drink()
{
Console.WriteLine("Animal used to drink Water");
}
}
class Tiger : Animal
{
public override void eat()
{
Console.WriteLine("Tiger used to eat meat");
}
}
obj.add(20, 25);
obj.add(10, 30, 50);
obj.add(200.34, 300.988);
obj.studentIdentity("Rahul", 2);
obj.studentIdentity(4, "Rajesh");
Console.WriteLine(obj.add(1, 2));
Console.WriteLine(obj.add("Hello ", "C#"));
C# Run Time Polymorphism Sample Code
public class Animal
{
public Animal() {
Console.WriteLine("Animal Class Constructor");
}
public string name ="Elephant";
//Overridden method
public void eat()
{
Console.WriteLine("Animal is eating");
}
}
public class Tiger : Animal
{
public Tiger() : base()
{
}
/*public Tiger()
{
Console.WriteLine("Tiger Class Constructor");
} */
public string name = "Royal Bengal Tiger";
//Overriding method
public void eat()
{
Console.WriteLine("Tiger is eating");
}
obj.printName();
C# Abstraction Sample Code
abstract class Animal
{
public abstract void eat();
public abstract void run(); //Create later after one run
public void drink()
{
Console.WriteLine("Animal used to drink Water");
}
}
class Tiger : Animal
{
public override void eat()
{
Console.WriteLine("Tiger used to eat meat");
}
public override void run()
{
Console.WriteLine("Tiger used to run fast");
}
}
class Student
{
private int StudentRollNo; //these private variables can only be accessed by public methods of class
private string studentName;
private int studentAge;
//Creating public Setter and Getter methods, This method is used to return the data stored in the StudentRollNo variable
public int sRollNo
{
get { return StudentRollNo; }
class Program
{
static void Main()
{
Student student = new Student();
//User can access the private variable via public setter and getter methods
student.sRollNo = 1;
student.sName = "Rahul";
student.sAge = 10;
Console.WriteLine("Student Roll No : " + student.sRollNo);
Console.WriteLine("Student Name : " + student.sName);
Console.WriteLine("Student Age : " + student.sAge);
//Console.WriteLine(student.StudentRollNo); //User cannot access the Private Variable directly and it will throw Compile Time Error
Console.ReadKey();
}
}