Properties/Inheritance Flashcards
Features and Advantages of Properties
Properties create a protection layer around fields, preventing assignment of invalid values into properties & also do some calculation automatically when someone has invoked the property.
No memory will be allocated for the property.
Properties: Key Points to Remember
It is recommended to use Properties always in real-time projects.
You can also use ‘Auto-implemented properties’ to simplify the code.
Properties doesn’t occupy any memory (will not be stored).
Properties forms a protection layer surrounding the private field that validates the incoming value before assigning into field.
Read-only property has only ‘get’ accessor; Write-only property has only ‘set’ accessor.
Properties can’t have additional parameters.
Inheritance
This allows classes to be arranged in a hierarchy that represents “is-a-type-of” relationships.
The “parent class” acts as a “base type” of “one or more child classes”.
Child classes are derived from parent class.
Concept of extending the parent class, by creating child class.
“Child class” extends “parent class”.
The child class’s object stores members of both child class and parent class.
Types of Inheritance
- Single Inheritance
class ParentClassName
{}
class ChildClassName : ParentClassName
{}
One Parent Class, One Child Class.
- Multi-Level Inheritance
class ParentClassName
{}
class ChildClass1 : ParentClassName
{}
class ChildClass2 : ChildClass1
{}
One Parent Class, One Child Class; and the Child class has another Child class.
- Hierarchical Inheritance
class ParentClassName
{}
class ChildClass1 : ParentClassName
{}
class ChildClass2 : ParentClassName
{}
One Parent Class, Multiple Child Classes.
- Multiple Inheritance
class ParentClass1
{}
class ParentClass2
{}
class ChildClass : ParentClass1, ParentClass2
{}
Multiple Parent Classes, One Child class.
- Hybrid Inheritance
class ParentClassName
{}
class ChildClass1 : ParentClassName
{}
class ChildClass2 : ChildClass1
{}
class ChildClass3 : ChildClass1
{}
Hierarchical Inheritance + Multi Level Inheritance
‘base’ keyword
The “base” keyword represents parent class’s members in the child class.
It is optional to use, by default.
It is must to use, when there is “name ambiguity” between parent class’s member and child class’s member.
Method Hiding
It is a concept, which is used to hide (overwrite) the parent class’s method, by creating another method in the child class with same name and same parameters.
When method hiding is done, if the method is called using child class’s object; the child class’s method only executes; parent class’s method will not be executed.
Method hiding is done automatically; but is recommended to use “new” keyword (but not must).
TWORZĘ METODĘ O TEJ SAMEJ NAZWIE W KLASIE DZIECKA, ALE DAJĘ SŁOWO NEW, BEZ VIRTUAL AND OVERRIDE
CANT USE BASE KEYWORD TO ACCESS PARENT METHOD
Method Overriding
It is a concept, which is used to extend the parent class’s method, by creating another method in the child class with same name and same parameters.
parent class method- virtual
child class method- overrride
When method overriding is done, if the method is called using child class’s object; the parent class’s method first and child’s method executed next.
Method Overriding is done with “virtual” keyword at parent class; and “override” keyword at child class’s method.
WE CAN STILL CALL THE PARENT METHOD USING BASE
The parent class’s method invoked using “base” keyword.
Without ‘virtual’ keyword are parent class’s method; the child class’s method can’t be ‘override’.
Key Points to Remember about inheritance
The “parent class” acts as a “base type” of “one or more child classes”.
The ‘base’ keyword inside the child class, can access the members of parent class.
The child class’s object stores parent class’s fields also, automatically; however they are accessible in child class if they are not ‘private’.
Method hiding is a concept of ‘overwriting’ the parent class’s method, by creating another method in child class, with same signature.
Method overriding is a concept of ‘extending’ the parent class’s method, by creating another method in child class, with same signature.
C# doesn’t support multiple inheritance (with multiple parent classes). That means, a child class can have ONLY ONE parent class; however, a child class can have MULTIPLE parent interfaces; so in this way, C# supports multiple inheritance.
Abstraction
Abstraction
Abstraction is a concept of representing objects in a simplified way by focusing only on the essential features and ignoring the irrelevant details.
**It provides a blueprint or a contract **for derived classes to follow, ensuring consistency in your code.
Benefit:
Makes the program loosely coupled (makes the classes less dependent on the other).
Implemented using:
Abstract classes [or]
Interfaces
Abstract Classes
Abstract class is a parent class, for which, we can’t create object; but we can create child classes.
The main intention of abstract class is to provide common set of fields and methods to all of its child classes of a specific group.
Abstract class can contain all types of members (fields, properties, methods, constructors etc.).
We can’t create object for abstract class; but we can access its members through child class’s object.
So ‘creating child class of abstract class’ is the only-way to utilize abstract classes.
Use Abstract class concept, for the classes, for which, you feel creating object is not meaningful.
Abstract Methods
Abstract methods are declared in parent class, with “abstract” keyword; implemented in child classes, with “override” keyword.
When the parent class don’t want to provide the definition of a method;it wants to let child classes to implement the method.
Abstract Methods contain “method declaration” only; but not “method body”.
Child class must provide method body for abstract methods.
Interfaces
Interface is a set of abstract methods, that must be implemented by the child classes.
The child class that implements the interface, MUST implement ALL METHODS of the interface.
Interface methods are by default “public” and “abstract”.
The child class must implement all interface methods, with same signature.
You can’t create object for interface.
You can create reference variable for the interface.
The reference variable of interface type can only store the address of objects of any one of the corresponding child classes.
You can implement multiple interfaces in the same child class [Multiple Inheritance].
An interface can be child of another interface.
Polymorphism
Polymorphism provides the ability to the developer, to define different implements for the same method in the same class or different classes.
Compile-time polymorphism:
Eg: Method Overloading
Decission will be taken at compilation time.
Also known as “Early binding” / “Static polymorphism”.
Run-time polymorphism:
Eg: Method Overriding
Decission will be taken at run time.
Also known as “Late binding” / “Dynamic polymorphism”.
Multiple Inheritance
In C#, “multiple inheritance” IS POSSIBLE with INTERFACES; that means a child class can have multiple parent interfaces.
“One Child - Multiple Parent Classes / Parent Interfaces” is called as “Multiple Inheritance”.
In C#.NET, “multiple inheritance” IS NOT POSSIBLE with CLASSES; that means you can’t specify multiple parent classes.
The child class MUST IMPLEMENT all methods of all the interfaces, that are inherited from.
Interface Inheritance
If an interface inherits from another interface, we call it as “Interface Inheritance”.
The child class that implements the child interface must implement all the members of both parent interface and child interface too.