Inheritance

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Inheritance

29 December 2021 09:31

Object Identity: Two objects are considered to be identical if they refer to


the same instance in the memory. In .NET whether object x is identical to
object y can be determined from expression:
System.Object.ReferenceEquals(x, y)

Object Equality: Two objects are considered to be equal if they refer to


instances of same type and with matching data in the memory. In .NET
whether object x is identical to object y can be determined from
expression:
x.GetHashCode() == y.GetHashCode() && x.Equals(y)

Member visibility outside of its defining type

Access Modifier In Current In External


Assembly Assembly
private (default) none none
internal all none
protected derived derived
internal protected all derived
public all all

Abstract Class Interface


Defines a non-activatable Defines a non-activatable reference
reference type which can contain type which cannot contain instance
instance fields fields
Can define pure (unimplemented) Instance methods are implicitly pure
instance methods which must be unless defined with default
declared with abstract modifier. implementation.
Members are private by default. Members are public by default.
Can define constructors required Cannot define constructors.
by the derived classes.
Can extend exactly one other class Can extend multiple other interfaces.
which may or may not be
abstract.
A class can inherit only from a A class can inherit from multiple
single abstract class and it must interfaces and it must implement all of
override all of its pure methods their pure methods unless this class is
unless this class is also declared as declared abstract in which case it can
abstract. define those methods as abstract
A struct cannot inherit from an A struct can inherit from multiple
abstract class. interfaces and it must implement all of
their methods to support implicit
conversion to those interfaces through
boxing.
Suitable for segregating the Suitable for segregating the common
common type of state (fields) type of behavior (methods) inherited by
inherited by different types of different types of data.
objects.

Multiple Inheritance in C#: CLR's type system does not allow a class to
inherit from multiple other classes because an instance of such a class will
require multiple sub-objects out of which only the first one can be
reference in a safe manner and without complicating essential runtime
mechanisms such as type-casting, virtual dispatch, reflection and garbage
collection. Since an interface cannot define an instance field it does require
a sub-object within an instance of its inheriting type and as such multiple
inheritance is supported through interfaces.

You might also like