Here I would like to explain about oops concepts.
Class: Class is the collection of namespaces and methods, fields and properties .class is place we can have access modifiers to access to class as security purpose.
class Customers
Customers objCustomer=new Customers();
Methods: Methods are like function which we can call based on requirements.
Constructors: Constructors are two types
Class: Class is the collection of namespaces and methods, fields and properties .class is place we can have access modifiers to access to class as security purpose.
class Customers
{
}
Object: Object meaning normal its thing like book, laptop, but
object is the instance of the class. We can create as many as instances from
the class based on requirement and we can also restrict user to create
instances of the class using oops concepts.Customers objCustomer=new Customers();
Methods: Methods are like function which we can call based on requirements.
Constructors: Constructors are two types
Private
Static
Private: A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. For example:
Private: A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. For example:
class Utility
{
// Private Constructor:
private Utility ()
{ }
public static double e =
Math.E; //2.71828...
}
Static constructors have the following properties:
Static constructors have the following properties:
- A static
constructor does not take access modifiers or have parameters.
- A static constructor is called
automatically to initialize the class before the
first instance is created or any static members are referenced.
- A static
constructor cannot be called directly.
- The user
has no control on when the static constructor is executed in the program.
- A typical use of
static constructors is when the class is using a log file and the
constructor is used to write entries to this file.
- Static
constructors are also useful when creating wrapper classes for unmanaged
code, when the constructor can call the Load Library method.
- If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
class Utility
{
// Private Constructor:
static Utility ()
{ }
public static double e = Math.E; //2.71828...
}
Inheritance: The process of the sub-classing a class to extend its functionality is called Inheritance. Using Inheritance we can achieve reusability and extensibility.
{ }
public static double e = Math.E; //2.71828...
}
Inheritance: The process of the sub-classing a class to extend its functionality is called Inheritance. Using Inheritance we can achieve reusability and extensibility.
- Reusability is property of a module that’s enabled to used in different applications without any source code changes.
- Extensibility of the module is its potential extend as new needs evolve.
- Reusability in OOP Languages is achieved by reducing coupling between different classes.
- Extensibility is achieved by sub classes.
Ex: I have class called student Class which
is having registration Number, name, and dateOfBirth these are all common
fields, properties and getAge() method in the class.
class Student
{
private int registrationNumber;
private string name;
private DateTime dateOfBirth;
public Student()
{
Console.WriteLine("New
Student Created. Parameterless constructors called..");
}
public Student(int registrationNumber, string name, DateTime dateOfBirth)
{
this.registrationNumber
= registrationNumber;
this.name
= name;
this.dateOfBirth
= dateOfBirth;
Console.WriteLine("New
Student Created.Parameterized Constructor called...");
}
public int RegistrationNumber
{
get
{
return registrationNumber;
}
set
{
registrationNumber = value;
}
}
public string
Name
{
get
{
return name;
}
set
{
name = value;
}
}
public DateTime DateOfBirth
{
get
{
return dateOfBirth;
}
set
{
dateOfBirth = value;
}
}
public int GetAge()
{
int age
= DateTime.Now.Year - dateOfBirth.Year;
return age;
}
}
SchoolStudent (Sub class) Inherits the Student (Parent Class) Class common variables should be able to access in the subclass.
SchoolStudent (Sub class) Inherits the Student (Parent Class) Class common variables should be able to access in the subclass.
class SchoolStudent:Student
{
public SchoolStudent(int regNum, string name, DateTime dob, int totalMarks, int intTotalObtainedMarkS):base(regNum,name,dob)
{
this.totalMark = totalMarks;
this.totalObtainedMarks
= totalObtainedMarks;
Console.WriteLine("New
School student created. Parameterless Constructor called...");
}
}
Polymorphism:
Polymorphism means having more than one form .Overloading and Overriding are use to implement polymorphism .Polymorphism is classified into two types.
- Compile time
polymorphism or early binding or static binding
- Runtime time polymorphism or late binding or dynamic binding
Method Overloading:
- Compile time
binding also known as Method Overloading.
- Method Overloading means calling method with the same name andd different signature.
class MethoOverloading
{
public void Message()
{
Console.WriteLine("Hi
This Message from MethodA");
}
public int void Message(int
i)
{
Console.WriteLine("Hi
This Message from MethodA");
}
public int void Message(int
I, int j)
{
int k=i+j;
Console.WriteLine("Hi
This Message from MethodA"+k.toString(););
}
}
Method Overriding:
- Runtime time
polymorphism or late binding or dynamic binding also known as Method
Overloading.
- Method
Overriding means call method with same name and different signature.
- We can
achieve Method overriding using Inheritance.
- We use Virtual
keyword in Parent class which allows to Override.
- We use Override Keyword in Derived class which helps to implement its own functionality.
class PolymorphismA
{
public virtual void Message()
{
Console.WriteLine("Hi
This Message from MethodA");
}
}
class PolymorphismB:PolymorphismA
{
public override void Message()
{
Console.WriteLine("Hi
This message is from PolymorphismB");
}
}
No comments:
Post a Comment