SAD 03 Object Oriented Concepts

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

OBJECT ORIENTED CONCEPTS

Object Oriented System


• Object Oriented Systems focus on capturing
the structure and behavior of information
systems in little modules that encompass
both data and process. These little modules
are known as objects.
Basic Characteristics
• Basic Characteristics of “Object Oriented
Systems” include:

– Classes & Objects


– Methods & Messages
– Encapsulation & Information Hiding
– Inheritance
– Polymorphism & Dynamic Binding
Classes & Objects
• A class is the general template we use to
define and create specific instances, or
objects. An object is an instance of a class.

– Class:
Template to define specific instances or objects
– Object:
Instance of a class
Classes & Objects

Class

RAM

Obj1 Obj2 Obj3


Creating Classes
Java

public class Patient


{

C#

public class Patient


{

}
Creating Objects
Java

Patient p1 = new Patient();

Patient p2;
p2 = new Patient();

C#

Patient p1 = new Patient();

Patient p2;
p2 = new Patient();
Classes & Objects

Patient

RAM

p1 p2
Object Attributes
• Each object has attributes that describe
information about the object.

– also used to represent relationships between


objects
– state of an object is defined by the value of its
attributes and its relationships with other objects
at a particular point in time
Attributes
Java

public class Patient


{
String name;
String address;
String status;
}

C#

public class Patient


{
string name;
string address;
string status;
}
Object Behaviors
• Each object also has behaviors. The behaviors
specify what the object can do.

– In object-oriented programming, behaviors are


implemented as methods
Behaviors
Java

public class Patient


{
public void MakeAppointment()
{ ... }
}

C#

public class Patient


{
public void MakeAppointment()
{ ... }
}
Classes & Objects
Methods & Messages
• Methods implement an object’s behavior.
– A method is nothing more than an action that an
object can perform.

• Message is essentially a function or


procedure call from one object to another
object.
Methods & Messages
C#

public class Patient


{
...
public void MakeAppointment()
{ ... }
...
}

C#

...
Patient p = new Patient();
...
p.MakeAppointment();
...
Encapsulation & Information Hiding

• Encapsulation is simply the combination of


process and data into a single entity.

– You achieve this by creating objects


Encapsulation & Information Hiding

• The principle of information hiding suggests


that only the information required to use a
software module be published to the user of
the module.

– Objects are treated like black boxes


– This is key to reusability because it shields the internal workings of
the object from changes in the outside system, and it keeps the
system from being affected when changes are made to an object.
Encapsulation Using Type Properties
public class Employee
{
private int empID;

public int ID
{
get { return empID; } Property
set { empID = value; }
}
...
}
Access Modifiers
Modifier Scope
Members can be accessed from any member of
public any class.
Members can only be accessed by the same class
private members.
Members can only be accessed by the same class
protected members and child class members.
Members can only be accessed from any class
internal member within a same assembly.
Combination of protected and internal - means
protected protected or internal (i.e., the member is available
internal to all child classes as well as all classes in the
same assembly).
Controlling Visibility Levels of Property
public class Employee
{
private int empID;

public int ID
{
get { return empID; }
protected set { empID = value; }
}
...
}
Method Overloading

• Overloading is the process to define more


than one methods with the same name.
Method Overloading

Rule 1: Data types are different

public void Add( int a, int b )


{
...
}
public void Add( double a, double b )
{
...
}
Method Overloading

Rule 2: Number of parameters are different

public void Add( int a, int b )


{
...
}
public void Add( int a, int b, int c )
{
...
}
Method Overloading

Rule 3: Arrangement of parameters are different

public void Add( int a, double b )


{
...
}
public void Add( double a, int b )
{
...
}
Inheritance
• The basic idea behind inheritance is that new
classes may leverage (and possibly extend)
the functionality of existing classes.

• Typically, classes are arranged in a hierarchy,


whereby the superclasses, or general classes,
are at the top, and the subclasses, or specific
classes, are at the bottom.
Inheritance

Human Being

Asian American

Indian Pakistani

Punjabi Sindhi
Inheritance
• Subclasses inherit the appropriate attributes
and methods from the superclasses above
them.

• Inheritance in OOP facilitates code reuse.


Inheritance
Java

public class Pakistani


{}

public class Sindhi extends Pakistani


{}

C#

public class Pakistani


{}

public class Sindhi : Pakistani


{}
Inheritance
Inheritance
• The relationship between the class and its
superclass is known as the a-kind-of (AKO)
relationship.
• Any class that has instances is called a
concrete class.
• Classes that are used merely as templates for
other more specific classes are referred to as
abstract classes.
Referencing Parent
Java

super.Display(); //Method Calling

C#

base.Display(); //Method Calling


Preventing Inheritance
Java

public final class Sindhi extends Pakistani


{}
C#

public sealed class Sindhi : Pakistani


{}

In C# you use sealed keyword, that prevents


inheritance from occurring. When you mark a class as
sealed, the compiler will not allow you to derive from
this type.
Prevent Direct Instantiation
C#

public abstract class Asian


{}

In C#, you can programmatically prevent creating of


objects by using the abstract keyword, thus creating
an abstract base class.
Polymorphism & Dynamic Binding
• Polymorphism means that the same
message can be interpreted differently by
different classes of objects.
Polymorphism
Method Overriding
public class Shape
{
public virtual void DrawYourself()
{...}
}

public class Square : Shape


{
public override void DrawYourself()
{...}
}
Method Overriding
public class Shape
{
public virtual void Draw()
{...}
}

public class Rectangle : Shape


{
public override void Draw()
{...}
}
Polymorphism & Dynamic Binding
• Dynamic, or late, binding is a technique that delays
typing the object until run-time. As such, the
specific method that is actually called is not chosen
by the object-oriented system until the system is
running.

• In static binding, the type of object is determined at


compile time. Therefore, the developer has to
choose which method should be called instead of
allowing the system to do it.
Dynamic Binding
Shape s; Shape
Rectangle r = new Rectangle( );
Draw()
Oval o = new Oval( );
Paint()

Rectangle Oval

Draw() Draw()
Paint() Paint()

s = r; s = o;
s.Draw( ); s.Draw( );
Pillars of OOP
• All object-based languages must contend with three core
principals of object-oriented programming, often called the
“pillars of object-oriented programming (OOP)”:
– Encapsulation:
• How does this language hide an object’s internal
implementation details and preserve data integrity?
– Inheritance:
• How does this language promote code reuse?
– Polymorphism:
• How does this language let you treat related objects
in a similar way?
Key Terms

Abstract classes Encapsulation Object


A-kind-of Information hiding Polymorphism
Attribute Inherit State
Behavior Inheritance Static binding
Class Instance Subclass
Concrete classes Message Superclass
Dynamic binding Method

You might also like