0% found this document useful (0 votes)
4 views

Classes and Methods

Uploaded by

medo43005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Classes and Methods

Uploaded by

medo43005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

What is OOP?

• OOP stands for Object-Oriented Programming.


• Procedural programming is about wri�ng procedures or
methods that perform opera�ons on the data, while object-
oriented programming is about crea�ng objects that contain
both data and methods.
• Classes and objects are the two main aspects of object-
oriented programming.
• Everything in C# is associated with classes and objects, along
with its atributes 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";

sta�c void Main(string[] args)


{
Car myObj = new Car();
Console.WriteLine(myObj.color);
}
}

Classes and Methods

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";

public void Drive()


{
Console.WriteLine("Driving...");
}
}

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.

2. Abstract Class (‫ اﻟـ‬Class ‫)اﻟﻣﺟردة‬:


(object) ‫ﻻ ﯾﻣﻛن اﺧذ‬
(Inheritance) ‫ﻟﻛن ﯾﻣﻛن ﻋﻣل‬
Abstract classes provide a base structure for other classes. They
cannot be instan�ated directly. An abstract class may include
abstract methods, which must be implemented by derived
classes, and regular methods with implementa�ons. For
example:
public abstract class Animal
{
public abstract void MakeSound(); // Abstract method

public void Sleep()


{
Console.WriteLine("Sleeping...");
}
}
‫( اﻟطرق اﻟﻣﺟردة‬Abstract Methods):
‫ھﻲ طرق ﺑدون ﺗﻧﻔﯾذ ﻓﻲ اﻟﻛﻼس اﻟﻣﺟرد وﯾﺟب ﻋﻠﻰ اﻟﻛﻼﺳﺎت اﻟﻣﺷﺗﻘﺔ ﺗﻧﻔﯾذھﺎ‬
Abstract Methods: Methods without an implementa�on in the
abstract class. Derived classes must provide the
implementa�on.

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)

• Abstract Class: Use abstract methods when you want


derived classes to provide specific implementa�ons, and
regular or virtual methods for common func�onality across
all subclasses. .(Abstract Method & Regular Method)

• Interface: Use method signatures and proper�es to define a


contract that classes must follow, with no implementa�on
provided. .(Abstract Methods only)

Interfaces Abstract Concrete ‫اﻟﻣﯾزة‬


class class
   objects ‫اﻧﺷﺎء‬
(constant only)
   Regular
method
   Abstract
methods
   Inheritance
Method Overloading:-
With method overloading, mul�ple methods can have the
same name with different parameters
int MyMethod(int x)
float MyMethod(float x)
double MyMethod(double x, double y)

sta�c int PlusMethodInt(int x, int y)


{
return x + y;
}

sta�c double PlusMethodDouble(double x, double y)


{
return x + y;
}

sta�c void Main(string[] args)


{
int myNum1 = PlusMethodInt(8, 5);
double myNum2 = PlusMethodDouble(4.3, 6.26);
Console.WriteLine("Int: " + myNum1);
Console.WriteLine("Double: " + myNum2);
}
Method Override:-
(Abstract Method) ‫ﯾﺗم اﺳﺗﺧدام‬
(body) ‫ﯾﺗم اﺳﺗﺧدام ﻧﻔس اﻻﺳم ﻟﻛن ﻣﻊ اﺧﺗﻼف‬
: ‫ﻣﺛﺎل‬
‫ ﻟﺣﺳﺎب اﻟﻣﺳﺎﺣﮫ‬area ‫ ﺑﺎﺳم‬method ‫ﻋﻣل‬
‫ اﻟﺧﺎص ﺑﮭﺎ‬body ‫ﻟﻛن ﻋﻠﻲ ﺣﺳب اﻟﻣطﻠوب ﯾﺗم ﻋﻣل‬
(..... - ‫)ﺣﺳﺎب ﻣﺳﺎﺣﮫ اﻟداﺋرة – اﻟﻣﺳﺗطﯾل – اﻟﻣرﺑﻊ‬

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

// Create a class constructor for the Car class


public Car()
{model = "Mustang"; // Set the ini�al value for model}

sta�c void Main(string[] args)


{Car Ford = new Car(); // Create an object of the Car Class (this will
call the constructor)
Console.WriteLine(Ford.model); // Print the value of model
}
}
Note that the constructor name must match the class name,
and it cannot have a return type (like void or int).
Also note that the constructor is called when the object is
created.
All classes have constructors by default: if you do not create a
class constructor yourself, C# creates one for you. However,
then you are not able to set ini�al values for fields.
Constructors save �me! Take a look at the last example on this
page to really understand why.

Inheritance:

In C#, it is possible to inherit fields and methods from one class


to another. We group the "inheritance concept" into two
categories:

• Derived Class (child) - the class that inherits from another


class
• Base Class (parent) - the class being inherited from
• To inherit from a class, use the : symbol.
In the example below, the Car class (child) inherits the fields
and methods from the Vehicle class (parent):
class Vehicle // base class (parent)
{
public string brand = "Ford"; // Vehicle field
public void honk() // Vehicle method
{
Console.WriteLine("Tuut, tuut!");
}
}

class Car : Vehicle // derived class (child)


{
public string modelName = "Mustang"; // Car field
}

class Program
{
sta�c void Main(string[] args)
{
// Create a myCar object
Car myCar = new Car();

// Call the honk() method (From the Vehicle class) on the


myCar object
myCar.honk();

// Display the value of the brand field (from the Vehicle


class) and the value of the modelName from the Car class
Console.WriteLine(myCar.brand + " " + myCar.modelName);
}
}
Why And When To Use "Inheritance"?
- It is useful for code reusability: reuse fields and methods of an
exis�ng class when you create a new class.

Different Types of Inheritance


 Single inheritance (only one base class)
 Mul�ple inheritance (several base classes)
 Hierarchical inheritance (one base class, many subclasses)
 Mul�level inheritance (derived from a derived class)

You might also like