Learn C#_ Learn C#_ Interfaces and Inheritance Cheatsheet _ Codecademy
Learn C#_ Learn C#_ Interfaces and Inheritance Cheatsheet _ Codecademy
Cheatsheets / Learn C#
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
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
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;
}
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
Print Share
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-interfaces-inheritance/cheatsheet 3/3