Properties/Inheritance Flashcards

1
Q

Features and Advantages of Properties

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Properties: Key Points to Remember

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Inheritance

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Types of Inheritance

A
  1. Single Inheritance

class ParentClassName
{}
class ChildClassName : ParentClassName
{}
One Parent Class, One Child Class.

  1. 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.

  1. Hierarchical Inheritance

class ParentClassName
{}
class ChildClass1 : ParentClassName
{}
class ChildClass2 : ParentClassName
{}
One Parent Class, Multiple Child Classes.

  1. Multiple Inheritance

class ParentClass1
{}
class ParentClass2
{}
class ChildClass : ParentClass1, ParentClass2
{}
Multiple Parent Classes, One Child class.

  1. Hybrid Inheritance

class ParentClassName
{}
class ChildClass1 : ParentClassName
{}
class ChildClass2 : ChildClass1
{}
class ChildClass3 : ChildClass1
{}
Hierarchical Inheritance + Multi Level Inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

‘base’ keyword

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Method Hiding

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Method Overriding

A

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’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Key Points to Remember about inheritance

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Abstraction

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Abstract Classes

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Abstract Methods

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Interfaces

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Polymorphism

A

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”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Multiple Inheritance

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Interface Inheritance

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explicit Interface Implementation

A

“Explicit Interface Implementation” is used to implement an interface method privately; that means the interface method becomes as “private member” to the child class.

If a child class inherits from two or more interfaces, and there is a duplicate method (having same name and parameters) among those interfaces; then use “Explicit Interface Implementation”, to provide different implementations for different interface methods respectively.

You can use “Explicit Interface Implementation” to create private implementation of interface method; so that you can create abstraction for those methods.

void IAnimal.Speak()
{
Console.WriteLine(“Woof! Woof! (Robot Dog)”);
}

// Implementacja metody Speak z interfejsu IMachine
**void IMachine.Speak()**
{
    Console.WriteLine("Beep! Boop! (Robot Machine)");
}
17
Q

Namespaces

A

Namespaces is a collection of classes and “other types such as interfaces, structures, delegate types, enumerations).

Namespaces goal is to group-up classes and other types that are related to a particular project-module, into an unit.

Syntax to access a type that is present inside the namespace: NamespaceName.TypeName

18
Q

Nested Namespaces

A

The namespace which is declared inside another namespace is called as “Nested namespace” or “Inner Namespace”.

Use nested namespaces, in order to divide the classes of a larger namespace, into smaller groups.

Syntax to access a type in the inner namespace: OuterNamespace.InnerNamespace.TypeName

19
Q

Importing Namespaces (‘using’ Directive)

A

The “using” is a directive statement (top-level statement) that should be placed at the top of the file, which specifies the namespace, from which you want to import all the classes and other types.

Syntax: using Namespacename;

When you import a namespace, you can directly access all of its classes and other types (but not inner namespaces).

The “using directives” are written independently for every file.

“One using directive” can import “one namespace” only.

20
Q

‘using’ Alias Name

A

The “using alias” directive allows you to create “alias name” for the namespace.

Syntax:

using AliasName = Namespacename;

Use “using alias” directive, if you want to access long namespaces with shortcut name.

It is much useful to access specific namespace, when there is namespace name ambiguity (two classes with same name in two different namespaces and both namespaces are imported in the same file).

21
Q

‘using’ static

A

The “using static” directive allows you import a static class directly from a namespace; so that you can directly access any of its methods anywhere in the current file.

Syntax:

using static Namespacename.StaticClassName;

Use the “using static” directive to access methods of static class easily, without repeating the class name each time.

22
Q

Partial Classes

A

Partial Class is a class that splits into multiple files.

Each file is treated as a “part of the class”.

At compilation time, all partial classes that have same name, become as a “single class”.

All the partial classes (that want to be a part of a class) should have same name and should be in the same namespace and same assembly & should have same access-modifier (such as ‘internal’ or ‘public’).

Duplicate members are not allowed in partial classes.

Any attributes / modifiers (such as abstract, sealed) applied on one partial class, will be applied to all partial classes that have same name.

The ‘partial’ keyword can be used only at before the keywords ‘class’, ‘struct’, ‘interface’ and ‘void’.

Each partial class can be developed individually, by different developers / teams.

In WinForms / WebForms, the ‘Designer-generated code’ will be kept in one partial class; the ‘code written by developer’ will be kept in another partial class with same name; so both become as a single class at compilation time.

23
Q

Partial Methods

A

Partial Methods are “declared in one partial class” (just like abstract method), and “implemented in another partial class”, that have same name.

Assume, there are two developers; the first developer develops the first partial class; second developer develops the second partial class.

The partial method lets the first developer to declare a partial method in one partial class; and the second developer implements the partial method in the other partial class.

Partial Methods can only be created in partial class or partial structs.

Partial Methods are implicitly private. It can’t have any other access modifier.

Partial Methods can have only “void” return type.

Implementation of partial methods is optional. If there is no implementation of partial methods in any parts of the partial class, the method calls are removed by the compiler, at compilation time.

If you are building large class libraries and decide extension of methods to other developers, partial methods can be used.

24
Q

Static Classes

A

Static class is a class that can contain only “static members”.

If you don’t want even single ‘instance member’ (non-static member), then use ‘static class’.

Advantage: We can avoid accidental creation of object for the class, by making it as “static class”.

25
Q

Static Classes [vs] Normal Classes

A

Static Class:

static class ClassName
{
static fields
static methods
static constructors
static properties
static events
}
Static class is to create ONLY static members (static fields, static methods etc.)

Static Class:

class ClassName
{
static fields
static methods
static constructors
static properties
static events

instance fields
instance methods
instance constructors
instance properties
instance events
destructors
}
Normal (non-static class) is to create both instance & static members [or] only instance members.

26
Q

Enumerations

A

Enumeration is a collection of constants.

Enumeration is used to specify the list of options allowed to be stored in a field / variable.

Use enumeration if you don’t want to allow other developers to assign other value into a field / variable, other than the list of values specified in the enumeration

Syntax to access member of enum: EnumerationName.ConstantName

Syntax to create Enumeration:

enum EnumerationName
{
Constant1, Constant2, …
}

By default, each constant will be assigned to a number, starts from zero; however you can change the number (integer only).

enum EnumerationName
{
Constant1 = value, Constant2 = value, …
}

The default data type of enum member is “int”. However, you can change its data type as follows.

enum EnumerationName : datatype
{
Constant1 = value, Constant2 = value, …
}

27
Q
A