Oops basics part 2

we aim to write easily maintainable and reusable pieces of code that can perform collectively very complex tasks.

11) Properties :

Properties are in extremely common use, and can significantly simplify the external user interface exposed by classes. Properties exist for the situation in which you want to make a method call look like a field. A property is a method or pair of methods that are exposed to the outside world as if they are fields.

Example :


12) Data encapsulation :
In OOP, we aim to make it so that users of objects only need to know what an object does, not how it does it. So making fields directly accessible to users defeats the ideology behind OOP.

If we make fields directly visible to external users, we lose control over what they do to the fields. They might modify the fields in such a way as to break the intended functionality of the object (give the fields in appropriate values, let’s say).

However, if we use properties to control access to a field, this is not a problem because we can add functionality to the property that checks for inappropriate values. Related to this, we can also provide read-only properties by omitting the set accessor completely. The principle of hiding fields from client code in this way is known as data encapsulation.


13) Inheritance

C# is it supports both types of inheritance. implementation inheritance & interface inheritence

Example

public class Nevermore60Customer : Customer
{
}
This tells the compiler that Nevermore60Customer is derived from Customer or we can say that each member of Customer is inherited in Nevermore60Customer. Also, Nevermore60Customer is said to be a derived class, while Customer is said to be the base class

The base class itself is never implicitly modified in any way by the existence of the derived class. This must always be the case, because when you code the base class, you don’t necessarily know what other derived classes might be added in the future—and you wouldn’t want your code to be broken when someone adds a derived class!

Override keyword :

access modifier protected :

It indicates that any class that is derived
from Customer, as well as Customer itself, should be allowed access to this member. The member is still invisible, however, to code in any other class that is not derived from Customer. Essentially, we’re assuming that, because of the close relationship between a class and its derived class, it’s fine for the derived class to know a bit about the internal workings of the base class, at least as far as protected members are concerned.


virtual keyword:

C# will not allow derived classes to override a method unless that method has been declared as virtual in the base class.



Note : As a rule that is enforced by .NET and C#: All .NET classes must ultimately derive from a base class called Object. In C# code, if you write a class and do not specify a base class, the compiler will supply System.Object as the base class by default.

This means that all objects in the .NET Framework have certain methods inherited from the Object class, including the ToString() and GetType() methods



14 ) Single and multiple inheritance :

In C#, each derived class can only inherit from one base class (although we can create as many different classes that are derived from the same base class as we want). The terminology to describe this is single inheritance. Some other languages, including C++, allow you to write classes that have more than one base class, which is known as multiple inheritance.

15) Method Hiding

Even if a method has not been declared as virtual in a base class, it is still possible to provide another method with the same signature in a derived class. The signature of a method is the set of all information needed to describe how to call that method: its name, number of parameters, and parameter types. However, the new method will not override the method in the base class. Rather, it is said to hide the base class method.

If a method hides a method in a base class, then you should normally add the keyword new to its definition. Not doing so does not constitute an error, but it will cause the compiler to give you a warning.

16) Abstract class

Every time you defined a class you will actually create instances of that class, but that’s not always the case. In many situations, you’ll define a very generic class from which you intend to derive other, more specialized classes but don’t ever intend to actually use. C# provides the keyword abstract for this purpose. If a class is declared as abstract it is not possible to instantiate it.

Example :

abstract class MyBaseClass

In this case the following statement will not compile:

MyBaseClass MyBaseRef = new MyBaseClass();

However, it’s perfectly legitimate to have MyBaseClass references, so long as they only point to derived classes. For example, you can derive a new class from MyBaseClass:

class MyDerivedClass : MyBaseClass
{
...
In this case, the following is perfectly valid code:

MyBaseClass myBaseRef;
myBaseRef = new MyDerivedClass();

17) Abstract methods

It’s also possible to define a method as abstract. This means that the method is treated as a virtual method, and that you are not actually implementing the method in that class, on the assumption that it will be overridden in all derived classes. If you declare a method as abstract you do not need to supply a method body:

Note : If any method in a class is abstract, then that implies the class itself should be abstract, and the compiler will raise an error if the class is not so declared. Also, any non-abstract class that is derived from this class must override the abstract method. These rules prevent you from ever actually instantiating a class that doesn’t have implementations of all its methods.


18) What is the use of abstract methods and classes?

They are extremely useful for two reasons. One is that they often allow a better design of class hierarchy, in which the hierarchy more closely reflects the situation you are trying to model. The other is that the use of abstract classes can shift certain potential bugs from hard-to-locate runtime errors into easy-to-locate compile-time errors.


19) Sealed Classes and Methods

Methods and classes that cannot be overridden or inherited from.C# also supports declaring an individual override method as sealed, preventing any further overrides
of it.

The most likely situation when you’ll mark a class or method as sealed will be if it is very much internal to the operation of the library, class, or other classes that you are writing, so you are fairly sure that any attempt to override some of its functionality causes problems. You might also mark a class or method as sealed for commercial reasons, in order to prevent a third party from extending your classes in a manner that is contrary to the licensing agreements.


20) Interfaces

In general, an interface is a contract that says that a class must implement certain features (usually methods and properties), but which doesn’t specify any implementations of those methods and properties. Therefore you don’t instantiate an interface; instead a class can declare that it implements one or more interfaces. In C#, as in most languages that support interfaces, this essentially means that the class inherits from the interface.

Example :

interface IEnumerator
{
// Properties
object Current {get; }

// Methods
bool MoveNext();
void Reset();
}

No comments:

Post a Comment