SAD 03 Object Oriented Concepts
SAD 03 Object Oriented Concepts
SAD 03 Object Oriented Concepts
– Class:
Template to define specific instances or objects
– Object:
Instance of a class
Classes & Objects
Class
RAM
C#
}
Creating Objects
Java
Patient p2;
p2 = new Patient();
C#
Patient p2;
p2 = new Patient();
Classes & Objects
Patient
RAM
p1 p2
Object Attributes
• Each object has attributes that describe
information about the object.
C#
C#
C#
...
Patient p = new Patient();
...
p.MakeAppointment();
...
Encapsulation & Information Hiding
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
Human Being
Asian American
Indian Pakistani
Punjabi Sindhi
Inheritance
• Subclasses inherit the appropriate attributes
and methods from the superclasses above
them.
C#
C#
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