Oops
Oops
Inheritance
It allows a class to "inherit" (behavior or characteristics) of another, more general class.
In .NET and other modern programming languages, a class can inherit from a single class
only (single inheritance), unlike C++ which supports inheriting from multiple classes
(multiple inheritance). This limitation is necessitated by the difficulty in deciding which
method to use when there are duplicate methods across classes (in C++, this problem is
solved in a very complicated manner). In .NET, classes can inherit multiple interfaces.
Base keyword
The base keyword is used to access members of the base class from within a derived class. Use
it if you want to:
Call a method on the base class that has been overridden by another method.
Specify which base-class constructor should be called when creating instances of the
derived class.
When inheriting a class, our constructors must call the base class constructor, so that it can
initialize its member variables. If we do not do this explicitly, the compiler will place a call to the
parameterless base class constructor, ":base()", at the beginning of all our inheriting class'
constructors.
If the base class has no default constructor (one without parameters) or that constructor is
hidden (Either class is static, or has private constructor, or has explicitly defined constructor),
our constructors need to explicitly call one of the other base class constructors. The omission of
such a call will result in a compile-time error.
public class Felidae
{
private bool male;
// This constructor calls another constructor
public Felidae() : this(true)
{}
// This is the constructor that is inherited
public Felidae(bool male)
{
this.male = male;
}
public bool Male
{
get { return male; }
set { this.male = value; }
}
}
class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}
/*
Output
Name: John L. Malgraine
SSN: 444-55-6666
Employee ID: ABC567EFG
*/
this Keyword
The current instance of the class
Uses
To declare indexers
System.Object Class
In .NET every class, which does not inherit a class explicitly, inherits the system class
System.Object by default.
Every class, which inherits from another class indirectly, inherits Object from it.
Because of this property, every class instance can be cast to Object.
Upcasting
Object.ToString()
It returns a textual representation of an object. Every object includes this method and
therefore has a textual representation
override
Methods inherited from the base class and declared as virtual can be overridden. This means
changing their implementation; the original source code from the base class is ignored and new
code takes its place. Like overriding ToString method above.
This indicates for the compiler to use the last defined implementation of a method. Even if the
method is called on a reference to the base class it will use the implementation overriding it.
New
when we use the keyword new, we create a new method, which hides the old one. The old
method can then only be called with an upcast.
The new modifier instructs the compiler to use your child class implementation instead of the
parent class implementation. Any code that is not referencing your class but the parent class
will use the parent class implementation.
Will first call Base.DoIt, then Derived.DoIt. They're effectively two entirely separate methods
which happen to have the same name, rather than the derived method overriding the base
method.
using System;
using System.Text;
namespace OverrideAndNew
{
class BaseClass
{
public virtual void Method1()
{
Console.WriteLine("Base - Method1");
}
public void Method2()
{
Console.WriteLine("Base - Method2");
}
// The following two calls do what you would expect. They call
// the methods that are defined in DerivedClass.
DerivedClass dc = new DerivedClass();
dc.Method1();
dc.Method2();
dc.Method3();
//Derived - Method1
//Derived - Method2
//Base - Method3
// Error
//DerivedClass dcbc = new BaseClass();
}
}
}
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-
structs/knowing-when-to-use-override-and-new-keywords
Abstraction
Working with something we know how to use without knowing how it works internally. Like
using List, We do not care internally if List uses Array or linked List or Tree, we just use List for
our collection.
Interfaces
An interface can only declare methods and constants.
The best way to achieve abstraction is by working though interfaces. A component works with
interfaces which another implements. That way, a change in the second component will not
affect the first one as long as the new component implements the old interface.
Some important interfaces from the Common Type System (CTS) are the list and collection
interfaces: System.Collections.Generic.IList and System.Collections.Generic.ICollection.
When Should we Use Interface: The answer to this question is: always when we want to achieve
abstraction of data or actions, whose implementation can change later on. Code, which
communicates with another piece of code through interfaces, is much more resilient to changes
Encapsulation
"information hiding"
Like using access modifier, doesn’t publicly disclose information about its inner workings
Consider a real-life example of encapsulation, in a company, there are different sections like the
accounts section, finance section, sales section, etc. Now,
The finance section handles all the financial transactions and keeps records of all the data
related to finance.
Similarly, the sales section handles all the sales-related activities and keeps records of all
the sales.
Now there may arise a situation when for some reason an official from the finance section
needs all the data about sales in a particular month.
In this case, he is not allowed to directly access the data of the sales section. He will first have to
contact some other officer in the sales section and then request him to give the particular data.
Data Protection: Encapsulation protects the internal state of an object by keeping its data
members private. Access to and modification of these data members is restricted to the class’s
public methods, ensuring controlled and secure data manipulation.
Information Hiding: Encapsulation hides the internal implementation details of a class from
external code. Only the public interface of the class is accessible, providing abstraction and
simplifying the usage of the class while allowing the internal implementation to be modified
without impacting external code.
Polymorphism
Polymorphism allows treating objects of a derived class as objects of its base class
For example, big cats (base class) catch their prey (a method) in different ways. A Lion (derived
class) sneaks on it, while a Cheetah (another derived class) simply outruns it.
Abstract Classes
The class in incomplete and the class is not ready to be instantiated.
Class with at least one abstract method must be abstract, however, it is also possible to
define a - class as an abstract one, even when there are no abstract methods in it.
Abstract classes, as well as interfaces, cannot be instantiated
A pure abstract class is an abstract class, which has no implemented methods and no member
variables. It is very similar to an interface. The fundamental difference is that a class can
implement many interfaces and inherit only one class (even if that class is abstract).