SlideShare una empresa de Scribd logo
1 de 28
Descargar para leer sin conexión
Learn C# Programming
Classes & Inheritance
Eng Teong Cheah
MVP Visual Studio & Development Technologies
Classes
Classes
When you define a class, you define a blueprint for a data type. This does not actually
define any data, but it does define what the class name means. That is, what an object
of the class consists of and what operations can be performed on that object. Objects
are instances of a class. The methods and variables that constitute a class are called
members of the class.
Defining a Class
A class definition starts with the keyword class followed by the class name; and the class
body enclosed by a pair of curly braces. Following is the general form of a class
definition -
Defining a Class
Note –
- Access specifiers the access rules for the members as well as the class itself. If not
mentioned, then the default access specifier for a class type is internal. Default access
for the members is private.
- Data type specifies the type of variable, and return type specifies the data type of the
data the method returns, if any.
- To access the class members, you use the dot(.) operator.
- The dot operator links the name of an object with the name of a member.
Demo – Defining a Class
Member Functions and Encapsulation
A member function of a class is a function that has its definition or its prototype within
the class definition similar to any other variable. It operates on any object of the class of
which it is a member, and has access to all the members of a class for that object.
Member variables are attributes of an object (from design perspective) and they are
kept private to implement encapsulation. These variables can only be accessed using
the public member functions.
Demo – Member
Functions and
Encapsulation
C# Constructors
A class constructor is a special member function of a class that is executed whenever we
create new objects of that class.
A constructor has exactly the same name as that of class and it does not have any
return type.
Demo – C# Constructor
C# Constructors
A default constructor does not have any parameter but if you need, a constructor can
have parameters. Such constructors are called parameterized constructors.
Demo – Parameterized
Constructor
C# Destructors
A destructor is a special member function of a class that is executed whenever an object
of its class goes out of scope. A destructor has exactly the same name as that of the
class with a prefixed tilde (~) and it can neither return a value nor can it take any
parameters.
Destructor can be very useful for releasing memory resources before exiting the
program. Destructors cannot be inherited or overloaded.
Demo – Destructors
Static Members of a C# Class
We can define class members as static using the static keyword. When we declare a
member of a class as static, it means no matter how many objects of the class are
create, there is only one copy of the static member.
The keyword static implies that only one instance of the member exists for a class. Static
variables are used for defining constants because their values can be retrieved by
invoking the class without creating an instance of it. Static variables can be initialized
outside the member function or class definition. You can also initialize static variables
inside the class definition.
Demo – Static variables
Static Members of a C# Class
You can also declare a member function as static. Such functions can access only static
variables. The static functions exist even before the object is created.
Demo – Static functions
Inheritance
Inheritance
One of the most important concepts in object-oriented programming is inheritance.
Inheritance allows us to define a class in terms of another class, which makes it easier to
create and maintain and application. This also provides an opportunity to reuse the
code functionality and speeds up implementation time.
When creating a class, instead of writing completely new data members and member
functions, the programmer can designate that the new class should inherit the members
of an existing class. This existing class is called the base class, and the new class is
referred to as the derived class.
The idea of inheritance implements the IS-A relationship. For example, mammal IS
A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.
Base and Derived Classes
A class can be derived from more than one class or interface, which means that it
can inherit data and functions from multiple base classes or interfaces.
The syntax used in C# for creating derived classes is as follows −
Demo – Base and
Derived Classes
Initializing Base Class
The derived class inherits the base class member variables and member methods.
Therefore the super class object should be created before the subclass is created. You
can give instructions for superclass initialization in the member initialization list.
Demo – Initializing Base
Class
Multiple Inheritance in C#
C# does not support multiple inheritance. However, you can use interfaces to
implement multiple inheritance.
Demo – Multiple
Inheritance in C#
Related Content
TutorialsPoint
www.tutorialspoint.com
Thank you

Más contenido relacionado

La actualidad más candente

java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 

La actualidad más candente (20)

Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
 
inheritance
inheritanceinheritance
inheritance
 
Java interface
Java interfaceJava interface
Java interface
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
C# Types of classes
C# Types of classesC# Types of classes
C# Types of classes
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Java(inheritance)
Java(inheritance)Java(inheritance)
Java(inheritance)
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentals
 
javainheritance
javainheritancejavainheritance
javainheritance
 
javainterface
javainterfacejavainterface
javainterface
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 

Similar a Learn C# Programming - Classes & Inheritance

Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
lykado0dles
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
ArpitaJana28
 
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
AnkurSingh340457
 

Similar a Learn C# Programming - Classes & Inheritance (20)

Class properties
Class propertiesClass properties
Class properties
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Oops
OopsOops
Oops
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngnClass-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
 
My c++
My c++My c++
My c++
 
C# program structure
C# program structureC# program structure
C# program structure
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Classes2
Classes2Classes2
Classes2
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
Class and object
Class and objectClass and object
Class and object
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
 
C# interview
C# interviewC# interview
C# interview
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Understanding class, object & interface
Understanding class, object & interfaceUnderstanding class, object & interface
Understanding class, object & interface
 

