Oop PDF
Oop PDF
PROGRAMMING - FUNDAMENTAL
CONCEPTS
BY LEONARDO HIDROVO
08-25-2014
CONTENTS
Inheritance allows child classes inherits the characteristics of existing parent class
• Attributes (fields and properties)
• Operations (methods)
Child class can extend the parent class
• Add new fields and methods
• Redefine methods (modify existing behavior)
A class can implement an interface by providing implementation for all its
methods
INHERITANCE – BENEFITS
Employee Student
+Company: String +School: String
+Salary: double
INHERITANCE IN .NET
class Creature
{
protected string Name { get; private set; }
private void Talk()
{
Console.WriteLine("I am creature ...");
}
protected void Walk()
{
Console.WriteLine("Walking ...");
}
}
class Mammal : Creature
{
// base.Talk() can be invoked here
// this.name can be read but cannot be modified here
}
IMPORTANT ASPECTS
Structures cannot be inherited
In C# there is no multiple inheritance
• Only multiple interfaces can be implemented
"Relevant" to what?
• ... relevant to the given project (with an eye to future reuse in similar projects)
• Abstraction = managing complexity
ABSTRACTION
Abstraction is something we do every day
• Looking at an object, we see those things about it that have meaning to us
• We abstract the properties of the object, and keep only what we need
• E.g. students get "name" but not "color of eyes"
Allows us to represent a complex reality in terms of a simplified model
Abstraction highlights the properties of an entity that we need and
hides the others
In .NET abstraction is achieved in several ways:
• Abstract classes
• Interfaces
• Inheritance
ENCAPSULATION
Virtual method is method that can be used in the same way on instances of
base and derived classes but its implementation is different
A method is said to be a virtual when it is declared as virtual
public virtual void GetDataContext()
Methods that are declared as virtual in a base class can be overridden using
the keyword override in the derived class
Using override we can modify a method or property
An override method provides a new implementation of a member inherited from
a base class
You cannot override a non-virtual or static method
The overridden base method must be virtual, abstract, or override
Polymorphism ensures that the appropriate method of the subclass is called
through its base class' interface
Polymorphism is implemented using a technique called late method binding
Exact method to be called is determined at runtime, just before performing the
call
Applied for all abstract / virtual methods
Note: Late binding is slower than normal (early) binding
COHESION AND COUPLING
Cohesion describes how closely all the routines in a class or all the code in a
routine support a central purpose
Cohesion must be strong
• Well-defined abstractions keep cohesion strong
Classes must contain strongly related functionality and aim for single purpose
Cohesion is a useful tool for managing complexity
double sideC =
Math.Pow(sideA, 2) + Math.Pow(sideB, 2)
- 2 * sideA * sideB * Math.Cos(angleAB);