OOPs Concept (C#,Java, C++,Python)
OOPs Concept (C#,Java, C++,Python)
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
• 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.
Objects
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;
• A class is a blueprint for creating objects. In this case, the Car class is defined as a
blueprint for car objects.
2. Properties:
o public string Brand { get; set; } allows Brand to be read and assigned outside
the class.
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.
• This program defines a Car class with three properties (Brand, Model, and Year) and
a method (StartEngine()).
• The StartEngine() method is then called on the myCar object, triggering the console
to display "The engine is started."
2. Encapsulation
Example program:
using System;
public class Car
{
// Private field (data is hidden)
private string model;
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):
3. Abstraction
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();
Explanation:
• 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
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;
// 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)
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.
5.Polymorphism
// 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.");
}
}
Explanation:
• 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).