Más de Eng Teong Cheah

Más de Eng Teong Cheah (20)

Monitoring Models
Monitoring ModelsMonitoring Models
Monitoring Models
 
Responsible Machine Learning
Responsible Machine LearningResponsible Machine Learning
Responsible Machine Learning
 
Training Optimal Models
Training Optimal ModelsTraining Optimal Models
Training Optimal Models
 
Deploying Models
Deploying ModelsDeploying Models
Deploying Models
 
Machine Learning Workflows
Machine Learning WorkflowsMachine Learning Workflows
Machine Learning Workflows
 
Working with Compute
Working with ComputeWorking with Compute
Working with Compute
 
Working with Data
Working with DataWorking with Data
Working with Data
 
Experiments & TrainingModels
Experiments & TrainingModelsExperiments & TrainingModels
Experiments & TrainingModels
 
Automated Machine Learning
Automated Machine LearningAutomated Machine Learning
Automated Machine Learning
 
Getting Started with Azure Machine Learning
Getting Started with Azure Machine LearningGetting Started with Azure Machine Learning
Getting Started with Azure Machine Learning
 
Hacking Containers - Container Storage
Hacking Containers - Container StorageHacking Containers - Container Storage
Hacking Containers - Container Storage
 
Hacking Containers - Looking at Cgroups
Hacking Containers - Looking at CgroupsHacking Containers - Looking at Cgroups
Hacking Containers - Looking at Cgroups
 
Hacking Containers - Linux Containers
Hacking Containers - Linux ContainersHacking Containers - Linux Containers
Hacking Containers - Linux Containers
 
Data Security - Storage Security
Data Security - Storage SecurityData Security - Storage Security
Data Security - Storage Security
 
Application Security- App security
Application Security- App securityApplication Security- App security
Application Security- App security
 
Application Security - Key Vault
Application Security - Key VaultApplication Security - Key Vault
Application Security - Key Vault
 
Compute Security - Container Security
Compute Security - Container SecurityCompute Security - Container Security
Compute Security - Container Security
 
Compute Security - Host Security
Compute Security - Host SecurityCompute Security - Host Security
Compute Security - Host Security
 
Virtual Networking Security - Network Security
Virtual Networking Security - Network SecurityVirtual Networking Security - Network Security
Virtual Networking Security - Network Security
 
Virtual Networking Security - Perimeter Security
Virtual Networking Security - Perimeter SecurityVirtual Networking Security - Perimeter Security
Virtual Networking Security - Perimeter Security
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Learn C# Programming - Classes & Inheritance

  • 1. Learn C# Programming Classes & Inheritance Eng Teong Cheah MVP Visual Studio & Development Technologies
  • 3. Classes When you define a class, you define a blueprint for a data type. This does not actually define any data, but it does define what the class name means. That is, what an object of the class consists of and what operations can be performed on that object. Objects are instances of a class. The methods and variables that constitute a class are called members of the class.
  • 4. Defining a Class A class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces. Following is the general form of a class definition -
  • 5. Defining a Class Note – - Access specifiers the access rules for the members as well as the class itself. If not mentioned, then the default access specifier for a class type is internal. Default access for the members is private. - Data type specifies the type of variable, and return type specifies the data type of the data the method returns, if any. - To access the class members, you use the dot(.) operator. - The dot operator links the name of an object with the name of a member.
  • 7. Member Functions and Encapsulation A member function of a class is a function that has its definition or its prototype within the class definition similar to any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object. Member variables are attributes of an object (from design perspective) and they are kept private to implement encapsulation. These variables can only be accessed using the public member functions.
  • 8. Demo – Member Functions and Encapsulation
  • 9. C# Constructors A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor has exactly the same name as that of class and it does not have any return type.
  • 10. Demo – C# Constructor
  • 11. C# Constructors A default constructor does not have any parameter but if you need, a constructor can have parameters. Such constructors are called parameterized constructors.
  • 13. C# Destructors A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor has exactly the same name as that of the class with a prefixed tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing memory resources before exiting the program. Destructors cannot be inherited or overloaded.
  • 15. Static Members of a C# Class We can define class members as static using the static keyword. When we declare a member of a class as static, it means no matter how many objects of the class are create, there is only one copy of the static member. The keyword static implies that only one instance of the member exists for a class. Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.
  • 16. Demo – Static variables
  • 17. Static Members of a C# Class You can also declare a member function as static. Such functions can access only static variables. The static functions exist even before the object is created.
  • 18. Demo – Static functions
  • 20. Inheritance One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain and application. This also provides an opportunity to reuse the code functionality and speeds up implementation time. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.
  • 21. Base and Derived Classes A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces. The syntax used in C# for creating derived classes is as follows −
  • 22. Demo – Base and Derived Classes
  • 23. Initializing Base Class The derived class inherits the base class member variables and member methods. Therefore the super class object should be created before the subclass is created. You can give instructions for superclass initialization in the member initialization list.
  • 24. Demo – Initializing Base Class
  • 25. Multiple Inheritance in C# C# does not support multiple inheritance. However, you can use interfaces to implement multiple inheritance.