Oops basics part 1

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

1) An object is anything that is identifiably a single material item. An object can be a car, a
house, a book, a document or a car radio. Most people don’t know exactly how a car radio works; however, they do know what it does and how to operate it.

2) In programming we break each program into lots of units and design each unit to perform a clearly specified role within the program. That’s basically what an object is.

3) If oops principals are used properly while programming then it becomes easier for multiple developers to work together, since they can work on different objects in the code; all they need to know is what an object can do and how to interface with it. They don’t have to worry about the details of how the underlying code works.

4) Difference between a class and object

In programming, we need to distinguish between a class and an object. A class is the generic definition of what an object is—a template. For example, a class could be “car radio”—the abstract idea of a car radio. The class specifies what properties an object must have to qualify as a car radio.


5) Class members

In general, a class is defined by its fields and methods. there are two sides to an object: what it does, which is usually publicly known, and how it works, which is usually hidden.
In programming, the “what it does” is normally represented in the first instance by methods, which are blocks of functionality that you can use. A method is just C# parlance for a function. The “how it works” is represented both by methods and by any data (variables) that the object stores. In C# the terminology is fields

6) Access Modifiers

Marking a field or method as private effectively ensures that that field or method will be part of the internal working of the class, as opposed to the external interface. The advantage of this is that if you d ecide to change the internal working (perhaps you later decide not to store password as a string but to use some other more specialized data type), you can just make the change without breaking the code out side the Authenticator class definition—nothing from outside of this class can access this field.



7) Creating a class instance

Authenticator myAccess = new Authenticator();

[ = new Authenticator() ] is a part of C# syntax, and is there because in C#, classes are always
accessed by reference.

We could actually use the following line if we just wanted to declare a new Authenticator object called myAccess:

Authenticator myAccess;

This declaration can hold a reference to an Authenticator object, without actually creating any object (in much the same way that the line Dim obj As Object in Visual Basic doesn’t actually create any object).

The new operator in C# is what actually instantiates an Authenticator object.




8) Why do we decalre some fields as static?

To indicate that a field should only be stored once, no matter how many instances of the class we create, we place the keyword static in front of the field declaration in our code:

private static uint minPasswordLength = 6;

By declaring the field as static, we ensure that it is only stored once, and this field is shared among all instances of the class. Fields declared with the static keyword are referred to as
static fields or static data, while fields that are not declared as static are referred to as instance fields or instance data

Important : If a field has been declared as static, then it exists when your program is running from the moment that the particular module or assembly containing the definition of the class is loaded—that is as soon as your code tries to use something from that assembly, so you can always guarantee a static variable is there when you want to refer to it. This is independent of whether you actually create any instances of that class. By contrast, instance fields only exist when there are variables of that class currently in scope—one set of instance fields for each variable.


Also : static keyword is independent of the accessibility of the member to which it
applies. A class member can be public static or private static.

Also : However, just as with fields, it is possible to declare methods as static, provided that they do not attempt to access any instance data or other instance methods.



9) Ovwerloading a method :

To overload a method is to create several methods each with the same name, but each with a different signature. The reason why you might want to use overloading is best explained with an example. Consider how in C# we write data to the command line, using the Console.WriteLine() method.

Console.WriteLine() can display int, strings and lot of other datatypes since there are many Console.WriteLine() overloads

When to use overloading : Generally, you should consider overloading a method when you need a number of methods that take different parameters, but conceptually do the same thing, as with Console.WriteLine() above.


10) Different output types : Out parameter

occasionally you might have a method that calculates or obtains some quantity, and depending on the circumstances, you might want this to be returned in more than one way. You cannot distinguish overloads using the return type of a method. However, you can do so using out parameters.

void GetAircraftLocation(DateTime Time, out string Location)
{
...
}
void GetAircraftLocation(DateTime Time, out float Latitude, out float Longitude)
{
...
}

Note: however, that in most cases using overloads to obtain different out parameters does not lead to an architecturally neat design. In the above example, a better design would perhaps involve defining a Location struct that contains the location string as well as the latitude and longitude and returning this from the method call, hence avoiding the need for overloads.

No comments:

Post a Comment