Object Oriented Programming Guidebook in ASP.NET (OOP)

I've consolidated some of the key topics dealing with Object Oriented Programming in ASP.NET. Polymorphism, Inheritance, Encapsulation -- and their minions -- it's all here. Enjoy!

  1. What are the 3 primary characteristics (or pillars) of Object Oriented Programming (OOP)?
    1. Polymorphism
    2. Inheritance
    3. Encapsulation

Think PIE and make mine cherry. (Java uses A-PIE and includes Abstraction.)

See: http://msdn.microsoft.com/en-us/library/ms173149.aspx

  1. What does Inheritance enable you to do?

"Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes."

Pasted from <http://msdn.microsoft.com/en-us/library/ms173149.aspx>

You use inheritance to promote code reuse and to use polymorphism.

  1. What is a base class and a derived class? (Java uses superclass and subclass.)

"The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

When you define a class to derive from another class, the derived class implicitly gains all the members of the base class, except for its constructors and destructors. The derived class can thereby reuse the code in the base class without having to re-implement it. In the derived class, you can add more members. In this manner, the derived class extends the functionality of the base class."

Pasted from <http://msdn.microsoft.com/en-us/library/ms173149.aspx>

  1. What is Polymorphism?

Polymorphism allows an entity to have more than one form. Polymorphism is achieved through the use of Inheritance, Overloading, and Overriding.

"Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways."

Pasted from <http://msdn.microsoft.com/en-us/library/dd460654.aspx>

  1. What members are the basis for polymorphism?

Virtual and Abstract Members.

"When a base class declares a method as virtual, a derived class can override the method with its own implementation. If a base class declares a member as abstract, that method must be overridden in any non-abstract class that directly inherits from that class. If a derived class is itself abstract, it inherits abstract members without implementing them. "

Pasted from <http://msdn.microsoft.com/en-us/library/ms173149.aspx>

"The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class."

Pasted from <http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx>

  1. How do you declare a Base Class as Abstract?

"Classes can be declared as abstract. This is accomplished by putting the keyword abstract before the keyword class in the class definition."

Pasted from <http://msdn.microsoft.com/en-us/library/ms173150(v=vs.80).aspx>

"You can declare a class as abstract if you want to prevent direct instantiation by using the new keyword. If you do this, the class can be used only if a new class is derived from it. An abstract class can contain one or more method signatures that themselves are declared as abstract. These signatures specify the parameters and return value but have no implementation (method body). An abstract class does not have to contain abstract members; however, if a class does contain an abstract member, the class itself must be declared as abstract. Derived classes that are not abstract themselves must provide the implementation for any abstract methods from an abstract base class. For more information, see Abstract and Sealed Classes and Class Members (C# Programming Guide) and Abstract Class Design."

Pasted from <http://msdn.microsoft.com/en-us/library/ms173149.aspx>

  1. What is Abstraction?

Abstraction is like listing the bullet point features without providing explanatory details. It focuses on the interface or outside view of an object.

  1. What are Abstract and Sealed Classes?

"The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.

The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual. "

"An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. "

"A sealed class cannot be used as a base class. "

Pasted from <http://msdn.microsoft.com/en-us/library/ms173150.aspx>

  1. What is an Interface?

"Interfaces describe a group of related functionalities that can belong to any class or struct. You define an interface by using the interface keyword."

Pasted from <http://msdn.microsoft.com/en-us/library/ms173156.aspx>

"An interface is a reference type that is somewhat similar to an abstract base class that consists of only abstract members. When a class implements an interface, it must provide an implementation for all the members of the interface. A class can implement multiple interfaces even though it can derive from only a single direct base class.

Interfaces are used to define specific capabilities for classes that do not necessarily have an "is a" relationship."

Pasted from <http://msdn.microsoft.com/en-us/library/ms173149.aspx>

  1. What member types are found in an Interface?

"Interfaces consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers."

Pasted from <http://msdn.microsoft.com/en-us/library/ms173156.aspx>

  1. "An interface has the following properties:
    • An interface is like an abstract base class: any non-abstract type that implements the interface must implement all its members.
    • An interface cannot be instantiated directly.
    • Interfaces can contain events, indexers, methods, and properties.
    • Interfaces contain no implementation of methods.
    • Classes and structs can implement more than one interface.
    • An interface itself can inherit from multiple interfaces."

Pasted from <http://msdn.microsoft.com/en-us/library/ms173156.aspx>

  1. What are the two distinct aspects of polymorphism?
  1. At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this occurs, the object's declared type is no longer identical to its run-time type.
  1. Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed."

Pasted from <http://msdn.microsoft.com/en-us/library/ms173152.aspx>

  1. Polymorphism manifests itself with Overloading and Overriding:

Overloading: Multiple methods have the same name with different arguments that support the same semantic operation.

"Method overloading occurs when a class contains two methods with the same name, but different signatures. "

Pasted from <http://msdn.microsoft.com/en-us/library/xxfyae0c(v=vs.71).aspx>

Overriding: Multiple methods have the same name, return type and arguments, but can be overridden through inheritance.

"A type may override an inherited overridable method by declaring a method with the same name and signature, and marking the declaration with the Overrides modifier. Whereas an Overridable method declaration introduces a new method, an Overrides method declaration replaces the inherited implementation of the method.

An overriding method may also be declared NotOverridable, which prevents any further overriding of the method in derived types. In effect, NotOverridable methods become non-overridable in any further derived classes. "

Pasted from <http://msdn.microsoft.com/en-us/library/aa711875(v=vs.71).aspx>

  1. What is Encapsulation?
    "Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object."

Pasted from <http://msdn.microsoft.com/en-us/library/dd460654.aspx>

Encapsulation hides the implementation details and prevents access of the properties and methods of the encapsulated object. Encapsulation implements the Abstraction interface.


1 Comment

Comments have been disabled for this content.