"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
Object oriented programming
1. OBJECT ORIENTED PROGRAMMING
OOP allows group of objects to cooperate with each other by exchanging data,
messages and logical framework to achieve a common or simple complex goal. Object-
oriented programming (OOP) is a programming paradigm that uses "objects" – data
structures consisting of datafields and methods together with their interactions – to
design applications and computer programs. Programming techniques may include
features such as data abstraction, encapsulation, modularity, polymorphism, and
inheritance. It was not commonly used in mainstream software application
development until the early 1990s. Many modern programming languages now support
OOP.
The Simula programming language was the first to introduce the concepts underlying
object-oriented programming (objects, classes, subclasses, virtual methods,
coroutines, and discrete event simulation) as a superset of Algol. Simula also used
automatic garbage collection which had been invented earlier for the functional
programming language Lisp. Simula was used for physical modeling, such as models to
study and improve the movement of ships and their content through cargo ports.
Smalltalk was the first programming language to be called "object-oriented".
Objects: Objects are software constructs that encapsulate data and allow the ability
to use, modify and share this data with other objects.
E.g.: public class Car {
public int wheels;
public string color;
public int noOfSeats;
}
Object Oriented Techniques:
a) Encapsulation: Encapsulation is the ability of an object to hide its data and methods
from the rest of the world. It is one of the fundamental principles of OOPs. A powerful
benefit of encapsulation is the hiding of implementation details from other objects.
This means that the internal portion (variables) of an object has more limited visibility
than the external portion (methods). This will protect the internal portion against
unwanted external access.
Properties Of Encapsulation:
The encapsulation principle leads us to typically store data in private fields and
provide access to this data through public accessor methods that allow us to
set and get values
So we have to create two methods such as GetData() and SetData()
C# provides a special property syntax that simplifies this process
Example1: Using Methods
public class Student {
private string name;
public string GetName() {
return name;
}
public void SetName(string n){
name = a;
}}
2. Example2: Using Property
public class Student {
private string name;
public string Name
{
get {
return name;
}
set {
name = value;
} }}
b) Polymorphism: Polymorphism means allowing a single definition to be used with
different types of data (specifically, different classes of objects). For example, a
polymorphic function definition can replace several type-specific ones, and a single
polymorphic operator can act in expressions of various types. Many programming
languages implement some forms of polymorphism. The concept of polymorphism
applies to data types in addition to functions. A function that can evaluate to and be
applied to values of different types is known as a polymorphic function. A data type
that contains elements of different types is known as a polymorphic data type.
Polymorphism may be achieved by overloading a function, overloading an operator,
changing the order of types, changing the types using the same name for the member
in context.
Example:
Public Class Calc {
public void fnMultiply(int x, int y)
{ return x * y; }
public void fnMultiply(int x, int y, int z)
{ return x * y * z; }
}
...
...
Calc obj;
int Result;
Result = obj.fnMultiply(2,3,4); // The second fnMultiply would be called
Result = obj.fnMultiply(3,4); // The first fnMultiply would be called
//Here, the call depends on the number of parameters passed, polymorphism is
achieved using overloading
c) Inheritence: Inheritance is the concept of passing the traits of a class to another
class. A class comprises of a collection of types of encapsulated instance variables and
types of methods, events, properties, possibly with implementation of those types
together with a constructor function that can be used to create objects of the class. A
class is a cohesive package that comprises of a particular kind of compile-time
metadata. A Class describes the rules by which objects behave; these objects are
referred to as "instances" of that class.
Is multiple inheritance possible in .NET?
No. Multiple inheritance is not possible in .NET. This means it is not possible for one
class to inherit from multiple classes. However, a class may implement multiple
interfaces. We may also declare objects of different classes in a class. This way, the
encapsulated class may be instantiated in other classes.
3. E.g.: class Vehicle {
private string _color;
public string Color {
get { return _color; }
set { _color = value; }
}
public virtual void Speed() {
Console.WriteLine ( 0); }
}
class Car : Vehicle {
public override void Speed() {
Console.WriteLine(120);
} }
class Plane : Vehicle {
public override void Speed()
{
Console.WriteLine(12000);
}
public void Fly (){
Console.WriteLine("Fly High in the Sky");
} }
class Cruise : Vehicle {
public override void Speed() {
Console.WriteLine(1000);
}
public void Sail()
{
Console.WriteLine("Float Float and Float");
} }
public class TestVehicle {
public static void Main() {
Vehicle v = new Car();
v.Color = "Red";
Console.WriteLine("Car Color {0}", v.Color);
v.Speed();
v = new Plane();
v.Color = "White";
Console.WriteLine("Plane Color {0}", v.Color);
v.Speed();
((Plane)v).Fly();
v = new Cruise();
v.Color = "Brown";
Console.WriteLine("cruise Color {0}", v.Color);
v.Speed();
((Cruise)v).Sail();
Console.ReadLine();
} }