Lecture 05 - programming fundamentals
Lecture 05 - programming fundamentals
• namespace consoleprogram12
• {
• class overloading
• {
• public int x;
• public void calculate(int a, int b) => x = a + b;
• public void calculate(int a, int b, int c) => x = a + b + c;
• namespace consoleprogram13
• {
• class Rectangle
• {
• public double area, volume;
• public void calculate(double a, double b)
• {
• area = a * b;
• }
• public void calculate(double x, double y, double z)
• {
• volume = x * y * z;
• }
• public void display()
• {
• Console.WriteLine($" Area = {area}");
• Console.WriteLine($" Volume = {volume}");
• }
• }
• class Execute
• {
• static double length, width, height;
• static void Main(string[] args)
• {
• Console.WriteLine("Enter Length: ");
• length = Convert.ToDouble(Console.ReadLine());
• Console.WriteLine("Enter width: ");
• width = Convert.ToDouble(Console.ReadLine());
• Console.WriteLine("Enter height: ");
• height = Convert.ToDouble(Console.ReadLine());
• }
• }
• }
Inheritance
• Allows classes to inherit commonly used state and behaviour from other
classes.
• Ability to create a new class from an exiting class by extending it.
Bicycle
• namespace consoleprogram14
• {
• public class Father
• {
• public string surname { get; } = "Perera";
• protected string lapmodel { get; } = "HP";
• internal string house { get; } = "Beach house";
• string designation { get; } = "General Manager";
• }
Console program 14 – inheritance
• public class child:Father
• {
• public string name { get; } = "Yasiru";
• public string degree { get; } = "Computer Science";
• public void display()
• {
• Console.WriteLine($"Laptop Model: {lapmodel}");
• // Console.WriteLine(designation); cannot access because it
is a private variable
• }
• }
• internal class Program
• {
• static void Main(string[] args)
• {
• Console.WriteLine("Chile class propertise");
• var a = new child();
• Console.WriteLine($"Name: {a.name}");
• Console.WriteLine($"Degree: {a.degree}");
• Console.WriteLine($"Surname: {a.surname}");
• Console.WriteLine($"House : {a.house}");
• a.display();
• Console.WriteLine("Parent class propertise");
• var b = new Father();
• Console.WriteLine($"Surname: {b.surname}");
• Console.WriteLine($"House: {b.house}");
• Console.ReadKey();
• }
• }
• }
• Encapsulation is a process of binding the data
members and member functions into a single unit.
In c#, the class is the real-time example for
encapsulation because it will combine various types
of data members and member functions into a
single unit.
• Data hiding example, there is a class with a variable
and two functions. Here, the variable "num" is
private so, it can be accessed only by the members
of the same class, and it can't be accessed anywhere
else. Hence, it is unable to access this variable
outside the class, which is called data hiding.
• Child class override parent class method.
• Cannot change the number of parameters and type
of the parameters.
Overriding • Must need the same signature.
• Virtual and override key words are used for the
methods.
• Allow the function to be overridden in any derived
class
Virtual & • Virtual function has to implement both in the base
class and derived class.
override • If a method is virtual in the base class then provide
keyword the override keyword in the derived class
• Neither members fields nor static functions can
be declared as virtual.
Console program 15 – override
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;
• namespace consoleprogram15
• {
• public class Emp
• {
• public virtual void join()
• {
• Console.WriteLine("New employee joined.");
• }
• }
• public class Managers:Emp
• {
• public override void join()
• {
• Console.WriteLine("New employee joined as manager.");
• }
• }
• internal class Program
• {
• static void Main(string[] args)
• {
•
• var b = new Managers();
• b.join();
• Console.ReadKey();
• }
• }
• }
• Example: A child class Boy and a parent class
Human. The Boy class extends Human class. Both
the classes have a common method void eat() . Boy
class is giving its own implementation to the eat()
method or in other words it is overriding the eat()
method.
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;
• namespace consoleprogram16
• {
• public abstract class Abstractclass
• {
• public abstract void display();//abstract method with no implementation
• }
• public class Abssubclass:Abstractclass
• {
• public override void display() => Console.WriteLine("Printing");
• //overriding abstract method to implement
• }
• internal class Program
• {
• static void Main(string[] args)
• {
• //cannot create instance for abstract class
• /*var a = new Abssubclass();
• a.display();*/
• }
• class EditBox : control, IdataBound // inherit a class as well as interface
• {
• void IdataBound.bind(IdataBound binder)
• {
• //implementation
• }
• }
Console program 17 – interface
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;
• namespace consoleprogram17
• {
• interface Ijump
• {
• void jump();
• }
• class Car:Ijump
• {
• public void jump()
• {
• Console.WriteLine("Jump");
• }
• }
• internal class Program
• {
• static void Main(string[] args)
• {
• var x = new Car();
• x.jump(); // class object calls the method
• Console.ReadKey();
• }
• }
• }
Difference between interface and abstract
Interface Abstract
• Interface support multiple implementation • Abstract class does not support multiple
implementation
• Interface does not contain data member
• Interface does not contain constructors
• Abstract class contain data member
• Abstract class contain constructors
Thank you