• Save
Polymorphism
Upcoming SlideShare
Loading in...5
×
 

Polymorphism

on

  • 2,825 views

 

Statistics

Views

Total Views
2,825
Views on SlideShare
2,416
Embed Views
409

Actions

Likes
4
Downloads
0
Comments
1

3 Embeds 409

http://beta.dec.uwi.edu 221
http://myelearning.sta.uwi.edu 184
http://www.linkedin.com 4

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • I liked it.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Polymorphism Polymorphism Presentation Transcript

    • POLYMORPHISM Concept & Application Prepared By:- Rakhi Kumari Kumar Gaurav
    • Polymorphism: Literal meaning• polymorphism in Latin word which made up of poly means many and morphs means forms.• From the Greek: – Polus + Morphe = Polumorphos (many ) (shape/form)• This is something similar to a word having several different meanings depending on the context
    • A simple word ‘Cut’ can have different meaningdepending where it is used
    • Polymorphism, In Context Of OOP• Polymorphism is about an objects ability to provide context when methods or operators are called on the object.• In OOP, Polymorphism is the characteristic of being able to assign a different meaning to a particular symbol or "operator" in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
    • Polymorphism• When a program invokes a method through a superclass variable, – the correct subclass version of the method is called, – based on the type of the reference stored in the superclass variable• The same method name and signature can cause different actions to occur, – depending on the type of object on which the method is invoked 5
    • Polymorphism• Polymorphism enables programmers to deal in generalities and – let the execution-time environment handle the specifics.• Programmers can command objects to behave in manners appropriate to those objects, – without knowing the types of the objects – (as long as the objects belong to the same inheritance hierarchy). 6
    • The Meaning of the word.• Example: The operator + has a different meaning in the expression 2 + 3 (add two integers) than in 1.7 + 3.3 (add two floating point numbers) 7
    • Types of polymorphism• 1. compile time polymorphism ( function and operator overloading)• 2.run time polymorphism (virtual functions)
    • Method & Function Overloading• Overloading a function simply means, that a function is not only defined its name but by its name and parameter types. These three• The following functions are different in C++: are methods – Int area(int i, int k); different. – void area( float i, float k); – Float area(); 9
    • Method Overloadingusing System; class maindemousing System.Collections.Generic; {using System.Linq; static void Main(string[] args)using System.Text; { Area a1= new Area(); a1.length=10; a1.breadth=20; class Area{ int area1 =a1.calc(10,20); public double length; double area2= a1.calc(10.5, 20.2); public double breadth; a1.calc(); Console.WriteLine(); Console.Write("Area =" + area1); public int calc(int l, int b) Console.WriteLine(); { return (l * b); } Console.Write("Area =" + area2); public double calc(double l,double b) Console.WriteLine(); { return (l * b); } } public void calc() } { Console.Write("Area =" + 40 ); } }
    • outputArea=40;Area=200;Area=212.1;
    • Method overriding class car:vechileusing System; {Using System.Collections.Generic; public override void display()using System.Linq; {using System.Text; Console.WriteLine(" It is from child Car"); } } class vechile class maindemo { { public virtual void display() static void Main(string[] args) { { vechile v1= new car(); Console.WriteLine(“It is from v1.display(); parent Vechile"); } } } }
    • outputIt is from child Car
    • Run-time polymorphism• Run-time polymorphism, also called dynamic binding, or late binding is often considered as the object oriented feature of C#.• Dynamic means objects are created at run time• Dynamic binding offers greater flexibility and a higher level of abstraction than static binding because it is done "on the fly" when a program executes
    • Static typing & Dynamic binding• Static typing means that the • Dynamic binding means that legality of a member the address of the code in a function invocation is member function invocation checked at the earliest is determined at the last possible moment: by the possible moment: based on the dynamic type of the compiler at compile time. object at run time. It is called The compiler uses the static "dynamic binding" because type of the pointer to the binding to the code that determine whether the actually gets called is member function accomplished dynamically invocation is legal. (at run time). 15
    • Static typing & Dynamic binding• With static binding, you • Dynamic binding offers get better run time greater flexibility and a efficiency because the higher level of compiler can actually abstraction than static optimize the code binding because it is before running it. done "on the fly" when a program executes.• The CLR knows how • The CLR doesnt know much memory to take how much memory to up for the static method take up for the dynamic object method object