OOP_week 9
OOP_week 9
Lap Content:
➢ Inheritance
➢ Encapsulation
➢ Polymorphism
Inheritance:
▪ Inheritance in C# allows one class (child/derived class) to inherit the members (fields,
properties, methods, and events) of another class (parent/base class).
▪ The constructor of derived class call the constructor of parent class the complete the code
of the derived class constructor
Example 1
Output:
Animal constructor called.
Dog constructor called.
1|P age
Explanation:
Parameter less parent constructor automatically called before constructor of child class
Example 2
Output:
Animal constructor called. Name: Buddy
Dog constructor called.
Explanation:
Because base class has a parameterized constructor the derived class constructor must explicitly call
Example 3
internal class Program
{
private static void Main(string[] args)
{
Dog dog1 = new Dog();
Console.WriteLine();
Dog dog2 = new Dog("Buddy");
}
}
class Animal
2|P age
{
public Animal()
{
Console.WriteLine("Default Animal constructor called.");
}
Output:
Default Animal constructor called.
Default Dog constructor called.
Explanation:
Multiple constructors may be called derived class constructor based on parameters passed
Encapsulation
▪ Encapsulation is the process of hiding the internal implementation details of a class and
exposing data only through defined methods (getters and setters).
▪ This concept enhances security, reusability, and maintainability of code in object-oriented
systems.
Attributes: Methods:
- Name: string + set_name (name:string): void
- Age: int + get_name(): string
+ set_age (age:int): void
+ get_age(): int 3|P age
+------------------------+
Class
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PracticeApp
{
class Program
{
static void Main(string[] args)
{
Person person = new Person();
class Person
{
private string name;
private int age;
4|P age
get { return name; }
}
}
}
}
Polymorphism:
• Polymorphism is one of the key principles of Object-Oriented Programming (OOP) that allows objects
to take on multiple forms. In C#, polymorphism is achieved through method overriding (runtime
polymorphism) and method overloading (compile-time polymorphism).
1- Method overloading :
o Creating multiple methods with the same name but different parameters (type, number, or order)
within the same class.
o perform similar tasks but with different inputs.
Example
internal class Program
{
private static void Main(string[] args)
{
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(5, 3)); // Output: 8
Console.WriteLine(calc.Add(5.2, 3.3)); // Output: 8.5
}
}
class Calculator
{
5|P age
public int Add(int a, int b)
{
return a + b;
}
2- Method overriding
o Redefining a method in a derived class that exists in the base class, using the same name and
parameters.
o provide a specific implementation of a method in the derived class.
o Method in parent class called virtual method: method declared in a base class using the virtual
keyword, and it is intended to be overridden by derived classes using the override keyword.
o The base class method must use virtual keyword
o The derived class method must use override keyword
Example
internal class Program
{
private static void Main(string[] args)
{
Animal animal = new Dog();
animal.Speak(); // Calls Dog's overridden method
}
}
class Animal
{
public virtual void Speak()
{
Console.WriteLine("The animal makes a sound.");
}
}
Example
class Program
{
static void Main(string[] args)
{
Computer pc = new Computer(16,256,"pc name",12);
pc.ram = 64;
pc.display();
Phone mobile = new Phone(16, 256, "pc name", "android");
mobile.type = "my phone";
mobile.display();
Console.Read();
}
}
class Computer:Device
{
public Computer(int ram, int rom, string name, int generation):base(ram,rom,name)
{
this.generation = generation;
}
public void display()
{
base.display();
Console.WriteLine("generation is "+generation);
}
public int generation;
}
class Phone : Device
{
public Phone(int ram, int rom, string name,string type):base(ram,rom,name)
{
this.type = type;
}
public string type
{
set
{
if (value.ToLower().Equals("android")|| value.ToLower().Equals("ios"))
{
Type = value;
}else
{
Console.WriteLine("only android and ios options is allowed");
}
}
get
{
return Type;
}
}
7|P age
string Type;
}
class Device
{
public Device(int ram, int rom, string name)
{
this.ram = ram;
this.rom = rom;
this.name = name;
}
8|P age