Integ - Inheritance & Poly
Inheritance is one of the principles of object-oriented programming that allows the defining of a child class that reuses or inherits the behavior of a parent class (or existing class).
The inheriting class is called a derived class or subclass.
The existing class whose members are being inherited is called base class or superclass.
The C# programming language only supports single inheritance, where a subclass can only inherit from a single superclass.
Derived classes can reuse or inherit the behavior (methods) of the superclass.
Derived classes can use a method from the superclass without recreating their own method.
Derived classes can have their own methods or properties.
Because inheritance is transitive, the members of a class are available to the upcoming derived classes of its subclasses.
In C#, the colon (:) symbol indicates that the class
DerivedClassinherits the members of the classBaseClass.Derived classes do not inherit the base class' constructors.
Derived classes can invoke (or call) the constructors of the base class.
Calling the constructor of a base class from the constructor in the derived class is used to initialize the inherited data members of a derived class from the base class.
A derived class can inherit the private members of a base class, but these are not accessible.
When an instance variable of a base class is declared as private, the derived class can only access that variable through methods or by using properties.
Declaring the members of a superclass as private prevents its subclasses from modifying the members of the superclass.
Unlike private, the protected members of a base class are inherited and are accessible by their derived classes.
The keyword
protectedis used to declare a protected member.Members of a superclass are declared protected if you want its subclasses to access its members.
In C#, the
basekeyword is used to specify which constructor from the base class should invoke when creating instances of the derived class.The
basekeyword can also be used to call a method from the base class.When creating an instance of the derived class using the defined constructor, the required arguments of the constructor must be defined.
Declaring a method in a derived class with the same name as the method from its base class is called method overriding.
Method overriding is redefining the functionality of an existing method.
Method overriding is used for defining a specific behavior to the overriding method of a derived class that is different from the existing method from its base class.
The override method should have the same method signature (access modifier, return type, method name, and parameter list) as the overridden method.
In C#, the overridden method from the base class should be declared as
virtual.If the class is abstract, the overridden method should be declared
abstractorvirtual.The
virtualmodifier specifies that a derived class can override the method in the base class.When overriding a virtual method from the base class, the
overridemodifier is required to modify the abstract or virtual implementation of the inherited method.The override method must have the same method signature as the overridden method (virtual method).
Only an abstract and virtual method can be overridden in C#.
An abstract class is a base class that cannot be instantiated to create an object.
The purpose of an abstract class is to provide an outline and class members that must be implemented to its derived classes.
The
abstractkeyword is used to declare an abstract class and is placed before the class name.An abstract class may contain abstract methods that must be declared as a member of the abstract class.
An abstract method is a method header with an abstract modifier that has no implementation or method body.
The implementation for an abstract method is provided by overriding it on the derived class.
An abstract method must be declared within the abstract class and must have a semicolon (;) after parentheses (()).
When you want the base class not to be instantiated, declare it as an abstract class.
Polymorphism, which means “multiple forms,” is one of the fundamental concepts of object-oriented programming.
It enables classes to provide multiple methods with the same name but with different implementations or behavior.
Polymorphism has two (2) different forms: compile time and runtime polymorphisms.
Compile time polymorphism – Also known as “static polymorphism,” this polymorphism is implemented using method overloading.
In method overloading, a method is executed depending on the number and type of parameters passed to it.
When a program is compiled, the compiler binds the appropriate method to the object based on the method’s arguments. This process is called early binding.
Runtime polymorphism – This polymorphism is a process in which the compiler determines which method to call during runtime.
This process is also called dynamic polymorphism or late binding.
Runtime polymorphism is achieved using method overriding.