oops in c#

oops in c# with example

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:
  • Abstraction.
  • Encapsulation.
  • Inheritance and.
  • Polymorphism.
Abstraction
  1. Abstraction is "To represent the essential feature without representing the background details."
  2. Abstraction lets you focus on what the object does instead of how it does it.
  3. Abstraction provides you a generalized view of your classes or objects by providing relevant information.
  4. Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.


Real-world Example of Abstraction
Suppose you have an object Mobile Phone.
Suppose you have 3 mobile phones as in the following:
Nokia 1400 (Features: Calling, SMS)
Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

Abstract information (necessary and common information) for the object "Mobile Phone" is that it makes a call to any number and can send SMS.

So that, for a mobile phone object you will have the abstract class as in the following,
  1. abstract class MobilePhone {  
  2.     public void Calling();  
  3.     public void SendSMS();  
  4. }  
  5. public class Nokia1400: MobilePhone {}  
  6. public class Nokia2700: MobilePhone {  
  7.     public void FMRadio();  
  8.     public void MP3();  
  9.     public void Camera();  
  10. }  
  11. public class BlackBerry: MobilePhone {  
  12.     public void FMRadio();  
  13.     public void MP3();  
  14.     public void Camera();  
  15.     public void Recording();  
  16.     public void ReadAndSendEmails();  
  17. }  
Abstraction means putting all the variables and methods in a class that are necessary.For example: Abstract class and abstract method.Abstraction is a common thing.
Example
If somebody in your collage tells you to fill in an application form, you will provide your details, like name, address, date of birth, which semester, percentage you have etc .
If some doctor gives you an application to fill in the details, you will provide the details, like name, address, date of birth, blood group, height and weight.
See in the preceding example what is in common?
Age, name and address, so you can create a class that consists of the common data. That is called an abstract class.
That class is not complete and it can be inherited by other classes.

Encapsulation
Wrapping up a data member and a method together into a single unit (in other words class) is called Encapsulation.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.
  1. class Bag {  
  2.     book;  
  3.     pen;  
  4.     ReadBook();  
  5. }  
Encapsulation means hiding the internal details of an object, in other words how an object does something.

Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.

Encapsulation is a technique used to protect the information in an object from another object.

Hide the data for security such as making the variables private, and expose the property to access the private data that will be public. 
So, when you access the property you can validate the data and set it.

Example 1
  1. class Demo {  
  2.     private int _mark;  
  3.     public int Mark {  
  4.         get {  
  5.             return _mark;  
  6.         }  
  7.         set {  
  8.             if (_mark > 0) _mark = value;  
  9.             else _mark = 0;  
  10.         }  
  11.     }  
  12. }  
Real-world Example of Encapsulation
Let's use as an example Mobile Phones and Mobile Phone Manufacturers.
Suppose you are a Mobile Phone Manufacturer and you have designed and developed a Mobile Phone design (a class). Now by using machinery you are manufacturing Mobile Phones (objects) for selling, when you sell your Mobile Phone the user only learns how to use the Mobile Phone but not how the Mobile Phone works.

This means that you are creating the class with functions and by with objects (capsules) of which you are making available the functionality of your class by that object and without the interference in the original class.

Example 2
TV operation
It is encapsulated with a cover and we can operate it with a remote and there is no need to open the TV to change the channel.
Here everything is private except the remote, so that anyone can access the remote to operate and change the things in the TV.

Inheritance
When a class includes a property of another class it is known as inheritance.
Inheritance is a process of object re-usability.
For example, a child includes the properties of its parents.
  1. public class ParentClass {  
  2.     public ParentClass() {  
  3.         Console.WriteLine("Parent Constructor.");  
  4.     }  
  5.     public void print() {  
  6.         Console.WriteLine("I'm a Parent Class.");  
  7.     }  
  8. }  
  9. public class ChildClass: ParentClass {  
  10.     public ChildClass() {  
  11.         Console.WriteLine("Child Constructor.");  
  12.     }  
  13.     public static void Main() {  
  14.         ChildClass child = new ChildClass();  
  15.         child.print();  
  16.     }  
  17. }  
Output
Parent Constructor.
Child Constructor.
I'm a Parent Class.


Polymorphism

Polymorphism means one name, many forms.
One function behaves in different forms.
In other words, "Many forms of a single object is called Polymorphism."

Real-world Example of Polymorphism
Example 1
A teacher behaves students.
A teacher behaves his/her seniors.
Here teacher is an object but the attitude is different in different situations.
Example 2
A person behaves the son in a house at the same time that the person behaves an employee in an office.
Example 3
Your mobile phone, one name but many forms:
  • As phone
  • As camera
  • As mp3 player
  • As radio
polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.
Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.

Static Polymorphism

The mechanism of linking a function with an object during compile time is called early binding. It is also called static binding. C# provides two techniques to implement static polymorphism. They are −
  • Function overloading
  • Operator overloading
We discuss operator overloading in next chapter.

Function Overloading

You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.
The following example shows using function print() to print different data types −
 using System;

namespace PolymorphismApplication {
   class Printdata {
      void print(int i) {
         Console.WriteLine("Printing int: {0}", i );
      }
      void print(double f) {
         Console.WriteLine("Printing float: {0}" , f);
      }
      void print(string s) {
         Console.WriteLine("Printing string: {0}", s);
      }
      static void Main(string[] args) {
         Printdata p = new Printdata();
       
         // Call print to print integer
         p.print(5);
       
         // Call print to print float
         p.print(500.263);
       
         // Call print to print string
         p.print("Hello C++");
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following result 

Printing int: 5 Printing float: 500.263 Printing string: Hello C++

Dynamic Polymorphism

C# allows you to create abstract classes that are used to provide partial class implementation of an interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have more specialized functionality.
Here are the rules about abstract classes −
  • You cannot create an instance of an abstract class
  • You cannot declare an abstract method outside an abstract class
  • When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.
The following program demonstrates an abstract class −
using System;

namespace PolymorphismApplication {
   abstract class Shape {
      public abstract int area();
   }
   
   class Rectangle:  Shape {
      private int length;
      private int width;
      
      public Rectangle( int a = 0, int b = 0) {
         length = a;
         width = b;
      }
      public override int area () { 
         Console.WriteLine("Rectangle class area :");
         return (width * length); 
      }
   }
   class RectangleTester {
      static void Main(string[] args) {
         Rectangle r = new Rectangle(10, 7);
         double a = r.area();
         Console.WriteLine("Area: {0}",a);
         Console.ReadKey();
      }
   }
}
When the above code is compiled and executed, it produces the following result −
Rectangle class area :
Area: 70
When you have a function defined in a class that you want to be implemented in an inherited class(es), you use virtual functions. The virtual functions could be implemented differently in different inherited class and the call to these functions will be decided at runtime.
Dynamic polymorphism is implemented by abstract classes and virtual functions.
The following program demonstrates this −
using System;

namespace PolymorphismApplication {
   class Shape {
      protected int width, height;
      
      public Shape( int a = 0, int b = 0) {
         width = a;
         height = b;
      }
      public virtual int area() {
         Console.WriteLine("Parent class area :");
         return 0;
      }
   }
   class Rectangle: Shape {
      public Rectangle( int a = 0, int b = 0): base(a, b) {

      }
      public override int area () {
         Console.WriteLine("Rectangle class area :");
         return (width * height); 
      }
   }
   class Triangle: Shape {
      public Triangle(int a = 0, int b = 0): base(a, b) {
      }
      public override int area() {
         Console.WriteLine("Triangle class area :");
         return (width * height / 2); 
      }
   }
   class Caller {
      public void CallArea(Shape sh) {
         int a;
         a = sh.area();
         Console.WriteLine("Area: {0}", a);
      }
   }  
   class Tester {
      static void Main(string[] args) {
         Caller c = new Caller();
         Rectangle r = new Rectangle(10, 7);
         Triangle t = new Triangle(10, 5);
         
         c.CallArea(r);
         c.CallArea(t);
         Console.ReadKey();
      }
   }

}

What is difference between encapsulation and abstraction with examples?


Abstraction: only show relevant details and rest all hide it. Suppose I have three mobile like Nokia,Samsung,Lenovo. They have all properties like calling receiving other.
But main feature is showing but do not show how it work.

Encapsulation:Encapsulation is defined as the process of enclosing one or more details from outside world through access right .


When we switch on the Bluetooth I am able to connect another mobile but not able to access the other mobile features like dialing a number, accessing inbox etc. This is because, Bluetooth feature is given some level of abstraction. 

Post a Comment

0 Comments