1 8_oop
Jason Zhu edited this page 2021-02-23 13:57:37 +11:00

Module 8. Object-oriented Programming with C#

The Pillars of OOP

3 pillars

  • Encapsulation: hide details.
    • Methods; Properties; Access modifiers;
  • Inheritence: Reuse code
  • Polymorphism: Have object behave differently

Deriving from a Base Class

Reference:

Syntax:

public class ChildClass : ParentClass
{
    //...
}

Chaining Constructions

Reference:

When constructor of parent class has parameter, we can chain the constructor of child class using base(param1, ...)

Syntax:

    public class ParentClass
    {

        public ParentClass(string name)
        {
            Name = name;
        }
        public string Name
        {
            get;
            set;
        }
    }

    public class ChildClass : ParentClass
    {
        public ChildClass(string name) : base(name)
        {
            Name = name;
        }
    }

Deriving fromm System.Object

In .NET, everything is derived from System.Object class (even when you don't specify it when defining classes). We can view it by pressing F12.

This class has methods:

  • ToString
  • GetHashCode()
  • ...

All these methods will be inherited in its derived class (i.e. all classes)

Setting up a Scenario & Defining and Abstract Class

Reference:

vehicle is something that has various forms; two-wheeler, three-wheeler and four-wheeler and so on. So this is one example of polymorphism. Technically we can say that when a function shows different behaviors when we passed different types and number values, then it is called polymorphism. So behaving in different ways depending on the input received is known as polymorphism i.e. whenever the input changes, automatically the output or the behavior also changes.

Abstract class is prerequist of polymorphism

TODO: Understand polymorphism more deeply.

Defining an Interface

Reference:

.NET provides an alternative approach known as the interface to support the concept of multiple inheritances, (as .NET does not support by nature)

Interface force the class signatures, while ignoring implementation in details.

Declare interface

Using interface to declare an interface.

// SYNTAX:
public interface InterfaceName
{
    //only abstract members:
    // Abstract methods
    // Properties
    // Indexes
    // Events
}

e.g.

public interface Example
{
    void show();
}

Working with interface

Interface provides a forceful rule for classes to implementation. Hence, multi-inheritance is enabled

namespace InterfaceDemo
{
    public interface Area
    {
        void area(double a, double b);
    }
    class Rectangle : Area
    {
        public void area(double a, double b)
        {
            double areaRectangle;
            areaRectangle = a * b;
            Console.WriteLine("the area of rectangle is :" + areaRectangle);
        }
    }
    class Circle : Area
    {
        static double PI = 3.14;
        public void area(double a, double b)
        {
            double areaCircle;
            areaCircle = PI * a * a;
            Console.WriteLine("the area of Circle is :" + areaCircle);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Area a = new Rectangle();
            a.area(5, 6);
            a = new Circle();
            a.area(7, 0);
            Console.WriteLine("Press any key to exist.");
            Console.ReadKey();
        }
    }
}

Tips

  • Naming Convention: Interface is begin by I. e.g. IBook
  • Interfaces are far more commen than abstract class
  • When class inherit from interface, methods from interface must be implemented, even if it's abstract class, and implemented methods are virtual

Writing Grades to a File & Using IDisposable

Reference:

A Statistical Challenge & Refactoring Statistics

  • Copy & Paste multiple lines of code is not good. Stick with DRY
  • Important to separate signing from doing

Refactoring code require low coupling, high cohesion