Objects - Inheritance Flashcards
What are the four core principles of OOP?
Encapsulation, inheritance, polymorphism and abstraction.
Subclasses
Subclasses inherit the attributes and behaviors of the more general classes.
When you write a class, you usually want to try and make it as general and open ended as possible so that you can reuse it in other programs that you write.
A subclass is taking a more general class and then adding the needed functionality to it that is specific to the current program that you are working on.
How do you spewcify a class as a subclass?
You add a colon after the name of the SUBclass that you are making, followed by the name of the CLASS that you are extending.
class NewSubClass : OriginalClass {
}
When creating a subclass, what do you need to make sure and do when adding it’s parameters?
You need to include the base class’s parameter’s as well so that they can be satisfied.
You do this by adding a colon after the SUBCLASS’s parameters have been laid out, then adding the keyword “base” followed by the parameters that the base class takes.
public NewSubClass (int x, int y) : base(x, y) {
}
What are other terms for base class?
Parent Class or Super Class
What are other names for Sub Class?
Child Class or Derived Class
What does the “is” operator do?
It will return true if a variable is of a given type.
You use this to preform a type check.
Console.WriteLine(x is MapLocation);
will tell you T or F as to weather or not x is of the type MapLocation.
What are exceptions used for?
They are used to communicate that an error has happened.
How do you create an object of an exception that you’ve made?
In the catch declairation, you add a variable name in the parameters next to the exception’s name.
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}