Back
Close

C# Refresh

Nonsultant
9,998 views
Previous: Welcome Next: Interfaces

Object Oriented Programming and C#

C# is a multi paradigm programming language, which is used in the Microsoft .NET framework, currently (March 2020) in version 8.0.

Object Oriented Programming (OOP) is a programming paradigm which is highly used in C#.

The definition of OOP

Objects are simply experts. You tell them what you need and they get it done. Forget all of the handwaving about blueprints or "data with behaviors". Those are implementation details. And once you start thinking about objects as simply experts about a particular problem domain, OOP becomes much easier.

Quote from: Alan Kay and OO Programming

It's important note that there is kind of a gap between the original definition of Object Oriented Programming keyed by Alan Kays in the 60s and the common definition used today. In this course will the definition of OOP be centred around the typical modern definition talking about blueprints/templates as it's the current standard in the industry.

Steve Jobs explains object oriented programming

This is part of a 1994 interview with Steve Jobs from the The Rolling Stone:

Interviewer:

Would you explain, in simple terms, exactly what object-oriented software is?

Steve Jobs:

Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.

Here’s an example: If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.

Programming concepts in OOP

Especially four programming concepts are important to understand when programming using OOP : Encapsulation, Inheritance, Polymorphism and Abstraction. These concepts often goes hand in hand, and can be hard to separate.

Encapsulation

What is the goal of encapsulation within OOP?

Example of encapsulation written in C#. The class Counter, have the inner state counterValue which is hidden for the outside and can only be modified by the methods CountUp and CountDown.

Inheritance

What is the goal of inheritance within OOP?

Example of inheritance, the species dog and human is in the functionality of the mammal, and modifies the two parameters legs and specie.

Polymorphism

What can you do with polymorphism within OOP?

Example of polymorphism, the class Polymorph implements three different (polymorph) versions of the method Print. Type of input decides which of the implementations is executed.

Abstraction

What is the goal of abstraction within OOP?

Example of abstraction, the class mammal can't be instantiated since it have the modifier abstract, only the class Dog and Human can be instantiated, all functionality data which is generic for mammals are hidden in the Mammal class. The abstract method MakeASound needs to be implemented in all classes which inherits from Mammal.

OOP Elements in C#

An object is an entity that encapsulates state and behaviour. The state of an object is the current characteristics it possesses, for example, if a car is an object, one of its characteristics or properties may be how many people are in the car. A more static characteristic is the colour of the car.

The behaviour of an object is represented by the possible actions or methods that the object can perform. In our automobile example, a car can start its engine, or shift into drive gear. A car can stop, make a turn, turn on its wipers, etc.

An object can also inherit properties and methods from other types of objects (via class templates). For example, the car object can be considered a specialized form of “vehicle” which possesses more general properties and methods. A truck can inherit from the vehicle class. So can a farm tractor or an airplane or a train or a boat.

Quote from: How can I develop object oriented thinking in programming?

Objects are instances of classes. In other words, an instance of a class is an object defined by that particular class. Creating a new instance (in C# using the new term), or an object, is called instantiation.

A class can be seen as a "template" or "blueprint" from which objects will be instantiated. It’s like the mould from which objects can be created/instantiated from. A class represents a real life concept such as a person, an animal, a car, a house or a football.

A class consists of a collection of states (a.k.a. variables or properties) and behaviours (a.k.a. methods).

An object is an instance of a class. The attributes of an object as stored using variables for storing data and the behaviour/methods of an object are implemented as functions or procedures and can be used to perform operations on the data.

Class

A representation of a collection of similar objects. Classes are often physical objects, representing a person, place, or thing. A class could be seen as a template of data with a given behaviour.

In C# we use the word class to define a class, example:

class Car{
	int numberOfWheels;	
}

Read more: Classes (C# Programming Guide)

Object

An instance of a particular class. While a class may represent people, an object represents one particular person.

In C# we create an instance using the term new, example:

// instanciates two car obejcts
var car1 = new Car();
var car2 = new Car();	

Read more: Objects (C# Programming Guide)

Abstract Class

A class that can never instantiated into an object.

In C# we use abstract class to mark a class as abstract.

abstract class Vehicle{
	public abstract void Start();
}

class Car : Vehicle{
    int numberOfWheels;
    public overrides void Start(){
        // logic to start the car
    }
}

Read more: abstract (C# Reference)

Properties

An interesting property of a class. Usually in the form of a value.

In C# these are defined much the same way as a method:

class Car{
	int NumberOfWheels{ get; set; };	
}

Read more: Properties

Often these is used to access a hidden/encapsulated variable.

class Car{
    private int numberOfWheels;
    
    // The property makes it only possible to retrive the number of wheels, not modify it this increases the encapsulation
	public int NumberOfWheels{ 
		get {
			return this.numberOfWheels;
		}
	};	
}

Design principles

I the following lecture will we look more into OOP design principles, but the there is two basic design principles of OOP which is important to look into in beforehand and that is coupling and cohesion.

What is our goal as developers when developing OO code?

Object oriented thinking

There is a lot of tools to support the development of OOP applications, one of the most wide spread is UML.

In this course are we especially going to use Use case diagrams which is good to identify the users of the system and the functionality the need.

Besides this are we also going to use the technique CRC cards, which can be used for object oriented thinking is the : Introduction to CRC Cards

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants