Object Oriented Programming or OOPs is a widely used term in Computer Science . OOPs is a vital concept for every Computer Science students , it is asked not only in exams but also in jobs interviews as well .Many students find it difficult to understand these Object Oriented Programming (OOP) concepts . So I decided to write an article on it , I hope you will like it .
There are many terms in Object Oriented programing , together these terms are called as OOP s concepts . These OOP s concepts are used in all modern programming languages like Java , J2EE,C++ , C#, .Net , Python and Javascript .Lets us begin with these OOPs concepts.
Class
As per C# .net class is a collection fields,properties, method and events,Object is instance of a class.
A C# Class is considered to be the primary building block of the language. What I mean by the primary building block is that every time you work with C# you will create classes to form a program. We use classes as a template to put the properties and functionality or behaviors in one building block for a group of objects and after that we use the template to create the objects we need.
Objects
Objects can be considered as a thing that performs a set of related functions .Programming objects are used to model real worlds objects. An object is also an instant of a class . For our class Car , Ferrari will be our object
Inheritance
Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective employment of reuse, you can save time in your programming.
Definition :-Deriving the features from one class into another class is called inheritance.
Example1 for implementing Inheritence:
Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective employment of reuse, you can save time in your programming.
Definition :-Deriving the features from one class into another class is called inheritance.
Example1 for implementing Inheritence:
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Output:
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Example2:
// Inheritance
using System;
class Base
{
public int x = 10;
public int y = 20;
public void Method()
{
Console.WriteLine("Base Method");
}
}
class Derived : Base
{
public int z = 30;
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
Console.WriteLine("{0},{1},{2}",d1.x,d1.y,d1.z); // displays 10,20,30
d1.Method();// displays 'Base Method'
}
}
Note: C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. However, it does allow multiple interface inheritance.
Output:
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Example2:
// Inheritance
using System;
class Base
{
public int x = 10;
public int y = 20;
public void Method()
{
Console.WriteLine("Base Method");
}
}
class Derived : Base
{
public int z = 30;
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
Console.WriteLine("{0},{1},{2}",d1.x,d1.y,d1.z); // displays 10,20,30
d1.Method();// displays 'Base Method'
}
}
Note: C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. However, it does allow multiple interface inheritance.
Interfaces:
An Interface is a collection of abstract members
An interface looks like a class, but has no implementation.
An interface has no implementation; it only has the signature or in other words, just the declaraton of the methods without the body
An Interface is a reference type and it contains only abstract members
All the member declarations inside interface are implicitly public
Interface can contain declaration of events, indexers, methods and/or properties. They have no body, just the declaration.
The interface can't contain constants, data fields, constructors, destructors and static members
Interface cannot be instantiated
Example:
interface IMyInterface
{
void MethodToImplement();
}
defines an interface named IMyInterface. A common naming convention is to prefix all interface names with a capital "I". This interface has a single method named MethodToImplement(). This could have been any type of method declaration with different parameters and return types. I just chose to declare this method with no parameters and a void return type to make the example easy. Notice that this method does not have an implementation (instructions between curly braces - {}), but instead ends with a semi-colon, ";". This is because the interface only specifies the signature of methods that an inheriting class or struct must implement.
Example:. Using an Interface implementing Inheritance:
class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
}
public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
}
output=MethidToImplement() called
Example2:
using System;
interface IParentInterface
{
void ParentInterfaceMethod();
}
interface IMyInterface : IParentInterface
{
void MethodToImplement();
}
class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
iImp.ParentInterfaceMethod();
}
public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
public void ParentInterfaceMethod()
{
Console.WriteLine("ParentInterfaceMethod() called.");
}
}
output = MethodToImplement() called
ParentInterfaceMethod() called
The above example contains two interfaces: IMyInterface and the interface it inherits, IParentInterface. When one interface inherits another, any implementing class or struct must implement every interface member in the entire inheritance chain. Since the InterfaceImplementer class inherits from IMyInterface, it also inherits IParentInterface. Therefore, the InterfaceImplementer class must implement the MethodToImplement() method specified in the IMyInterface interface and the ParentInterfaceMethod() method specified in the IParentInterface interface.
Example 2 for Interface
using System;
using System.Collections.Generic;
using System.Text;
namespace Interfaces1
{
interface ICollegeInterface
{
void CollegeInterfaceMethod();
}
interface IStudentInterface : ICollegeInterface
{
void StudentInterfaceMethod();
}
class UniversityClass : IStudentInterface
{
static void Main(string[] args)
{
UniversityClass uni= new UniversityClass();
uni.CollegeInterfaceMethod();
uni.StudentInterfaceMethod();
Console.ReadLine();
}
public void CollegeInterfaceMethod()
{
Console.WriteLine("CollegeInterfaceMethod() called.");
}
public void StudentInterfaceMethod()
{
Console.WriteLine("StudentInterfaceMethod() called.");
}
}
}
Abstraction
Abstraction defines way to abstract or hide your data and members from outside world. Simply speaking Abstraction is hiding the complexities of your class or struct or in a generic term Type from outer world. This is achieved by means of access specifiers.
Definition: - Hiding the complexities of your type from outside world.
How to Abstract: - By using Access Specifiers
.Net has five access specifiers:-Public,private,prtotected,internal and protected internal.
Example1 for Abstraction and Encapsulation implimentation:
public class Class1
{
private int i; // private
public int j; // Public
protected int k; //Protected data
internal int m; // Internal means visible inside assembly
protected internal int n; //inside assembly as well as to derived classes outside assembly
public static int y; //Static
public void myFoo()
{
//Within a class if you create an object of same class then you can access all data members through object reference even private data too
Class1 obj = new Class1();
obj.i =10; //Error can’t access private data through object.But here it is accessible.:)
obj.j =10;
obj.k=10;
obj.m=10;
obj.n=10;
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}
}
Now lets try to write the same code inside Main method and try to compile
static void Main()
{
//Access specifiers comes into picture only when you create object of class outside the class
Class1 obj = new Class1();
// obj.i =10; //Error can’t access private data //through object.
obj.j =10;
// //obj.k=10; //Error can’t access protected data through object.
obj.m=10;
obj.n=10;
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}
What if Main is inside another assembly
static void Main()
{
//Access specifiers comes into picture only when you create object of class outside the class
Class1 obj = new Class1();
// obj.i =10; //Error can’t access private data through object.
obj.j =10;
// obj.k=10; //Error can’t access protected data through object.
// obj.m=10; // Error can’t access internal data outside assembly
// obj.n=10; // Error can’t access protected internal data outside assembly
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}
Encapsulation
Encapsulation is process of keeping data and methods together inside objects
Definition:- Binding data and member functions together inside a single unit.
How to Encapsulate:- By creating types e.g Classes and Struct
Polymorphism
What is polymorphism?
The meaning of the word polymorphism is something like one name, many forms.
How does C# implement polymorphism?
Defination:providing many functionalities with a single insatance name
Polymorphism manifests itself in C# in the form of multiple methods having the same name.
From a practical programming viewpoint, polymorphism manifests itself in two distinct forms in C#:
Fucntion Overloading:
Ø Overloading means having more than one method with the same name, but a different number(list) of arguments or diiferent type of arguments in the same class or in a combination of base and derived classes .
Example:
class Projector
{
public static void Display( string str )
{
System.Console.WriteLine( str );
}
public static void Display( int number )
{
System.Console.WriteLine( number);
}
public static void Main()
{
Projector.Display( "Hello" );
Projector.Display( 1234 );
}
}
Output:
Hello
1234
Function Overloading:-
Function overloading depends on:-
1.) Variable Number of parameters to function
2.) Order of parameters to function
3.) Data type of parameters
First we will consider cases on which function overloading depends on
Variable Number of parameters to function
Example 1:
public class MyBaseClass
{
public void foo()
{
Console.WriteLine("Hi I am original");
}
public void foo(int i)
{
Console.WriteLine("Hi I am overloaded and here is the value of i " + i);
}
}
In this example, number of parameters are different so this will compile fine and now when you create object of this class you will see two overloaded version of foo.
Lets now create object of this class and call both the versions:-
MyBaseClass obj = new MyBaseClass();
obj.foo();
obj.foo(10);
output:-
Hi I am original
Hi I am overloaded and here is the value of i 10
2) Order of parameters to function:
public class MyBaseClass
{
public void foo(int i , string s)
{
Console.WriteLine("The parameter order is int and string.The value of i is {0} and that of s is {1}",i,s);
}
public void foo(string s, int i)
{
Console.WriteLine("The parameter order is string and int.The value of s is {0} and that of i is {1}", s, i);
}
}
In the example above, number of parameters are same but since the order is different so this will compile with out errors.
Lets now create object of this class and call both the versions:-
MyBaseClass obj = new MyBaseClass();
obj.foo(1,”shakti”);
obj.foo(“Test”,2);
output:-
The parameter order is int and string.The value of i is 1 and that of s is shakti
The parameter order is string and int.The value of s is Test and that of i is 2
3)Data type of parameters:
public class MyBaseClass
{
public void foo(int i)
{
Console.WriteLine("This is one parameter function which takes int as parameter and value of i is {0}.",i);
}
public void foo(string s )
{
Console.WriteLine("This is one parameter function which takes string as parameter and value of s is {0}.", s);
}
}
Here the number of parameters are same but data types are different so this will compile fine.
Lets now create object of this class and call both the versions:-
MyBaseClass obj = new MyBaseClass();
obj.foo(10);
obj.foo("Test");
Output:-
This is one parameter function which takes int as parameter and value of i is 10.
This is one parameter function which takes string as parameter and value of s is Test.
Function overloading doesn’t depend on:-
1.) Return type of function
2.) Name of parameters
Now we will consider cases on which function overloading does not depend on:-
1.) Return type of function:
public class MyBaseClass
{
public void foo()
{
Console.WriteLine("Hi I am original");
}
public int foo()
{
Console.WriteLine("Hi I am Copy");
return 1;
}
}
The code sample will generate the following error:-
Type ‘MyBaseClass' already defines a member called 'foo' with the same parameter types
2.) Name of parameters:
public class MyBaseClass
{
public void foo(int i)
{
Console.WriteLine("Hi I am original");
}
public int foo(int j)
{
Console.WriteLine("Hi I am Copy");
return 1;
}
}
The above code will give the following error:-
Type 'MyBaseClass' already defines a member called 'foo' with the same parameter types.
Examples.for Method Overloading possibilities:
1)void calaculate();
2)void calaculate(int a); //ok:u can overload
3)void calaculate(string a, int b);//ok u can overload
4)void calaculate(string h, int t);//error 3 and 4 can’t overload,because signatures are same
5)int calaculate(int g); //error:2 and 5 can't overload based on return type only
//ref method
6)void calaulate(ref int x);// u can overload with all the above methods but u can’t overload with 7th method.
// out method
7)void calaculate(out int x);// u can not overload 6th and 7th methods,but with other methods u can overload 6th and 7th methods individually.
2)Function Overriding:
Overriding means have a method with the same name and the same signature in the base class and derived classes.
To override a method we have to use “virtual” keyword for base class method and override keyword for derived class method.
Example:
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public override void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Derived d1 ;
d1= new Derived();
d1.Method(); // Displays 'Derived Method'
}
}
When we declare a virtual method, it must contain a method body. Other wise the compiler will generate an error.
Remember that, since virtual methods are used for achieving polymorphism and since polymorphism works only with objects, it not possible to declare a static method as virtual in C#.
Similarly the private methods are also not possible to declare virtual, since they can’t override inside a derived class.
Method overriding is a feature that allows you to invoke functions (that have the same signatures) that belong to different classes in the same hierarchy of inheritance using the base class reference..
Both methods should have the same access level; the same return type, the same name and same method signature.
Let's understand this through small examples.
Example for NEW keyword:
We can override a method without declaring the base class method as 'virtual'. We can use the modifier 'new' to tell the compiler that the derived class method 'hides' the base class method.
class Base
{
public void Display()
{
Console.WriteLine("Base method");
}
}
class Derived:Base
{
new public void Display()
{
Console.WriteLine("Drived method");
}
}
class HideTest
{
public static void Main()
{
Base b=new Base();
b.display();
Derived d=new Derived();
d.Display();
}
}
Output:
Base method
Derived method
Example1:
class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class Demo
{
public static void Main()
{
BC b; // creating reference variable b of type BC
b = new BC(); //creating object of type BC and assign its reference to reference variable b
b.Display();
}
}
Output
BC::Display
The above program compiles and runs successfully to give the desired output. It consists of a base class BC and a derived class DC. Class BC consists of function Display(). Class DC hides the function Display() it inherited from the base class BC by providing its on implementatin of Display(). Class Demo consists of entrypoint function Main(). Inside Main() we first create a reference b of type BC. Then we create an object of type BC and assign its reference to reference variable b. Using the reference variable b we invoke the function Display(). As expected, Display() of class BC is executed because the reference variable b refers to the object of class BC.
Now we add a twist of line to the above program.
Example 2
class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();
b = new DC();
b.Display();
}
}
Output
BC::Display
BC::Display
Here we are creating an object of Derived class DC and storing its reference in the reference variable b of type BC. This is valid in C#. Next, using the reference variable b we invoke the function Display(). Since b contains a reference to object of type DC one would expect the function Display() of class DC to get executed. But that does not happen. Instead what is executed is the Display() of BC class. That's because the function is invoked based on type of the reference and not to what the reference variable b refers to. Since b is a reference of type BC, the function Display() of class BC will be invoked, no matter whom b refers to. Take one more example.
Example 3
class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class TC : BC
{
new public void Display()
{
System.Console.WriteLine("TC::Display");
}
}
class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();
b = new DC();
b.Display();
b = new TC();
b.Display();
}
}
Output
BC::Display
BC::Display
BC::Display
The output of the above program is a receipt of the fact that no matter to whom base class reference b refers, it invokes the functions of the class that matches its type.
But actually , If b contains the reference to a particular derived class object, then its supposed to invoke the function of that class.
Well, C# helps us do this by the usage of keywords virtual and override as shown in the following program.
Example 4
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();
b = new DC();
b.Display();
}
}
Output
BC::Display
DC::Display
The above program compiles and runs successfully to give the expected desired output. The function Display() of Base class BC is declared as virtual, while the Derived class's implementation of Display() is decorated with the modifier override. Doing so enables C# to invoke functions like Display() based on objects the reference variable refers to and not the type of reference that is invoking the function. Hence in the above program when b refers to the object of class BC it invokes Display() of BC and then when b refers to the object of class DC it invokes Display() of class DC. Let's see if this holds true for the third generation of derived classes. Take the following program.
Example 5
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class TC : DC
{
public override void Display()
{
System.Console.WriteLine("TC::Display");
}
}
class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();
b = new DC();
b.Display();
b = new TC();
b.Display();
}
}
Output
BC::Display
DC::Display
TC::Display
The above program compiles and runs successfully to give the expected desired output. The function Display() of Base class BC is declared as virtual, while the implementation of Display() in successive Derived classes is decorated with the modifier override. Next, we succesively create objects of each class and store their reference in base class reference variable b and invoke Display(). The rite versions of Display get invoked based on the object the reference variable refers to. Time for a tiny teaser! Guess what the output would be in the following program?
Example 6
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class TC : DC
{
}
class Demo
{
public static void Main()
{
BC b;
b = new TC();
b.Display();
}
}
Output
DC::Display
Since TC has no implementation of Display(), it inherits Display() from DC as TC is derived from DC. Hence Display() from Derived class DC gets executed. It's as if the derived class TC looked like this:
class TC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
to the compiler. Take one more example. Guess what its output will be.
Example 7
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}
class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
class TC : DC
{
public new void Display()
{
System.Console.WriteLine("TC::Display");
}
}
class Demo
{
public static void Main()
{
BC b;
b = new TC();
b.Display();
}
}
Output
DC::Display
Agreed that TC defines its own new version of Display(). But its version of display is not invoked as Display() of TC does not override the Display() of the base class. With this understood we are done with Method overriding in C#.
Differences between Function overriding and Function overloading
Function Overloading Function Overriding
1) Both method names should be same,but signature should not be same.
1) Both method names,and signature and retun type should be same
2) In Overloading we will not use any virtual and override keywords 2) In overriding we have to use Virtual and Override keywords
3)Overloading we can implement with in single class or combination
of base and derived classes.
3)We can implement Overriding in base class and derived class but we can not implement in a single class
No comments:
Post a Comment