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

Learn C#_ Learn C#_ Interfaces and Inheritance Cheatsheet _ Codecademy

This document is a cheatsheet for learning C# focusing on interfaces and inheritance. It explains key concepts such as inheritance, the use of override and virtual keywords, the protected and abstract keywords, and the definition of interfaces. Each concept is illustrated with code examples to demonstrate their application in C# programming.

Uploaded by

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

Learn C#_ Learn C#_ Interfaces and Inheritance Cheatsheet _ Codecademy

This document is a cheatsheet for learning C# focusing on interfaces and inheritance. It explains key concepts such as inheritance, the use of override and virtual keywords, the protected and abstract keywords, and the definition of interfaces. Each concept is illustrated with code examples to demonstrate their application in C# programming.

Uploaded by

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

10-02-2024 16:04 Learn C#: Learn C#: Interfaces and Inheritance Cheatsheet | Codecademy

Cheatsheets / Learn C#

Learn C#: Interfaces and Inheritance

C# Inheritance

In C#, inheritance is the process by which one class public class Honeymoon : TripPlanner
inherits the members of another class. The class that
{ }
inherits is called a subclass or derived class. The other
class is called a superclass, or a base class.
When you define a class that inherits from another // Similar to an interface, inheritance
class, the derived class implicitly gains all the members
also uses the colon syntax to denote a
of the base class, except for its constructors and
finalizers. The derived class can thereby reuse the code class inherited super class. In this
in the base class without having to re-implement it. In case, Honeymoon class inherits from
the derived class, you can add more members. In this
TripPlanner class.
manner, the derived class extends the functionality of
the base class.
// A derived class can only inherit from
one base class, but inheritance is
transitive. That base class may inherit
from another class, and so on, which
creates a class hierarchy.

C# override/virtual Keywords

In C#, a derived class (subclass) can modify the class BaseClass


behavior of an inherited method. The method in the
{
derived class must be labeled override and the
method in the base class (superclass) must be labeled public virtual void Method1()
virtual . {
The virtual and override keywords are useful for
Console.WriteLine("Base - Method1");
two reasons:
1. Since the compiler treats “regular” and virtual }
methods differently, they must be marked as }
such.
2. This avoids the “hiding” of inherited methods,
which helps developers understand the class DerivedClass : BaseClass
intention of the code. {
public override void Method1()
{
Console.WriteLine("Derived -
Method1"); }
}

https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-interfaces-inheritance/cheatsheet 1/3
10-02-2024 16:04 Learn C#: Learn C#: Interfaces and Inheritance Cheatsheet | Codecademy

C# protected Keyword

In C#, a protected member can be accessed by the public class BankAccount


current class and any class that inherits from it. This is
{
designated by the protected access modifier.
protected decimal balance = 0;
}

public class StudentAccount : BankAccount


{
}

// In this example, the BankAccount


(superclass) and StudentAccount
(subclass) have access to the balance
field. Any other class does not.

C# abstract Keyword

In C#, the abstract modifier indicates that the thing abstract class Shape
being modified has a missing or incomplete
{
implementation. It must be implemented completely by
a derived, non-abstract class. public abstract int GetArea();
The abstract modifier can be used with classes, }
methods, properties, indexers, and events. Use the
abstract modifier in a class declaration to indicate
that a class is intended only to be a base class of other class Square : Shape
classes, not instantiated on its own. {
If at least one member of a class is abstract , the
int side;
containing class must also be marked abstract .
public Square(int n) => side = n;
The complete implementation of an abstract member
must be marked with override . // GetArea method is required to avoid
a compile-time error.
public override int GetArea() => side *
side;
}

// In this example, GetArea() is an


abstract method within the abstract Shape
class. It is implemented by the derived
class Square.

https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-interfaces-inheritance/cheatsheet 2/3
10-02-2024 16:04 Learn C#: Learn C#: Interfaces and Inheritance Cheatsheet | Codecademy

C# Interface

In C#, an interface contains definitions for a group of interface IAutomobile


related functionalities that a class can implement.
{
Interfaces are useful because they guarantee how a
class behaves. This, along with the fact that a class can string LicensePlate { get; }
implement multiple interfaces, helps organize and double Speed { get; }
modularize components of software.
int Wheels { get; }
It is best practice to start the name of an interface with
“I”. }

// The IAutomobile interface has three


properties. Any class that implements
this interface must have these three
properties.

public interface IAccount


{
void PayInFunds ( decimal amount );
bool WithdrawFunds ( decimal amount );
decimal GetBalance ();
}

// The IAccount interface has three


methods to implement.

public class CustomerAccount : IAccount


{ }

// This CustomerAccount class is labeled


with : IAccount, which means that it will
implement that interface.

Print Share

https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-interfaces-inheritance/cheatsheet 3/3

You might also like