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

OOP_week 9

Uploaded by

moradalmaged
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

OOP_week 9

Uploaded by

moradalmaged
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CS 203 Object Oriented Programming Handout: lab 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

internal class Program


{
private static void Main(string[] args)
{
Dog dog = new Dog();
}
}
class Animal
{
public Animal()
{
Console.WriteLine("Animal constructor called.");
}
}

class Dog : Animal


{
public Dog()
{
Console.WriteLine("Dog constructor called.");
}
}

Output:
Animal constructor called.
Dog constructor called.

1|P age
Explanation:
Parameter less parent constructor automatically called before constructor of child class

Example 2

internal class Program


{
private static void Main(string[] args)
{
Dog dog = new Dog("buddy");
}
}
class Animal
{
public Animal(string name)
{
Console.WriteLine($"Animal constructor called. Name: {name}");
}
}

class Dog : Animal


{
public Dog(string name) : base(name)
{
Console.WriteLine("Dog constructor called.");
}
}

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.");
}

public Animal(string name)


{
Console.WriteLine($"Animal constructor called. Name: {name}");
}
}

class Dog : Animal


{
public Dog() : base()
{
Console.WriteLine("Default Dog constructor called.");
}

public Dog(string name) : base(name)


{
Console.WriteLine("Dog constructor called with a name.");
}
}

Output:
Default Animal constructor called.
Default Dog constructor called.

Animal constructor called. Name: Buddy


Dog constructor called with a name.

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.

Data Members Members functions

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();

// Setting Name and Age


person.Name="Omar;
person.Age=30;

// Getting Name and Age


Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
// Output: Name: Omar, Age: 30
}

class Person
{
private string name;
private int age;

// Set and Get proberty for Name


public string Name
{
set { name = value; }

4|P age
get { return name; }
}

// Set and Get proberty for Age


public int Age
{
set
{
if (value > 0)
age = value;
}

get { return age; }

}
}
}

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;
}

public double Add(double a, double 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.");
}
}

class Dog : Animal


{
public override void Speak()
{
6|P age
Console.WriteLine("The dog barks.");
}
}

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;
}

public int ram


{
set
{
Rom = rom % 8 == 0 ? rom : value;
}
get { return Ram; }
}
private int Ram;
private int Rom;
public int rom
{
set
{
Rom = rom % 8 == 0? rom:value;
}
get { return Rom; }
}
public string name;
public void display()
{
Console.WriteLine("ram is "+ram);
Console.WriteLine("rom is "+rom);
Console.WriteLine("name is "+name);
}
}

8|P age

You might also like