Confidential - Oracle Restricted
OOPs Concept
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of
objects. It organizes software design around data, or objects, rather than functions and
logic. Objects are instances of classes, which can contain both data (attributes/properties)
and methods (functions/behavior). OOP helps structure code in a way that is modular,
scalable, and easy to maintain
Key Concepts of OOP in C#
1. Classes and Objects
2. Encapsulation
3. Abstraction
4. Inheritance
5. Polymorphism
1. Classes and Objects
• Class: A class is a user-defined data type that acts as a blueprint for creating
objects
• Purpose
Classes are used to organize information, create and manage objects, and
reuse code.
• Example
For example, if you want to make multiband cars, you can create a class called
"car" that stores similar information about cars, such as their properties and
attributes.
• Structure
A class is defined using the class keyword, followed by the class name and
curly braces. All the properties and methods of the class are defined inside the
curly braces.
Confidential - Oracle Restricted
Confidential - Oracle Restricted
Objects
An object is an instance of a class, meaning it contains real values instead of
variables. Objects inherit all the properties and behaviors from the class, but
each object has different values for the properties.
Example:
// Class definition
public class Car
{
// Properties
public string Brand { get; set; }
public string Model { get; set; }
public int Year { get; set; }
// Method
public void StartEngine()
{
Console.WriteLine("The engine is started.");
}
}
// Creating an object
Car myCar = new Car();
myCar.Brand = "Toyota";
myCar.Model = "Corolla";
myCar.Year = 2022;
myCar.StartEngine(); // Output: The engine is started.
Here's an explanation of the code provided:
1. Class Definition (Car):
• A class is a blueprint for creating objects. In this case, the Car class is defined as a
blueprint for car objects.
2. Properties:
• Properties are used to store data related to the object.
o public string Brand { get; set; } allows Brand to be read and assigned outside
the class.
Confidential - Oracle Restricted
Confidential - Oracle Restricted
o public string Model { get; set; } allows Model to be read and assigned outside
the class.
o public int Year { get; set; } allows Year to be read and assigned outside the
class.
• The get and set keywords make these properties auto-implemented properties with
automatic backing fields.
3. Method (StartEngine):
• A method is a function defined within a class that describes an action the object
can perform.
• public void StartEngine() is a method that prints "The engine is started." when called.
4. Creating an Object:
• Car myCar = new Car(); creates an instance of the Car class called myCar.
• This object myCar has its own Brand, Model, and Year properties that can be set or
modified.
5. Assigning Property Values:
• myCar.Brand = "Toyota"; sets the Brand property of myCar to "Toyota".
• myCar.Model = "Corolla"; sets the Model property of myCar to "Corolla".
• myCar.Year = 2022; sets the Year property of myCar to 2022.
6. Calling the Method:
• myCar.StartEngine(); calls the StartEngine() method on the myCar object.
• The output of calling this method is: The engine is started.
How This Code Works:
• This program defines a Car class with three properties (Brand, Model, and Year) and
a method (StartEngine()).
• An instance of Car is created and its properties are set.
• The StartEngine() method is then called on the myCar object, triggering the console
to display "The engine is started."
Confidential - Oracle Restricted
Confidential - Oracle Restricted
2. Encapsulation
In object-oriented programming (OOP), encapsulation is the process of bundling
data and the methods that operate on it into a single unit or class. This concept
helps protect the integrity of the data by restricting direct access to some of the
object's components.
Example program:
using System;
public class Car
{
// Private field (data is hidden)
private string model;
// Public method to set the value of the private field
public void SetModel(string carModel)
{
model = carModel;
}
// Public method to get the value of the private field
public string GetModel()
{
return model;
}
}
public class Program
{
public static void Main(string[] args)
{
Car myCar = new Car();
// Setting the model using the public method
myCar.SetModel("Honda Civic");
// Getting and displaying the model using the public method
Console.WriteLine("Car model: " + myCar.GetModel()); // Output: Car
model: Honda Civic
}
}
Confidential - Oracle Restricted
Confidential - Oracle Restricted
This code demonstrates Encapsulation in C#. It hides the internal data of the Car class and
exposes only specific methods to interact with it. Let's break it down:
1. Class Definition (Car):
public class Car
{
// Private field (data is hidden)
private string model;
}
• Class: The Car class represents a blueprint for creating car objects.
• Private field: private string model; is a field that holds the model of the car. It is private,
meaning it cannot be accessed directly from outside the class. This is a key principle of
encapsulation—keeping the internal data safe from direct modification.
2. Public Methods:
// Public method to set the value of the private field
public void SetModel(string carModel)
{
model = carModel;
}
// Public method to get the value of the private field
public string GetModel()
{
return model;
}
• SetModel: This is a public method that allows external code to set the value of the model
field. It takes a string parameter carModel and assigns it to the private model field. By using
a method like this, we control how the model is set (e.g., we can add validation or logic if
needed in the future).
• GetModel: This is a public method that allows external code to retrieve the value of the
model field. It simply returns the value stored in model.
These methods serve as the only way to access or modify the model field, thus
implementing data hiding (a core concept of encapsulation).
3. Program Execution (Main Method):
public class Program
{
public static void Main(string[] args)
{
Car myCar = new Car();
Confidential - Oracle Restricted
Confidential - Oracle Restricted
// Setting the model using the public method
myCar.SetModel("Honda Civic");
// Getting and displaying the model using the public method
Console.WriteLine("Car model: " + myCar.GetModel()); // Output: Car model: Honda
Civic
}
}
• Creating an Object (myCar): Car myCar = new Car(); creates a new instance of the Car
class. myCar is now an object of the Car class.
• Setting the Model: myCar.SetModel("Honda Civic"); calls the SetModel method to set the
model field of myCar to "Honda Civic". This allows the model field to be modified, even
though it is private, by using the public method SetModel.
• Getting the Model: Console.WriteLine("Car model: " + myCar.GetModel()); calls the
GetModel method to retrieve the value of model and print it to the console. The output will
be:
Car model: Honda Civic
Confidential - Oracle Restricted
Confidential - Oracle Restricted
3. Abstraction
Abstraction is the concept of hiding the complex implementation details and
showing only the necessary features. This allows users to interact with the object
through a simple interface.
Example Program
using System;
// Abstract class representing a generic vehicle
public abstract class Vehicle
{
// Abstract method (no implementation, must be implemented by
derived classes)
public abstract void StartEngine();
// Concrete method (has implementation)
public void DisplayType()
{
Console.WriteLine("This is a vehicle.");
}
}
// Derived class that implements the abstract method
public class Car : Vehicle
{
// Implementing the abstract method
public override void StartEngine()
{
Console.WriteLine("The car engine starts with a key or
button.");
}
}
public class Program
{
public static void Main(string[] args)
{
// Creating an object of the Car class
Vehicle myCar = new Car();
// Using the methods
myCar.DisplayType(); // Output: This is a vehicle.
myCar.StartEngine(); // Output: The car engine starts with a
key or button.
}
}
Confidential - Oracle Restricted
Confidential - Oracle Restricted
Explanation:
• Abstract Class: Vehicle is an abstract class that cannot be instantiated directly. It
contains an abstract method StartEngine() without an implementation.
• Concrete Method: The DisplayType() method has an implementation and can be
used as-is.
• Derived Class: The Car class inherits from Vehicle and provides its own
implementation of the StartEngine() method.
• Abstraction: The Vehicle class hides the complex details of how StartEngine() works
and only requires derived classes to define the specifics. This allows focusing on the
essential operations without worrying about the detail
Confidential - Oracle Restricted
Confidential - Oracle Restricted
4. Inheritance
Inheritance is the concept that allows a class (child class) to inherit properties and
methods from another class (parent class). This promotes code reusability.
using System;
// Base class representing a generic vehicle
public class Vehicle
{
public void Start()
{
Console.WriteLine("The vehicle starts.");
}
public void Stop()
{
Console.WriteLine("The vehicle stops.");
}
}
// Derived class representing a specific type of vehicle: Car
public class Car : Vehicle
{
public void Honk()
{
Console.WriteLine("The car honks: Beep! Beep!");
}
}
public class Program
{
public static void Main(string[] args)
{
// Creating an object of the Car class
Car myCar = new Car();
// Using methods from both the base (Vehicle) and derived (Car)
class
myCar.Start(); // Output: The vehicle starts. (inherited from
Vehicle)
myCar.Honk(); // Output: The car honks: Beep! Beep! (specific
to Car)
Confidential - Oracle Restricted
Confidential - Oracle Restricted
myCar.Stop(); // Output: The vehicle stops. (inherited from
Vehicle)
}
}
Explanation:
• Base Class (Vehicle): This class contains methods Start() and Stop(), which are common
to all vehicles.
• Derived Class (Car): This class inherits the properties and methods of Vehicle and adds
its own method Honk().
• Inheritance: The Car class can use the Start() and Stop() methods from the Vehicle class
and its own Honk() method.
• Output:
o myCar.Start() and myCar.Stop() use methods from the Vehicle class.
o myCar.Honk() uses the method specific to the Car class.
This demonstrates how the Car class can extend the behavior of the Vehicle class using
inheritance.
Confidential - Oracle Restricted
Confidential - Oracle Restricted
5.Polymorphism
Polymorphism means "many forms" and allows methods to do different things
based on the object it is acting upon. This can be achieved through method
overloading (compile-time polymorphism) and method overriding (runtime
polymorphism).
using System;
// Base class
public class Vehicle
{
// Virtual method that can be overridden in derived classes
public virtual void StartEngine()
{
Console.WriteLine("The vehicle's engine starts.");
}
}
// Derived class
public class Car : Vehicle
{
// Overriding the base class method
public override void StartEngine()
{
Console.WriteLine("The car's engine starts with a key or
button.");
}
}
// Another derived class
public class ElectricCar : Vehicle
{
// Overriding the base class method
public override void StartEngine()
{
Console.WriteLine("The electric car's engine starts
silently.");
}
}
public class Program
{
Confidential - Oracle Restricted
Confidential - Oracle Restricted
public static void Main(string[] args)
{
// Using polymorphism: a base class reference pointing to
derived class objects
Vehicle myVehicle = new Vehicle();
Vehicle myCar = new Car();
Vehicle myElectricCar = new ElectricCar();
// Each StartEngine() call uses the overridden version in the
actual object type
myVehicle.StartEngine(); // Output: The vehicle's engine
starts.
myCar.StartEngine(); // Output: The car's engine
starts with a key or button.
myElectricCar.StartEngine(); // Output: The electric car's
engine starts silently.
}
}
Explanation:
• Polymorphism allows a method in the base class (StartEngine()) to be overridden
by derived classes (Car and ElectricCar).
• The StartEngine() method in Vehicle is marked as virtual, which means it can be
overridden.
• The Car and ElectricCar classes use override to provide their specific
implementation of StartEngine().
• Base class references (Vehicle myCar = new Car();) are used to call the method,
but the actual method executed depends on the object type (Car or ElectricCar).
Confidential - Oracle Restricted