Docsity

Basic Interfaces and Polymorphisms, Slides of Mathematics

Partial preview of the text

Download Basic Interfaces and Polymorphisms and more Slides Mathematics in PDF only on Docsity!

Basic Interfaces and

Polymorphisms

  • Abstraction
  • Interfaces
  • Polymorphism
    • Definition
    • Types

Table of Contents

  • From the Latin
  • Preserving information, relevant in a given context, and forgetting information that is irrelevant in that context

What is Abstraction?

Abs (away from) Trahere (to draw) Abstraction

  • Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the ones โ€ฆ
  • ... relevant to the context of the project we develop
  • Abstraction helps managing complexity
  • Abstraction lets you focus on what the object does instead of how it does it

Abstraction in OOP

"Relevant" to what?

WORKING WITH INTERFACES

Interfaces

  • Internal addition by compiler

Interface

public interface IPrintable { void Print(); } public interface IPrintable { public abstract void Print(); } compiler Keyword Name

  • Contains only the signatures of methods, properties, events or indexers
  • Can inherit one or more base interfaces
  • When a base type list contains a base class and interfaces, the base class must come first in the list
  • A class that implements an interface can explicitly implement members of that interface - An explicitly implemented member cannot be accessed through

a class instance, but only through an instance of the interface

Interface (2)

  • Relationship between classes and interfaces
  • Multiple inheritance

Multiple Inheritance

Interface

extends implements extends

Interface

Interface

Class Class

Class

Class

Interface Interface Interface Interface

Interface

implements^ extends

Solution: Shapes

public interface IDrawable { void Draw(); } public class Circle : IDrawable { // TODO: Add fields and a constructor public void Draw() { // TODO: implement } } public class Rectangle : IDrawable { // TODO: Add fields and a constructor public void Draw() { // TODO: implement } }

Solution: Shapes โ€“ Rectangle Draw

public void Draw() { DrawLine(this.width, '', ''); for (int i = 1; i < this.height - 1; ++i) DrawLine(this.width, '', ' '); DrawLine(this.width, '', '*'); } private void DrawLine(int width, char end, char mid) { Console.Write(end); for (int i = 1; i < width - 1; ++i) Console.Write(mid); Console.WriteLine(end); }

POLYMORPHISM

  • From the Greek
  • This is something similar to a word having several

different meanings depending on the context

  • Polymorphism is often referred to as the third pillar of

object-oriented programming, after encapsulation and

inheritance

What is Polimorphism?

Polys (many) Morphe (shape/forms) Polymorphos

  • Also known as Static Polymorphism
  • Argument lists could differ in:
    • Number of parameters
    • Data type of parameters
    • Order of parameters

Compile Time Polymorphism

public static void Main() { static int MyMethod(int a, int b) {} static double MyMethod(double a, double b) { โ€ฆ } } Method overloading

Problem: MathOperation

MathOperation +Add(int, int): int +Add(double, double, double): double +Add(decimal, decimal, decimal): decimal MathOperations mo = new MathOperations(); Console.WriteLine(mo.Add(2, 3)); Console.WriteLine(mo.Add(2.2, 3.3, 5.5)); Console.WriteLine(mo.Add(2.2m, 3.3m, 4.4m));