What Is an Object?
An object is a software bundle of related state and behavior.
Real World Example:
Bicycles have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).
Software objects are conceptually similar to real-world objects: An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages).
Bundling code into individual software objects provides a number of benefits, including:
- Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
- Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
- Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
- Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
What Is a Class?
A class is a blueprint or prototype from which objects are created.
class Bicycle {
int speed = 0;
int gear = 1;
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
What Is Encapsulation ?
Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.
Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers:
- Public
- Private
- Protected
- Internal
- Protected internal
Example:
public void print(int i)
{
Console.WriteLine("Printing int: {0}", i );
}
{
Console.WriteLine("Printing int: {0}", i );
}
private void print(double f)
{
Console.WriteLine("Printing float: {0}" , f);
}
{
Console.WriteLine("Printing float: {0}" , f);
}
What Is Polymorphism ?
The word polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.
Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.
Example:
void print(int i)
{
Console.WriteLine("Printing int: {0}", i );
}
void print(double f){
Console.WriteLine("Printing int: {0}", i );
}
{
Console.WriteLine("Printing float: {0}" , f);
}
No comments:
Post a Comment