OOPS:
----
OOP is a design philosophy. It stands for Object Oriented Programming.
Object-Oriented Programming (OOP) uses a different set of programming languages
than old procedural programming languages (C, Pascal, etc.).
Everything in OOP is grouped as self sustainable "objects".
Hence, you gain re-usability by means of four main object-oriented programming concepts.
The main use of the oops is sharing and security is possible in oops concepts.
the main things in the oops are class and object
OBJECT:
-------
An object can be considered a "thing" that can perform a set of related activities.
The set of activities that the object performs defines the object's behavior.
For example, the hand can grip something or a Student (object) can give the name or address.
In pure OOP terms "an object is an instance of a class. "
CLASS:
------
Class is a blue print .
the class compressed of the member data(class level variables) member function
in the class creation time no memorey is allocated for the class member variables
onley the object creation time the memorey is allocated for the variable.
The features of the oops are:
1.abstraction
2.encapuslation
3.inhiritence
4.polymorphism
1.Abstraction:
-------------
Abstraction is another good feature of OOPS.
Abstraction means to show only the necessary details to the client
of the object.
ex:Do you know the inner details of the Monitor of your PC? What happen when you switch ON Monitor?
Does this matter to you what is happening inside the Monitor?
No Right, Important thing for you is weather Monitor is ON or NOT.
When you change the gear of your vehicle are you really concern about the inner details of your vehicle engine?
No but what matter to you is that Gear must get changed that’s it!! This is abstraction;
show only the details which matter to the user.
def2:
-----
Abstraction" simply means showing only those details to the user which are of use to them ,
and hiding up the unnecessary
demo:public class Mammal
{
public string m_color; // Abstract Charactersistic 1
public string m_height; // Abstract Charactersistic 2
public string m_weight; // Abstract Charactersistic 1
public Mammal()
{
}
public void Move()
{
// Abstract behaviour
}
}
// Elephant is a Mammal hence it extends abstract class Mammal
public class Elephant : Mammal
{
public string m_color = "gray"; // Gray color
public int m_height = 120; // 10 feet or 120 inches
public int m_weight = 1500; // 1500 pounds
public Elephant()
{
}
public void Move()
{
// Implement the Elephant Walks Behaviour
}
}
// A whale is also a mammal hence it extends the Mammal class
public class Whale: Mammal
{
public string m_color = "darkgray"; // Dark Gray color
public int m_height = 60; // 5 feet or 60 inches
public int m_weight = 2000; // 2000 pounds
public Whale()
{
}
public void Move()
{
// Implement the way a Whale swims
}
}
2.encapuslation:
----------------
Encapsulation is the procedure of covering up of data and functions into a single unit (called class).
An encapsulated object is often called an abstract data type.
NEED FOR ENCAPSULATION:
----------------------
The need of encapsulation is to protect or prevent the code (data) from accidental
corruption due to the silly little errors that we are all prone to make.
In Object oriented programming data is treated as a critical element in the program development
and data is packed closely to the functions that operate on it and protects it from accidental modification
from outside functions.
implementing encapsulation by ushing the acess specifiers:
----------------------------------------------------------
an acess specifier defines the scope of a class member.
TYPES OF ACCESS SPECIFIERS:
---------------------------
C# supports the following acess specifiers:
-->public
-->private
-->protected
-->internel
-->protected internel
1.THE PUBLIC ACESS SPECIFIER:
-----------------------------
the public acess specifier allows a class to share its member variables and member functions with other classes(within or outside
the application).Any member that is declared public can be accessed from outside the class.
ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace public_acess_specifier_demo1
{
class car
{
private string carcolor; //since the variable is private ,it
//cannot be accessed outside the class defination.
class bike
{
public string bikecolor;//since the variable is public ,
//it can be acessed outside the class defination
}
class program
{
public static void Main(string[] args)
{
car obj1 = new car();
bike obj2 = new bike();
//obj1.carcolor = "red";//error bcz we can not acess private members
obj2.bikecolor = "yellow";// not error bcz we can acess anyware in the application becoz this is
//public variable
}
}
}
}
THE PRIVATE ACESS SPECIFIER:
---------------------------
the private acess specifier allows a class to hide its member variables and member functions from other class
object and functions
therefore the private members of a class are not visible outside the class.
ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace private_acess_specifier
{
class car
{
private string model;
void hunk()
{
Console.WriteLine("hi this is hunk");
}
public void setModel()
{
Console.WriteLine("enter the model name:");
model = Console.ReadLine();
}
public void displayModel()
{
Console.WriteLine("the model is :");
}
}
class abc
{
static void Main(string[] args)
{
car obj1 = new car();
obj1.setModel(); //accepts the model name
obj1.displayModel(); //display the model name
// obj1.honk();//error becoz this is private variable
// Console.WriteLine(obj1.model);//error becoz private variable
Console.ReadLine();
}
}
}
PROTECTED ACESS SPECIFIER:
--------------------------
the protected acess specifier allows a class to hide its member variables and
member functions from other class objects and functions except the child class.
this concept is importent while implementing the concept of inheritance
ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace protected_access_specifier_demo
{
class car
{
protected string model;
void honk()
{
Console.WriteLine("hi i am honk ");
}
public void setmodel()
{
Console.WriteLine(" enter the model name:");
model = Console.ReadLine();
}
public void displaymodel()
{
Console.WriteLine("the model is:");
}
}
class abc
{
static void Main(string[] args)
{
car abc = new car();
abc.setmodel();//accepts the model name
abc.displaymodel();//displays the model name
// abc.honk();//error bcz honk is private access specifier
//Console.WriteLine(abc.model);//error becoz it is a protected access specifier
}
}
}
INTERNAL ACCESS SPECIFIER:
--------------------------
THE internal access specifier allows a class to expose its member variables and member functions to other
class functions and objects with in the application onley
the default access specifier for a class is internal
ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace internal_access_specifier_demo
{
class car
{
private string carcolor;
internal void honk()
{
Console.WriteLine("hi i am honk ");
}
}
class bike
{
internal string bikecolor;//it is internal
//it can be accessed outside the class defination
}
class abc
{
public static void Main(string[] args)
{
bike obj1 = new bike();
car obj2 = new car();
//obj2.carcolor = "red";// error can not acess private members
obj1.bikecolor = "blue";
obj2.honk();//display the message
}
}
}
protected internal acess specifier:
-----------------------------------
the protected internal access specifier allows a class to expose its member variables and
member functions to the containing classes or class with in the same application
in addition ,it allows to access to the derived class outside the application
ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace protected_internal_demo
{
class car
{
protected internal string model;
void honk()
{
Console.WriteLine("hi helloooo");
}
public void setmodel()
{
Console.WriteLine(" enter the model name:");
model = Console.ReadLine();
}
public void displaymodel()
{
Console.WriteLine("the model is : ");
}
class display
{
static int Main(string[] args)
{
car abc = new car();
abc.setmodel();
abc.displaymodel();
abc.honk();
Console.WriteLine(abc.model);
return 0;
}
}
}
}
3.INHERITANCE:
--------------
-->it is the prime feature of the oops .
-->the process of creating the new classes from already existing class.
-->in the inheritance process existing class is known as the parent/base class,
neweley created class is known as derived class /child class.
-->main purpose of the inheritance is code re-usability and providing the
additional functionality
types of inheritance:
--------------------
-->single inheritance
-->multiple inheritance.
-->multilevel inheritance.
-->hybrid inheritance
-->hyerarcial inheritance
single inheritance:
-------------------
Creating a single new class from single base class is known as single inheritance
ex: [A]
^
|
|
[B]
Multiple Inheritance:
---------------------
creating the new class from two or more base classes is known as multiple inheritance
ex: [a] [b]
^ ^
|_______|
|
[c]
multilevel inheritance:
------------------------
creating a new class from already derived class is known as multilevel inheritance
ex:
[a]
^
|
|
[b]
^
|
|
[c]
hyerarcial inheritance: this is the combination of both multiple and multilevel inheritance
------------------------
ex: [a] [b]
^ ^
| |
|____________|
|
[c]
^
|
[d]
polymorphism:
------------
the term polymorphisam was derived from greek words 'poly'means 'maney' and 'morphos' means
'forms'.
polymorphism is the ability to allow a function to exist in different forms.
the polymorphism have two types :
1.static polymorphism
2.Dynamic polymorphism
1.static polymorphism:
---------------------
in static polymorphism the response to a function is decided at "compile time"
c# uses following approaches to implement static polymorphisam these are
->function overloading for implementing static polymorphism
-->operator overloading for implementing static polymorphism
function overloading:
--------------------
function overloading allows you to use same name for two or more functions.each of these functions having the same name
must use different function signature.
the signature function is defined by
void addnum(int)
void addnum(int,float)
void addnum(float,float)
the above code the three functions are different because their number and type of parameters is different
ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace fun_and_constructor_overloading_demo
{
class arithematic
{
int a, b, result;
static int c;
static arithematic()//static constructor
{
c = 20;
Console.WriteLine("static constructor invoked");
}
public arithematic()
{
a = 10;
b = 3;
Console.WriteLine("default constructor invoked");
}
public arithematic(int p, int q)
{
a = p;
b = q;
Console.WriteLine("2parametarised constructor invoked");
}
public arithematic(int p)
{
a = p;
b = 0;
Console.WriteLine("1 parametarised constructor invoked");
}
public arithematic(int a, int b, int c)
{
this.a = a;
arithematic.c=c;
this.b = b;
Console.WriteLine("3 param constructor invoked");
}
public void add()
{
int result=a+b+c;
Console.WriteLine("sum is :{0}",result);
}
static void Main(string[] args)
{
arithematic obj = new arithematic();//static ,default
obj.add();
arithematic obj1=new arithematic(3);//1 param
obj1.add();
arithematic obj2 = new arithematic(2, 1);//2 params const
obj2.add();
arithematic obj3 = new arithematic(2, 1,8);
obj3.add();
Console.ReadLine();
}
}
}
operator overloading:
---------------------
The concept of overloading a function can also be applied to operators .operator overloading
provides additional capabilities to c# operators when they are applied to userdefined data types.
ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace operator_overloding
{
class test
{
int a, b;
public test()
{
}
public test(int p, int q)
{
a = p;
b = q;
}
public void display()
{
Console.WriteLine("a value {0}\t b value{1}", a, b);
}
public static test operator +(test x, test y)
{
test temp = new test();//hi
temp.a = x.a + y.a;
temp.b = x.b + y.b;
return temp;
}
public static test operator -(test x,test y)
{
test temp = new test();//hi
temp.a =x.a-y.a;
temp.b=x.b-y.b;
return temp;
}
public static test operator *(test x, test y)
{
test temp = new test();
temp.a = x.a * y.a;
temp.b = x.b * y.b;
return temp;
}
public static test operator /(test x, test y)
{
test temp = new test();
temp.a = x.a/y.a;
temp.b = x.b/y.b;
return temp;
}
public static test operator++(test x)
{
x.a++;
x.b++;
return x;
}
public static test operator --(test x)
{
x.a--;
x.b--;
return x;
}
}
class prog
{
static void Main(string[] args)
{
test t1 = new test(4, 6);
test t2=new test (2,3);
test t3,t4,t5,t6,t7,t8,t9,t10;
t3=t1+t2;
t4 =t1-t2;
t5 = t1 * t2;
t6 = t1 / t2;
t1.display();
t2.display();
t3.display();
t4.display();
t5.display();
t6.display();
t7 = ++t1;
t8 = t1++;
t1.display();
t8.display();
t9 = --t2;
t2.display();
t10 = t2--;
t2.display();
t10.display();
Console.ReadLine();
}
}
}
2.Dynamic polymorphism:
-----------------------
in dynamic polymorphism the response to the function is decided at "runtime".
c# uses following approaches to implement static polymorphisam these are:
1.Abstract classes for implementing Dynamic polymorphism.
2.virtual functions for implementing Dynamic polymorphism.
Abstract classes:
------------------
a class which contains one or more "abstract functions" is known as an abstract class
--> to make any class as abstract use "abstract" keyword.
-->an abstract class can't be instantitated directley.
-->it is compulsory to create /derive a new class from an abstract class in order to provide functionality to its
abstract functions
-->an abstract class can contain non abstract functions.
-->an abstract class can contain all members of a class
-->by default abstract class functions are not treated as public and abstract.
abstract function:
-------------------
-->a function which contains onley declaration /signature and doesn't contain implementation/body/defination
is known as ABSTRACT FUNCTION
-->To make any function as abstract use 'abstract' keyword
-->an abstract function should be terminated .
-->Overriding of an abstract function is compulsory.
VIRTUAL FUNCTIONS:
------------------
WE are providing new functionality for the base class functions in derived class and base class functions are not usefull
now so base class functions are known as VIRTUAL FUNCTIONS and derived class are known as overriding functions.
-->to make any function as VIRTUAL keyword.
-->to override any virtual function in derived class use override keyword.
-->virtual functions may or mayn't override.
THE VIRTUAL FUNCTIONS and Abstract classes will create the bridge from parent to child
simple example for VIRTUAL FUNCTIONS and Abstract classes
parent class : child class
--------------------- --------------------
wish() wish()
add() mul()
sub() divide()
msg()
object creation for the parent and child classes for functions
case 1: parent obj1=new parent();
obj1.wish() ;
obj1.add() ;
obj1.sub() ;
obj1.msg() ;
case 2: parent obj2=new child();
obj2.wish();
case3: child obj3=new child();
obj3.wish() ;
obj3.add() ;
obj3.sub() ;
obj3.msg() ;
obj3.mul() ;
obj3.divide();
demo :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace abstract_virtuval
{
abstract class parent
{
public virtual void display()
{
Console.WriteLine("display from parent");
}
public void wish()
{
Console.WriteLine("wish from parent");
}
public abstract void sub();
public void add()
{
Console.WriteLine(" add from parent");
}
public void mul()
{
Console.WriteLine("mul from parent");
}
public void msg()
{
Console.WriteLine("hi hello i am from parent");
}
}
class child : parent
{
public override void display()
{
Console.WriteLine("display from child ");
}
public override void sub()
{
Console.WriteLine(" sub from child");
}
public void div()
{
Console.WriteLine(" div from child");
}
}
class program
{
static void Main()
{
child obj3 = new child();
obj3.wish();
obj3.add();
obj3.sub();
obj3.mul();
obj3.msg();
obj3.div();
Console.ReadLine();
}
}
}
NOTE:
Parent class can hold the child class memorey(but not vice versa)
-->when the function is present in the parent class and virtuval state in parent
then it goes to child function and executes
overriding:
----------
when a parent class function is redefined from the child class with the same
function signature
abstract:
--------
any function without body name as abstract
constructor:
-----------
a constructor is a special member function.
-->a constructor is a member method of a class which is invoked automaticalley when an object to the class is created
-->constructor name should be the same as the class name.
-->constructor doesn't have any return type even void also .
two types of constructors are supported by the c#
1.instance constructor
2.static constructor
instance constructor:
--------------------
an instance constructor is called whenever an instance if a class is created .these constructors are used to initilize
the member variables of the class .
static constructor:
------------------
static constructors are used to initilize the static variables of the class.these variables are created by
using the "static "keyword and they store values that can be shared by all the instance of a class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace op2
{
class calculate
{
static int number1;
public void display(int number)
{
Console.WriteLine(number);
}
calculate()
{
number1++;
display(number1);
}
static calculate()
{
number1 = 10;
number1++;
}
static void Main(string[] args)
{
calculate cal1=new calculate();
Console.ReadLine();
}
}
}
parameterized constructor:
--------------------------
-->a parameterized constructor accepts arguments to store the values in to the data fields.
-->using the parameterized constructor we can store different set of values into different objects
created to the class
DESTRUCTOR:
-----------
?Destructors? are used to destruct instances of classes. When we are using destructors in C#, we have to keep in mind the following things:
A class can only have one destructor.
Destructors cannot be inherited or overloaded.
Destructors cannot be called. They are invoked automatically.
A destructor does not take modifiers or have parameters.
garbage collector:
-------------------
a garbage collector is a process that automaticalley frees the memorey of object that are no more in use.the decision to involve the destructor is made by
a componentt of the CLR known as the garbage collector.
ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Constructordemo
{
class test
{
int a, b;
static int c;
static test()
{
c = 34;
Console.WriteLine("static constructor invoked");
}
public test() //instance default constructor
{
a = 30;
b = 12;
Console.WriteLine("default constructor invoked");
}
public test(int p, int q)// instance parametariged
{
a = p;
b = q;
Console.WriteLine("2param constructor invoked");
}
public test(int p)
{
a = p;
Console.WriteLine("1 param constructor invoked");
}
public void display()
{
Console.WriteLine("{0}\n{1}\n{2}",a,b,c);
}
~test()//destructor
{
Console.WriteLine(" i am in the process of destroying the obj");
}
static void Main(string[] args)
{
test obj=new test();//static instance default
obj.display();
test obj2=new test();
obj2.display();
test obj3=new test(10,4);
obj3.display();
test obj4=new test(67);// instance param const
obj4.display();
Console.ReadLine();
}
}
}
-----------------------------------------------------------THE --END------------------------------------
No comments:
Post a Comment