C# Object Oriented Programming - Class and Interface
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
C# is a fully object-oriented programming language that allows developers to create complex applications using the principles of OOP. Two fundamental concepts in OOP are Classes and Interfaces.
1. Classes
A class is a blueprint for creating objects. It defines a set of properties (attributes) and methods (behaviors) that the objects created from the class will have.- Structure of a Class:
- Fields: Variables to hold the state of the class.
- Properties: Public interfaces to get or set the values of private fields.
- Methods: Functions that define behaviors.
Example of a Class:
using System;
class Car
{
// Fields
private string model;
private string color;
private int year;
// Constructor
public Car(string model, string color, int year)
{
this.model = model;
this.color = color;
this.year = year;
}
// Property for Model
public string Model
{
get { return model; }
set { model = value; }
}
// Method to display car details
public void DisplayInfo()
{
Console.WriteLine($"Model: {model}, Color: {color}, Year: {year}");
}
}
class Program
{
static void Main()
{
Car car1 = new Car("Toyota Camry", "Red", 2020);
car1.DisplayInfo(); // Output: Model: Toyota Camry, Color: Red, Year: 2020
}
}
Output:
Model: Toyota Camry, Color: Red, Year: 2020
2. Inheritance
Inheritance allows a class to inherit members (fields, properties, methods) from another class, promoting code reuse and creating a hierarchical relationship between classes.Example of Inheritance:
using System;
class Vehicle
{
public void Start()
{
Console.WriteLine("Vehicle starting...");
}
}
class Motorcycle : Vehicle
{
public void RevEngine()
{
Console.WriteLine("Revving the engine!");
}
}
class Program
{
static void Main()
{
Motorcycle motorcycle = new Motorcycle();
motorcycle.Start(); // Output: Vehicle starting...
motorcycle.RevEngine(); // Output: Revving the engine!
}
}
Output:
Vehicle starting...
Revving the engine!
3. Polymorphism
Polymorphism allows methods to do different things based on the object that it is acting upon, often achieved through method overriding and interfaces.Example of Polymorphism:
using System;
class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Woof!");
}
}
class Cat : Animal
{
public override void Speak()
{
Console.WriteLine("Meow!");
}
}
class Program
{
static void Main()
{
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.Speak(); // Output: Woof!
myCat.Speak(); // Output: Meow!
}
}
Output:
Woof!
Meow!
4. Interfaces
An interface defines a contract for classes. It specifies what methods and properties a class must implement without providing the implementation itself.- Defining an Interface:
- An interface can contain method signatures and properties but cannot contain any implementation.
Example of an Interface:
using System;
interface IAnimal
{
void Speak();
}
class Dog : IAnimal
{
public void Speak()
{
Console.WriteLine("Woof!");
}
}
class Cat : IAnimal
{
public void Speak()
{
Console.WriteLine("Meow!");
}
}
class Program
{
static void Main()
{
IAnimal myDog = new Dog();
IAnimal myCat = new Cat();
myDog.Speak(); // Output: Woof!
myCat.Speak(); // Output: Meow!
}
}
Output:
Woof!
Meow!
5. Properties and Access Modifiers
Properties in C# can use access modifiers to control visibility. Common modifiers include:- `public`: Accessible from any other code.
- `private`: Accessible only within the same class.
- `protected`: Accessible within the same class and by derived classes.
- `internal`: Accessible only within the same assembly.
Example of Properties with Access Modifiers:
using System;
class Person
{
private string name;
public int Age { get; set; }
public string Name
{
get { return name; }
set { name = value; }
}
}
class Program
{
static void Main()
{
Person person = new Person();
person.Name = "Alice";
person.Age = 30;
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}"); // Output: Name: Alice, Age: 30
}
}
Output:
Name: Alice, Age: 30
6. Summary
- Classes: Serve as blueprints for creating objects, encapsulating data and functionality.- Interfaces: Define contracts that implementing classes must follow, enabling polymorphism and code flexibility.
Understanding classes and interfaces is essential for effective object-oriented programming in C#. These concepts help organize code, promote reuse, and improve maintainability.