OOP Flashcards
(17 cards)
What means encasulaption?
It means that a set of related properties and methods are treated as a single object/instance/unit. This single unit it’s called Class.
What’s inheritance ?
Create a class based on another class. Let’s say we have a class A, and we created a class B, which it’s based on class A. So that means class B it’s a sub class of A (Class A it’s parent, Class B it’s his child).
What is polymorphism?
It means that two or more class can have the same method but it can implemented differently each other.
How I can declare a class in C#?
class MyClass{ … }
Does C# allow multi inheritance?
No, it doesn’t. C# just allows to inherit from one base class.
What’s a property in a class?
It’s a feature that defines the class. For example, we have a class called Person. One of Person’s properties is Age.
class Person { public int age {get; set;} }
What’s a method?
It’s a operation or action that can perform the class. For example:
class Person { public void sayHello(){ Console.WriteLine("Hello!"); } }
What means overload a method?
It means the same method can have different signature. A method signature it’s the type of ouput and the type of parameters which receives. For example, the method string getName(Person person) have this signature: returns a String, and accepts a Person object as input. So overload it’s have the same method name but with distinct signatures. For example:
void sayHello(){ /*implementation*/} void sayHello(string name) { /*implementation*/}
Both methods have the same name but distinct signatures.
What’s a constructor?
It’s a method which initializes the properties of a class. A construct can be overloaded. For example:
class Person { string name;
public Person(){ name = "Alfonso"; } }
What’s a nested class and how it can be instantiated?
A nested class it’s a class which it’s inside another class. For example:
Class Parent{ /*fields and properties*/ Class Son{ /*fields and properties*/ } }
To instantiate the child class, just use the Parent (Container) class after dot operator and the Child (Nested) class:
Parent.Son sonSubclass = new Parent.Son();
What’s the difference between an assembly and a namespace?
A namespace contain one or more class. Whereas an assembly contain one or more namespace.
What are the access modifiers in c#?
public: the type or member can be accessed by any other code in the same assembly or another assembly which references the main assembly.
private: the type or member can be accesed by code in the same class.
protected: the type or member can be accesed by code in the same class or derived class.
internal: the type or member can be accesed by any code in the same assembly.
protected internal: the type or member can be accesed by any code in the same assembly, but by any derived class in another assembly.
What’s a static class?
A static member of the class is a property, procedure, or field that is shared by all instances of a class. All members of a static class needs to be also static, it cannot access to non static members. A static class cannot be instantiated.
For example:
static class Person{ static int age = 3;
}
To access to a member of the Person class, just access it with the dot operator:
var edadPersona = Person.age; /there’s not instantiation/
How I can inherit in C#?
Let’s say we have an Animal class, which it’s going to be our base class. And we want a child class from the Animal class (a subclass), and it’ll be called Dog. So what we need to do in C# is:
class Animal { /*implementation*/ }
class Dog: Animal { /*Dog class implementation*/ }
Now Dog has all properties and methods from Animal class.
How I can declare a class for not being inherited?
Using the keyword “sealed” while it’s being declared. For example:
sealed class Car{ /implementation/}
The next code will be raise an error because it cannot inherit from Car class:
class Corolla: Car {/implementation/}
How I can declare a class just for being inherited and not instantiated?
Using the keyword “asbract” while it’s being declared. For example:
abstract class Car{ /implementation/}
class Corolla: Car {/implementation/}
What’s an interface?
It’s like a class: has properties, methods and so on. But the methods are not implemented, just declared. A class can “inherit” from an interface but it needs to implement all his methods. An interface it’s like a contract.
For example:
interface ICar{ void start(); }
Camry: ICar{ void start(){ /*Camry class start() method implementation*/ } }
Civic: ICar{ void start(){ /*Civic class start() method implementation*/ } }