Encapsulation binds together code and the data it
manipulates and keeps them both safe from outside interference and misuse. In object oriented programming methodology, it prevents access to implementation details. Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. Review…
Public access specifier allows a class to expose its member
variables and member functions to other functions and objects. Any public member can be accessed from outside the class. Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members. Review…
A property is simply a method which can help us add logic
to, when the value of a field is retrieved or set. We want to make sure that the value passed to our field is acceptable. It has two parts: get and set. The get part is called when we retrieve or get value from a field. The set part is called whenever we give or set a value to a field. namespace Properties { class Car { private int numberOfDoors; public int NumberOfDoors { get { return numberOfDoors; } set { if ( value >= 2 && value <= 6 ) numberOfDoors = value; else numberOfDoors = 2; } } class Program { static void Main(string[] args) { Car myCar = new Car (); myCar.NumberOfDoors=250; Console.WriteLine (myCar.NumberOfDoors); Console.ReadKey (); } } } Constructors in C#
A class constructor is a special
function or method that is automatically executed whenever we create new objects of that class. A constructor can run only once when a class is created. Furthermore, the code in the constructor always runs before any other code in a class. A constructor will have exact same name as the class and it does not have any return type. Object is being created Length of line : 6 A constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new.
Constructors may also take parameters. This technique is
called “overloading constructors” and helps to assign initial value to an object at the time of its creation.
Classes can define multiple constructors and is not required
to define a default constructor.
Overloaded methods (and constructors) must differ in their
signature. This means that they must have a different number of parameters or parameters having a different type (or both). Object is being created, length = 10 Length of line : 10 Length of line : 6 This class can be created by using either of the following statements:
Employee e1 = new Employee(30000);
Employee e2 = new Employee(500, 52); Inheritance
Inheritance allows us to define a class in terms of
another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
The idea of inheritance implements the “IS-A”
relationship. For example, mammal “IS-A” animal, dog “IS-A” mammal hence dog “IS-A” animal as well and so on. Inheritance
When creating a class, instead of writing
completely new data members and member functions, the programmer can designate that the new class to inherit the members of an existing class.
This existing class is called thebase class, and the
new class is referred to as the derived class. Syntax: Base and Derived Classes
The syntax used in C# for creating derived classes is as
follows: Protected and Internal Access Modifiers
Protected access specifier allows a child class to access
the member variables and member functions of its base class. This way it helps in implementing inheritance.
Internal access specifier allows a class to expose its
member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined. Total area: 35