Prep OOP Object-Oriented Programming

  1. What is OOP?
    Object-oriented programming (OOP) is a computer programming model that organizes software design around objects rather than functions and logic. An object can be defined as having both data and behavior in a single, customized entity.
  2. What are OOP pro's and con's?
    It's been said that in order to program a banana using OOP, you have to create the banana, an ape, and the whole jungle.

    What these means is that OOP can be overly large, complex, and unnecessary. 

    Benefits to OOP come from its foundational characteristics which promote decoupled, extensible, testable, and more maintainable code.
  3. What are the 4 pillars of OOP?
    • Abstraction: Ability to hide information 
    • Inheritance: Ability to inherit methods and properties of a parent.
    • Polymorphism: Ability to take on many shapes or change the behavior of a previous defined method.
    • Encapsulation: Ability to collectively put both data and methods into a single customized entity.
  4. Difference between a class and a struct?
    Class is a reference type whereas a struct is a value type. Because of this, large objects can be passed without lots of overhead using a class but those same objects can be unbearably monstrous using a struct which are mainly good for simple, small structures.
  5. Define encapsulation.
    Encapsulation is an object-oriented programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.
  6. Define polymorphism. How is this done in C#?
    Polymorphism is a Greek word, meaning "one name many forms". In other words, one object has many forms or has one name with multiple functionalities. "Poly" means many and "morph" means forms. Polymorphism provides the ability to a class to have multiple implementations with the same name.

    Achieved using either an interface or an abstract class (method overriding).

    Two ways of achieving polymorphism exist:

    (a) method overloading (also known as compile-time polymorphism or static polymorphism) is where two methods have the same name but different parameters.

    (b) method overriding (also known as run-time polymorphism or dynamic polymorphism) which uses a base class and a concrete class and the 'virtual' and 'override' keywords to rewrite the functionality of a method within its inherited class.
  7. When creating an abstract class, how do you
    (a) allow a method to be overwritten?
    (b) prevent a method from being overwritten? 
    (c) prevent a class from being inherited/modified?
    (d) require a method in the subclass provide implementation?
    (e) give the subclass the option to override or not?
    (f) allow both class to have the same method name without overriding?
    • (a) use the keyword 'virtual' in method signature
    • (b) do not use the keyword 'virtual' (default behavior) 
    • (c) use the keyword 'sealed' in the class definition
    • (d) declare the method abstract
    • (e) use the keyword 'virtual' in method signature
    • (f) the parent class must have 'private' accessor
  8. Say Class A is the base class and then you have class B subclass of A (class B : A). Then you have class C : B.

    If you have a method in class A, can class C use that method?
    Yes, as long as the method is public or protected (not private). Inheritance is transitive. If C is derived from B, and B is derived from A, then C inherits the members declared in B as well as the members declared in A. A derived class extends its direct base class. A derived class can add new members to those it inherits, but it cannot remove the definition of an inherited member.
  9. Create an abstract class with five method types (a) plain (b) abstract (c) virtual [which will be overridden] (d) virtual [which will be overridden and sealed], and (e) virtual [which won't be overridden]. Then create a subclass which correctly codes against the five methods above and can't be changed.

    The concrete class must inherit and create 3-4 identical method signatures
    • public abstract class ClassA
    • {
    •   private void MethodA() { }
    •   public abstract void MethodB();
    •   public virtual void MethodC() { }
    •   public virtual void MethodD() { }
    •   public virtual void MethodE() { }
    • }

    • public sealed class ClassB: ClassA
    • {
    •   public void MethodA() { }
    •   public override void MethodB() { }
    •   public override void MethodC() { }
    •   public sealed override void MethodD() { }
    • }

    Note: No implementation is provided in ClassB for the MethodE method as requested.
  10. How do you override an abstract method? How do you override a virtual method?
    You use the 'override' keyword in the method declaration. Both method overrides are done exactly the same.
  11. What is an abstract method? What is a virtual method? What's the difference?
    An abstract method is a method within a base class that has no implementation at all (hence the name abstract). 

    A virtual method is one which has default implementation which may or may not be overridden in the subclass.

    The difference is one provides implementation while the other does not.
  12. Why do we use the term 'abstract' in some classes or methods? Explain.
    The term 'abstract' is used because what it is describing is abstract in nature and provides either no implementation or limited implementation.

    That is, we may call an abstract class Shape() or FourLeggedAnimal() wherein the inheriting class is concrete and will be specific, such as Square(), Triangle(), Dog(), Cow().
  13. What is the difference between an interface and an abstract class?
    • (a) Neither can be initialized. 
    • (b) Interface provides for multiple inheritance
    • (c) Interfaces provide no implementation
    • (d) Abstract class must have at least one abstract method
    • (e) All methods in Interface are public
    • (f) Abstract method can have constructors, destructors, indexers, private methods/fields
  14. Write an abstract class AND an interface called Shape. Use Area, GetArea(), and Print() if logical.
    • public abstract class Shape
    • {
    •   public double Area { get; set; }
    •   public abstract void GetArea();
    •   public virtual void Print(string name)
    •   {
    •     Console.WriteLine("The {0} has an area of {1}", name, Area);
    •   }
    • }

    • public interface Shape
    • {
    •   public double Area { get; }
    •   public void GetArea();
    •   public void Print(string name);
    • }
  15. Anything wrong with this interface?

    public interface Shape
      public abstract double Area { get; }
      public abstract void GetArea();
      public abstract void Print(string name);
    No. We have explicitly used the words Public and abstract and neither are required but it is not an error as all elements in an interface are both public and abstract (no implementation).
Card Set
Prep OOP Object-Oriented Programming
OOP Design, Inheritance, Abstract Classes, Polymorphism, Interfaces..