Object-Oriented Programming with C#: Week 4 Lesson - Inheritance and Interfaces, Essays (high school) of Computer science

A part of Week 4 lesson from a C# programming course, focusing on Object-Oriented Programming (OOP) concepts, specifically Inheritance and Interfaces. It covers designing custom classes, creating constructors, implementing properties, and using indexers. It also discusses object creation, destruction, and garbage collection, as well as the use of interfaces for decoupling and design by contract.

Typology: Essays (high school)

2018/2019

Uploaded on 03/03/2022

unknown user
unknown user 🇬🇧

8 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
5. OOP
2003
© 2003 Microsoft
1
OOPS
Week 4
Lesson Objectives
Carrying out practical on Inheritance
Carrying out practical on Interface
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Object-Oriented Programming with C#: Week 4 Lesson - Inheritance and Interfaces and more Essays (high school) Computer science in PDF only on Docsity!

OOPS

Week 4

Lesson Objectives

  • Carrying out practical on Inheritance
  • Carrying out practical on Interface

Objectives

“Classes, objects and object-oriented programming (OOP) play a fundamental role in .NET. C# features full support for the object- oriented programming paradigm…”

  • Designing your own classes
  • Destroying objects and garbage collection
  • Inheritance
  • Interfaces

Part 1

  • Designing your own classes…

Properties

  • Goal:
    • to allow our class users to safely write code like this:
    • provides field-like access with method-like semantics…
    • … enabling access control, validation, data persistence, screen updating, etc. Person p; p = new Person("joe hummel", 40); p.Age = p.Age + 1;

Observation

  • Read of value ("Get") vs. Write of value ("Set") Person p; p = new Person("joe hummel", 40); p.Age = p.Age + 1; Get age Set age

Property implementation

  • Implementation options:
    • read-only
    • write-only
    • read-write public class Person { private string m_Name; private int m_Age; . . . public string Name { get { ... } } public int Age { get { ... } set { ... } } } read-only read-write

Example

  • Simplest implementation just reads / writes private field: public class Person { private string m_Name; private int m_Age; . . . public string Name // Name property { get { return this.m_Name; } } public int Age // Age property { get { return this.m_Age; } set { this.m_Age = value; } } }

Part 2

  • Destroying objects and garbage collection…

Object creation and destruction

  • Objects are explicitly created via new
  • Objects are never explicitly destroyed!
    • .NET relies upon garbage collection to destroy objects
    • garbage collector runs unpredictably…

Finalization

  • Objects can be notified when they are garbage collected
  • Garbage collector (GC) will call object's finalizer public class Person { . . . ~Person() // finalizer { ... }

Should you rely upon finalization?

  • No!
    • it's unpredictable
    • it's expensive (.NET tracks object on special queue, etc.)
  • Alternatives?
    • design classes so that timely finalization is unnecessary
    • provide Close / Dispose method for class users to call **** Warning **** As a .NET programmer, you are responsible for calling Dispose / Close. Rule of thumb: if you call Open, you need to call Close / Dispose for correct execution. Common examples are file I/O, database I/O, and XML processing.

Implementation

  • C# supports single inheritance
    • public inheritance only (C++ parlance)
    • base keyword gives you access to base class's members public class Student : Person { private int m_ID; public Student(string name, int age, int id) // constructor :base(name, age) { this.m_ID = id; } } Student Person

Binding

  • C# supports both static and dynamic binding
    • determined by absence or presence of virtual keyword
    • derived class must acknowledge with new or override public class Person { .. . // statically-bound public string HomeAddress() { … } // dynamically-bound public virtual decimal Salary() { … } } public class Student : Person { . . . public new string HomeAddress() { … } public override decimal Salary() { … } }

All classes inherit from System.Object

String Array ValueType Exception Delegate Class Multicast Delegate Class Class Object Enum Primitive types Enum Structure Boolean Byte Int Int Int Char Single Double Decimal DateTime System-defined types User-defined types Delegate TimeSpan Guid

Part 4

  • Interfaces…

.NET is heavily influenced by interfaces

  • IComparable
  • ICloneable
  • IDisposable
  • IEnumerable & IEnumerator
  • IList
  • ISerializable
  • IDBConnection, IDBCommand, IDataReader
  • etc.

Example

  • Sorting
    • FCL contains methods that sort for you
    • sort any kind of object
    • object must implement IComparable object[] students; students = new object[n]; students[0] = new Student(…); students[1] = new Student(…); . . . Array.Sort(students); public interface IComparable { int CompareTo(object obj); }

To be a sortable object…

  • Sortable objects must implement IComparable
  • Example:
    • Student objects sort by id public class Student : Person, IComparable { private int m_ID; . . . int IComparable.CompareTo(Object obj) { Student other; other = (Student) obj; return this.m_ID other.m_ID; } } base class interface Student Person

Summary

  • Object-oriented programming is the paradigm of .NET
  • C# is a fully object-oriented programming language
    • fields, properties, indexers, methods, constructors
    • garbage collection
    • single inheritance
    • interfaces
  • Inheritance?
    • consider when class A "is-a" class B
    • but you only get single-inheritance, so make it count
  • Interfaces?
    • consider when class C interacts with classes D, E, F, …
    • a class can implement any number of interfaces