Classes and Methods
Classes and Methods
Create a Class:
• Create a class named "Car" with a variable color:
class Car
{
string color = "red";
}
Create an Object:-
Create an object called "myObj" and use it to print the value
of color:
class Car
{
string color = "red";
Types of classes:
1- Concrete Classes
2- Abstract Classes
3- Interfaces
Concrete Classes:-
Concrete classes contain complete implementa�ons of all their
members. Objects can be directly instan�ated from concrete
classes. For example:
ﯾﻣﻛن إﻧﺷﺎء ﻛﺎﺋﻧﺎت ﻣﻧﮭﺎ
Inheritance وﻋﻣل
class Car
{
string color = "red";
2. Abstract Class:
An Abstract Class is a class that cannot be instan�ated directly.
The purpose of an abstract class is to provide a common
structure (like proper�es and methods) that subclasses can
implement. It may contain abstract members (abstract
methods), which do not have implementa�ons in the abstract
class, and the derived classes are required to implement these
methods.
Features of Abstract Class:
• It can have abstract methods, which must be implemented
by derived classes.
• It can also have regular methods with implementa�on.
• It can have fields and proper�es.
• You cannot create objects directly from an abstract class.
Example:
public abstract class Animal
{
public abstract void MakeSound(); // Abstract method
(no implementa�on)
}
3. Interface Class:
An Interface is a type in C# that defines a set of method
signatures without providing any implementa�on. It cannot
contain fields or proper�es, and it only defines the contract
(method signatures) that classes implemen�ng the interface
must follow.
Features of Interface:
• It contains only method signatures, without
• implementa�ons.
• It cannot have fields or proper�es.
• It can be implemented by classes or structs.
• A class can implement mul�ple interfaces.
(variables / Atributes) ﻻﯾوﺟد
(constant) ﻟﻛن ﯾوﺟد
( ﻓﻘطAbstract Mothods) ﺑﮫ
Interfaces define a contract for classes. They contain only
method signatures without implementa�ons. Classes or structs
can implement mul�ple interfaces. For example:
public interface IAnimal
{
void MakeSound(); // Method signature
}
When to Use Different Methods:
• Concrete Class: Use instance, sta�c, or private methods as
needed for a full implementa�on. (Regular Method)
Constructors:-
A constructor is a special method that is used to ini�alize
objects. The advantage of a constructor, is that it is called when
an object of a class is created. It can be used to set ini�al values
for fields:
// Create a Car class
class Car
{
public string model; // Create a field
Inheritance:
class Program
{
sta�c void Main(string[] args)
{
// Create a myCar object
Car myCar = new Car();