Definition
- Inheritance in VB.NET is a fundamental and core concept of object-oriented programming that allows a class (child class or derived class) to inherit fields, properties, methods, and events from another class (parent class or base class).
Characteristics
- Inheritance promotes code reuse and establishes a relationship between classes.
- By using inheritance, we can create modular, reusable, and extensible code in VB.NET.
Syntax
Class ParentClassName
‘ Base class members(data & methods)
End Class
Class ChildClassName
Inherits ParentClassName
‘ Additional members for the derived class (data & methods)
End Class
Inheritance Keywords
- MyBase : This keyword is used to call/represent a base class method or constructor. This is similar to the Super Keyword of Java.
- NotInheritable : This keyword prevents a class from being inherited/supports inheritance. This is similar to Final Class in Java.
- MustInherit : This keyword declares an abstract class that cannot be instantiated but can serve as a base class.
Types of Inheritance
There are the following types of inheritance in VB .Net –
-
Single Inheritance
- A child class inherits from only one parent class and this child class has no further child.
-
Multilevel Inheritance
- A class inherits from a derived class, and this class may have a further child.
-
Hierarchical Inheritance
- When Multiple child classes(have no further children) inherit/derive from a single parent class(have no parent ancestors).
-
Multiple Inheritance
- VB .Net does not support this inheritance.
- This inheritance is supported with the help of the Interface concept.
-
Hybrid Inheritance
- Hybrid Inheritance is the combination of two or more types of Inheritance.
Method Overriding
Definition
- Method overriding in VB.NET allows a derived class to provide a specific implementation of a method that is already defined in its base class.
Keywords of Method Overriding
-
Overridable and Overrides :
- For Method overriding, the base class method must be marked as
Overridable
, and the derived class method must use theOverrides
keyword to override it. - In other words, The method to be overridden must be marked as Overridable, and the overriding method in the derived class must use the Overrides keyword.
- For Method overriding, the base class method must be marked as
-
MustOverride :
- If the method in the base class is declared with a keyword
MustOverride
, it must be overridden in the derived class, making the base class as abstract (MustInherit
).
- If the method in the base class is declared with a keyword
-
NotOverridable :
- A method in the base class can be marked as
NotOverridable
, preventing any derived class from overriding it.
- A method in the base class can be marked as
Characteristics
- Method Overriding enables/supports runtime polymorphism, where the method to be executed is determined at runtime based on the object type.
- The overriding method must have the same or greater access level as the base class method.
- By using the method overriding concept, VB.NET enables runtime polymorphism and allows developers to create flexible and extensible object-oriented designs.
0 Comments